1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
4  *
5  * Contact Information: wlanfae <wlanfae@realtek.com>
6  */
7 #include <linux/string.h>
8 #include "rtl_core.h"
9 #include "rtl_wx.h"
10 
11 #define RATE_COUNT 12
12 static u32 rtl8192_rates[] = {
13 	1000000, 2000000, 5500000, 11000000, 6000000, 9000000, 12000000,
14 	18000000, 24000000, 36000000, 48000000, 54000000
15 };
16 
17 #ifndef ENETDOWN
18 #define ENETDOWN 1
19 #endif
20 
_rtl92e_wx_get_freq(struct net_device * dev,struct iw_request_info * a,union iwreq_data * wrqu,char * b)21 static int _rtl92e_wx_get_freq(struct net_device *dev,
22 			       struct iw_request_info *a,
23 			       union iwreq_data *wrqu, char *b)
24 {
25 	struct r8192_priv *priv = rtllib_priv(dev);
26 
27 	return rtllib_wx_get_freq(priv->rtllib, a, wrqu, b);
28 }
29 
_rtl92e_wx_get_mode(struct net_device * dev,struct iw_request_info * a,union iwreq_data * wrqu,char * b)30 static int _rtl92e_wx_get_mode(struct net_device *dev,
31 			       struct iw_request_info *a,
32 			       union iwreq_data *wrqu, char *b)
33 {
34 	struct r8192_priv *priv = rtllib_priv(dev);
35 
36 	return rtllib_wx_get_mode(priv->rtllib, a, wrqu, b);
37 }
38 
_rtl92e_wx_get_rate(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)39 static int _rtl92e_wx_get_rate(struct net_device *dev,
40 			       struct iw_request_info *info,
41 			       union iwreq_data *wrqu, char *extra)
42 {
43 	struct r8192_priv *priv = rtllib_priv(dev);
44 
45 	return rtllib_wx_get_rate(priv->rtllib, info, wrqu, extra);
46 }
47 
_rtl92e_wx_set_rate(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)48 static int _rtl92e_wx_set_rate(struct net_device *dev,
49 			       struct iw_request_info *info,
50 			       union iwreq_data *wrqu, char *extra)
51 {
52 	int ret;
53 	struct r8192_priv *priv = rtllib_priv(dev);
54 
55 	if (priv->hw_radio_off)
56 		return 0;
57 
58 	mutex_lock(&priv->wx_mutex);
59 
60 	ret = rtllib_wx_set_rate(priv->rtllib, info, wrqu, extra);
61 
62 	mutex_unlock(&priv->wx_mutex);
63 
64 	return ret;
65 }
66 
_rtl92e_wx_set_rts(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)67 static int _rtl92e_wx_set_rts(struct net_device *dev,
68 			      struct iw_request_info *info,
69 			      union iwreq_data *wrqu, char *extra)
70 {
71 	int ret;
72 	struct r8192_priv *priv = rtllib_priv(dev);
73 
74 	if (priv->hw_radio_off)
75 		return 0;
76 
77 	mutex_lock(&priv->wx_mutex);
78 
79 	ret = rtllib_wx_set_rts(priv->rtllib, info, wrqu, extra);
80 
81 	mutex_unlock(&priv->wx_mutex);
82 
83 	return ret;
84 }
85 
_rtl92e_wx_get_rts(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)86 static int _rtl92e_wx_get_rts(struct net_device *dev,
87 			      struct iw_request_info *info,
88 			      union iwreq_data *wrqu, char *extra)
89 {
90 	struct r8192_priv *priv = rtllib_priv(dev);
91 
92 	return rtllib_wx_get_rts(priv->rtllib, info, wrqu, extra);
93 }
94 
_rtl92e_wx_set_power(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)95 static int _rtl92e_wx_set_power(struct net_device *dev,
96 				struct iw_request_info *info,
97 				union iwreq_data *wrqu, char *extra)
98 {
99 	int ret;
100 	struct r8192_priv *priv = rtllib_priv(dev);
101 
102 	if (priv->hw_radio_off) {
103 		netdev_warn(dev, "%s(): Can't set Power: Radio is Off.\n",
104 			    __func__);
105 		return 0;
106 	}
107 	mutex_lock(&priv->wx_mutex);
108 
109 	ret = rtllib_wx_set_power(priv->rtllib, info, wrqu, extra);
110 
111 	mutex_unlock(&priv->wx_mutex);
112 
113 	return ret;
114 }
115 
_rtl92e_wx_get_power(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)116 static int _rtl92e_wx_get_power(struct net_device *dev,
117 				struct iw_request_info *info,
118 				union iwreq_data *wrqu, char *extra)
119 {
120 	struct r8192_priv *priv = rtllib_priv(dev);
121 
122 	return rtllib_wx_get_power(priv->rtllib, info, wrqu, extra);
123 }
124 
_rtl92e_wx_set_mode(struct net_device * dev,struct iw_request_info * a,union iwreq_data * wrqu,char * b)125 static int _rtl92e_wx_set_mode(struct net_device *dev,
126 			       struct iw_request_info *a,
127 			       union iwreq_data *wrqu, char *b)
128 {
129 	struct r8192_priv *priv = rtllib_priv(dev);
130 
131 	enum rt_rf_power_state rt_state;
132 	int ret;
133 
134 	if (priv->hw_radio_off)
135 		return 0;
136 	rt_state = priv->rtllib->rf_power_state;
137 	mutex_lock(&priv->wx_mutex);
138 	if (wrqu->mode == IW_MODE_MONITOR) {
139 		if (rt_state == rf_off) {
140 			if (priv->rtllib->rf_off_reason >
141 			    RF_CHANGE_BY_IPS) {
142 				netdev_warn(dev, "%s(): RF is OFF.\n",
143 					    __func__);
144 				mutex_unlock(&priv->wx_mutex);
145 				return -1;
146 			}
147 			netdev_info(dev,
148 				    "=========>%s(): rtl92e_ips_leave\n",
149 				    __func__);
150 			mutex_lock(&priv->rtllib->ips_mutex);
151 			rtl92e_ips_leave(dev);
152 			mutex_unlock(&priv->rtllib->ips_mutex);
153 		}
154 	}
155 	ret = rtllib_wx_set_mode(priv->rtllib, a, wrqu, b);
156 
157 	mutex_unlock(&priv->wx_mutex);
158 	return ret;
159 }
160 
_rtl92e_wx_get_range(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)161 static int _rtl92e_wx_get_range(struct net_device *dev,
162 				struct iw_request_info *info,
163 				union iwreq_data *wrqu, char *extra)
164 {
165 	struct iw_range *range = (struct iw_range *)extra;
166 	struct r8192_priv *priv = rtllib_priv(dev);
167 	u16 val;
168 	int i;
169 
170 	wrqu->data.length = sizeof(*range);
171 	memset(range, 0, sizeof(*range));
172 
173 	/* ~130 Mb/s real (802.11n) */
174 	range->throughput = 130 * 1000 * 1000;
175 
176 	range->max_qual.qual = 100;
177 	range->max_qual.level = 0;
178 	range->max_qual.noise = 0;
179 	range->max_qual.updated = 7; /* Updated all three */
180 
181 	range->avg_qual.qual = 70; /* > 8% missed beacons is 'bad' */
182 	range->avg_qual.level = 0;
183 	range->avg_qual.noise = 0;
184 	range->avg_qual.updated = 7; /* Updated all three */
185 
186 	range->num_bitrates = min(RATE_COUNT, IW_MAX_BITRATES);
187 
188 	for (i = 0; i < range->num_bitrates; i++)
189 		range->bitrate[i] = rtl8192_rates[i];
190 
191 	range->max_rts = DEFAULT_RTS_THRESHOLD;
192 	range->min_frag = MIN_FRAG_THRESHOLD;
193 	range->max_frag = MAX_FRAG_THRESHOLD;
194 
195 	range->min_pmp = 0;
196 	range->max_pmp = 5000000;
197 	range->min_pmt = 0;
198 	range->max_pmt = 65535 * 1000;
199 	range->pmp_flags = IW_POWER_PERIOD;
200 	range->pmt_flags = IW_POWER_TIMEOUT;
201 	range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_ALL_R;
202 	range->we_version_compiled = WIRELESS_EXT;
203 	range->we_version_source = 18;
204 
205 	for (i = 0, val = 0; i < 14; i++) {
206 		if ((priv->rtllib->active_channel_map)[i + 1]) {
207 			s32 freq_khz;
208 
209 			range->freq[val].i = i + 1;
210 			freq_khz = ieee80211_channel_to_freq_khz(i + 1, NL80211_BAND_2GHZ);
211 			range->freq[val].m = freq_khz * 100;
212 			range->freq[val].e = 1;
213 			val++;
214 		}
215 
216 		if (val == IW_MAX_FREQUENCIES)
217 			break;
218 	}
219 	range->num_frequency = val;
220 	range->num_channels = val;
221 	range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
222 			  IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
223 	range->scan_capa = IW_SCAN_CAPA_ESSID | IW_SCAN_CAPA_TYPE;
224 
225 	/* Event capability (kernel + driver) */
226 
227 	return 0;
228 }
229 
_rtl92e_wx_set_scan(struct net_device * dev,struct iw_request_info * a,union iwreq_data * wrqu,char * b)230 static int _rtl92e_wx_set_scan(struct net_device *dev,
231 			       struct iw_request_info *a,
232 			       union iwreq_data *wrqu, char *b)
233 {
234 	struct r8192_priv *priv = rtllib_priv(dev);
235 	struct rtllib_device *ieee = priv->rtllib;
236 	enum rt_rf_power_state rt_state;
237 	int ret;
238 
239 	if (!(ieee->softmac_features & IEEE_SOFTMAC_SCAN)) {
240 		if ((ieee->link_state >= RTLLIB_ASSOCIATING) &&
241 		    (ieee->link_state <= RTLLIB_ASSOCIATING_AUTHENTICATED))
242 			return 0;
243 		if ((priv->rtllib->link_state == MAC80211_LINKED) &&
244 		    (priv->rtllib->cnt_after_link < 2))
245 			return 0;
246 	}
247 
248 	if (priv->hw_radio_off) {
249 		netdev_info(dev, "================>%s(): hwradio off\n",
250 			    __func__);
251 		return 0;
252 	}
253 	rt_state = priv->rtllib->rf_power_state;
254 	if (!priv->up)
255 		return -ENETDOWN;
256 	if (priv->rtllib->link_detect_info.busy_traffic)
257 		return -EAGAIN;
258 
259 	if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
260 		struct iw_scan_req *req = (struct iw_scan_req *)b;
261 
262 		if (req->essid_len) {
263 			int len = min_t(int, req->essid_len, IW_ESSID_MAX_SIZE);
264 
265 			ieee->current_network.ssid_len = len;
266 			memcpy(ieee->current_network.ssid, req->essid, len);
267 		}
268 	}
269 
270 	mutex_lock(&priv->wx_mutex);
271 
272 	priv->rtllib->first_ie_in_scan = true;
273 
274 	if (priv->rtllib->link_state != MAC80211_LINKED) {
275 		if (rt_state == rf_off) {
276 			if (priv->rtllib->rf_off_reason >
277 			    RF_CHANGE_BY_IPS) {
278 				netdev_warn(dev, "%s(): RF is OFF.\n",
279 					    __func__);
280 				mutex_unlock(&priv->wx_mutex);
281 				return -1;
282 			}
283 			mutex_lock(&priv->rtllib->ips_mutex);
284 			rtl92e_ips_leave(dev);
285 			mutex_unlock(&priv->rtllib->ips_mutex);
286 		}
287 		rtllib_stop_scan(priv->rtllib);
288 		if (priv->rtllib->rf_power_state != rf_off) {
289 			priv->rtllib->actscanning = true;
290 
291 			ieee->scan_operation_backup_handler(ieee->dev, SCAN_OPT_BACKUP);
292 
293 			rtllib_start_scan_syncro(priv->rtllib);
294 
295 			ieee->scan_operation_backup_handler(ieee->dev, SCAN_OPT_RESTORE);
296 		}
297 		ret = 0;
298 	} else {
299 		priv->rtllib->actscanning = true;
300 		ret = rtllib_wx_set_scan(priv->rtllib, a, wrqu, b);
301 	}
302 
303 	mutex_unlock(&priv->wx_mutex);
304 	return ret;
305 }
306 
_rtl92e_wx_get_scan(struct net_device * dev,struct iw_request_info * a,union iwreq_data * wrqu,char * b)307 static int _rtl92e_wx_get_scan(struct net_device *dev,
308 			       struct iw_request_info *a,
309 			       union iwreq_data *wrqu, char *b)
310 {
311 	int ret;
312 	struct r8192_priv *priv = rtllib_priv(dev);
313 
314 	if (!priv->up)
315 		return -ENETDOWN;
316 
317 	if (priv->hw_radio_off)
318 		return 0;
319 
320 	mutex_lock(&priv->wx_mutex);
321 
322 	ret = rtllib_wx_get_scan(priv->rtllib, a, wrqu, b);
323 
324 	mutex_unlock(&priv->wx_mutex);
325 
326 	return ret;
327 }
328 
_rtl92e_wx_set_essid(struct net_device * dev,struct iw_request_info * a,union iwreq_data * wrqu,char * b)329 static int _rtl92e_wx_set_essid(struct net_device *dev,
330 				struct iw_request_info *a,
331 				union iwreq_data *wrqu, char *b)
332 {
333 	struct r8192_priv *priv = rtllib_priv(dev);
334 	int ret;
335 
336 	if (priv->hw_radio_off) {
337 		netdev_info(dev,
338 			    "=========>%s():hw radio off,or Rf state is rf_off, return\n",
339 			    __func__);
340 		return 0;
341 	}
342 	mutex_lock(&priv->wx_mutex);
343 	ret = rtllib_wx_set_essid(priv->rtllib, a, wrqu, b);
344 
345 	mutex_unlock(&priv->wx_mutex);
346 
347 	return ret;
348 }
349 
_rtl92e_wx_get_essid(struct net_device * dev,struct iw_request_info * a,union iwreq_data * wrqu,char * b)350 static int _rtl92e_wx_get_essid(struct net_device *dev,
351 				struct iw_request_info *a,
352 				union iwreq_data *wrqu, char *b)
353 {
354 	int ret;
355 	struct r8192_priv *priv = rtllib_priv(dev);
356 
357 	mutex_lock(&priv->wx_mutex);
358 
359 	ret = rtllib_wx_get_essid(priv->rtllib, a, wrqu, b);
360 
361 	mutex_unlock(&priv->wx_mutex);
362 
363 	return ret;
364 }
365 
_rtl92e_wx_set_nick(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)366 static int _rtl92e_wx_set_nick(struct net_device *dev,
367 			       struct iw_request_info *info,
368 			       union iwreq_data *wrqu, char *extra)
369 {
370 	struct r8192_priv *priv = rtllib_priv(dev);
371 
372 	if (wrqu->data.length > IW_ESSID_MAX_SIZE)
373 		return -E2BIG;
374 	mutex_lock(&priv->wx_mutex);
375 	wrqu->data.length = min_t(size_t, wrqu->data.length,
376 				  sizeof(priv->nick));
377 	memset(priv->nick, 0, sizeof(priv->nick));
378 	memcpy(priv->nick, extra, wrqu->data.length);
379 	mutex_unlock(&priv->wx_mutex);
380 	return 0;
381 }
382 
_rtl92e_wx_get_nick(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)383 static int _rtl92e_wx_get_nick(struct net_device *dev,
384 			       struct iw_request_info *info,
385 			       union iwreq_data *wrqu, char *extra)
386 {
387 	struct r8192_priv *priv = rtllib_priv(dev);
388 
389 	mutex_lock(&priv->wx_mutex);
390 	wrqu->data.length = strlen(priv->nick);
391 	memcpy(extra, priv->nick, wrqu->data.length);
392 	wrqu->data.flags = 1;   /* active */
393 	mutex_unlock(&priv->wx_mutex);
394 	return 0;
395 }
396 
_rtl92e_wx_set_freq(struct net_device * dev,struct iw_request_info * a,union iwreq_data * wrqu,char * b)397 static int _rtl92e_wx_set_freq(struct net_device *dev,
398 			       struct iw_request_info *a,
399 			       union iwreq_data *wrqu, char *b)
400 {
401 	int ret;
402 	struct r8192_priv *priv = rtllib_priv(dev);
403 
404 	if (priv->hw_radio_off)
405 		return 0;
406 
407 	mutex_lock(&priv->wx_mutex);
408 
409 	ret = rtllib_wx_set_freq(priv->rtllib, a, wrqu, b);
410 
411 	mutex_unlock(&priv->wx_mutex);
412 	return ret;
413 }
414 
_rtl92e_wx_get_name(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)415 static int _rtl92e_wx_get_name(struct net_device *dev,
416 			       struct iw_request_info *info,
417 			       union iwreq_data *wrqu, char *extra)
418 {
419 	struct r8192_priv *priv = rtllib_priv(dev);
420 
421 	return rtllib_wx_get_name(priv->rtllib, info, wrqu, extra);
422 }
423 
_rtl92e_wx_set_frag(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)424 static int _rtl92e_wx_set_frag(struct net_device *dev,
425 			       struct iw_request_info *info,
426 			       union iwreq_data *wrqu, char *extra)
427 {
428 	struct r8192_priv *priv = rtllib_priv(dev);
429 
430 	if (priv->hw_radio_off)
431 		return 0;
432 
433 	if (wrqu->frag.disabled) {
434 		priv->rtllib->fts = DEFAULT_FRAG_THRESHOLD;
435 	} else {
436 		if (wrqu->frag.value < MIN_FRAG_THRESHOLD ||
437 		    wrqu->frag.value > MAX_FRAG_THRESHOLD)
438 			return -EINVAL;
439 
440 		priv->rtllib->fts = wrqu->frag.value & ~0x1;
441 	}
442 
443 	return 0;
444 }
445 
_rtl92e_wx_get_frag(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)446 static int _rtl92e_wx_get_frag(struct net_device *dev,
447 			       struct iw_request_info *info,
448 			       union iwreq_data *wrqu, char *extra)
449 {
450 	struct r8192_priv *priv = rtllib_priv(dev);
451 
452 	wrqu->frag.value = priv->rtllib->fts;
453 	wrqu->frag.fixed = 0;	/* no auto select */
454 	wrqu->frag.disabled = (wrqu->frag.value == DEFAULT_FRAG_THRESHOLD);
455 
456 	return 0;
457 }
458 
_rtl92e_wx_set_wap(struct net_device * dev,struct iw_request_info * info,union iwreq_data * awrq,char * extra)459 static int _rtl92e_wx_set_wap(struct net_device *dev,
460 			      struct iw_request_info *info,
461 			      union iwreq_data *awrq, char *extra)
462 {
463 	int ret;
464 	struct r8192_priv *priv = rtllib_priv(dev);
465 
466 	if (priv->hw_radio_off)
467 		return 0;
468 
469 	mutex_lock(&priv->wx_mutex);
470 
471 	ret = rtllib_wx_set_wap(priv->rtllib, info, awrq, extra);
472 
473 	mutex_unlock(&priv->wx_mutex);
474 
475 	return ret;
476 }
477 
_rtl92e_wx_get_wap(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)478 static int _rtl92e_wx_get_wap(struct net_device *dev,
479 			      struct iw_request_info *info,
480 			      union iwreq_data *wrqu, char *extra)
481 {
482 	struct r8192_priv *priv = rtllib_priv(dev);
483 
484 	return rtllib_wx_get_wap(priv->rtllib, info, wrqu, extra);
485 }
486 
_rtl92e_wx_get_enc(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * key)487 static int _rtl92e_wx_get_enc(struct net_device *dev,
488 			      struct iw_request_info *info,
489 			      union iwreq_data *wrqu, char *key)
490 {
491 	struct r8192_priv *priv = rtllib_priv(dev);
492 
493 	return rtllib_wx_get_encode(priv->rtllib, info, wrqu, key);
494 }
495 
_rtl92e_wx_set_enc(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * key)496 static int _rtl92e_wx_set_enc(struct net_device *dev,
497 			      struct iw_request_info *info,
498 			      union iwreq_data *wrqu, char *key)
499 {
500 	struct r8192_priv *priv = rtllib_priv(dev);
501 	int ret;
502 
503 	struct rtllib_device *ieee = priv->rtllib;
504 	u32 hwkey[4] = {0, 0, 0, 0};
505 	u8 mask = 0xff;
506 	u32 key_idx = 0;
507 	u8 zero_addr[4][6] = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
508 			     {0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
509 			     {0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
510 			     {0x00, 0x00, 0x00, 0x00, 0x00, 0x03} };
511 	int i;
512 
513 	if (priv->hw_radio_off)
514 		return 0;
515 
516 	if (!priv->up)
517 		return -ENETDOWN;
518 
519 	priv->rtllib->wx_set_enc = 1;
520 	mutex_lock(&priv->rtllib->ips_mutex);
521 	rtl92e_ips_leave(dev);
522 	mutex_unlock(&priv->rtllib->ips_mutex);
523 	mutex_lock(&priv->wx_mutex);
524 
525 	ret = rtllib_wx_set_encode(priv->rtllib, info, wrqu, key);
526 	mutex_unlock(&priv->wx_mutex);
527 
528 	if (wrqu->encoding.flags & IW_ENCODE_DISABLED) {
529 		ieee->pairwise_key_type = KEY_TYPE_NA;
530 		ieee->group_key_type = KEY_TYPE_NA;
531 		rtl92e_cam_reset(dev);
532 		memset(priv->rtllib->swcamtable, 0,
533 		       sizeof(struct sw_cam_table) * 32);
534 		goto end_hw_sec;
535 	}
536 	if (wrqu->encoding.length != 0) {
537 		for (i = 0; i < 4; i++) {
538 			hwkey[i] |=  key[4 * i + 0] & mask;
539 			if (i == 1 && (4 * i + 1) == wrqu->encoding.length)
540 				mask = 0x00;
541 			if (i == 3 && (4 * i + 1) == wrqu->encoding.length)
542 				mask = 0x00;
543 			hwkey[i] |= (key[4 * i + 1] & mask) << 8;
544 			hwkey[i] |= (key[4 * i + 2] & mask) << 16;
545 			hwkey[i] |= (key[4 * i + 3] & mask) << 24;
546 		}
547 
548 		switch (wrqu->encoding.flags & IW_ENCODE_INDEX) {
549 		case 0:
550 			key_idx = ieee->crypt_info.tx_keyidx;
551 			break;
552 		case 1:
553 			key_idx = 0;
554 			break;
555 		case 2:
556 			key_idx = 1;
557 			break;
558 		case 3:
559 			key_idx = 2;
560 			break;
561 		case 4:
562 			key_idx	= 3;
563 			break;
564 		default:
565 			break;
566 		}
567 		if (wrqu->encoding.length == 0x5) {
568 			ieee->pairwise_key_type = KEY_TYPE_WEP40;
569 			rtl92e_enable_hw_security_config(dev);
570 		}
571 
572 		else if (wrqu->encoding.length == 0xd) {
573 			ieee->pairwise_key_type = KEY_TYPE_WEP104;
574 			rtl92e_enable_hw_security_config(dev);
575 			rtl92e_set_key(dev, key_idx, key_idx, KEY_TYPE_WEP104,
576 				       zero_addr[key_idx], 0, hwkey);
577 			rtl92e_set_swcam(dev, key_idx, key_idx, KEY_TYPE_WEP104,
578 					 zero_addr[key_idx], hwkey);
579 		} else {
580 			netdev_info(dev,
581 				    "wrong type in WEP, not WEP40 and WEP104\n");
582 		}
583 	}
584 
585 end_hw_sec:
586 	priv->rtllib->wx_set_enc = 0;
587 	return ret;
588 }
589 
590 #define R8192_MAX_RETRY 255
_rtl92e_wx_set_retry(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)591 static int _rtl92e_wx_set_retry(struct net_device *dev,
592 				struct iw_request_info *info,
593 				union iwreq_data *wrqu, char *extra)
594 {
595 	struct r8192_priv *priv = rtllib_priv(dev);
596 	int err = 0;
597 
598 	if (priv->hw_radio_off)
599 		return 0;
600 
601 	mutex_lock(&priv->wx_mutex);
602 
603 	if (wrqu->retry.flags & IW_RETRY_LIFETIME ||
604 	    wrqu->retry.disabled) {
605 		err = -EINVAL;
606 		goto exit;
607 	}
608 	if (!(wrqu->retry.flags & IW_RETRY_LIMIT)) {
609 		err = -EINVAL;
610 		goto exit;
611 	}
612 
613 	if (wrqu->retry.value > R8192_MAX_RETRY) {
614 		err = -EINVAL;
615 		goto exit;
616 	}
617 	if (wrqu->retry.flags & IW_RETRY_MAX)
618 		priv->retry_rts = wrqu->retry.value;
619 	else
620 		priv->retry_data = wrqu->retry.value;
621 
622 	rtl92e_commit(dev);
623 exit:
624 	mutex_unlock(&priv->wx_mutex);
625 
626 	return err;
627 }
628 
_rtl92e_wx_get_retry(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)629 static int _rtl92e_wx_get_retry(struct net_device *dev,
630 				struct iw_request_info *info,
631 				union iwreq_data *wrqu, char *extra)
632 {
633 	struct r8192_priv *priv = rtllib_priv(dev);
634 
635 	wrqu->retry.disabled = 0; /* can't be disabled */
636 
637 	if ((wrqu->retry.flags & IW_RETRY_TYPE) ==
638 	    IW_RETRY_LIFETIME)
639 		return -EINVAL;
640 
641 	if (wrqu->retry.flags & IW_RETRY_MAX) {
642 		wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
643 		wrqu->retry.value = priv->retry_rts;
644 	} else {
645 		wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
646 		wrqu->retry.value = priv->retry_data;
647 	}
648 	return 0;
649 }
650 
_rtl92e_wx_set_encode_ext(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)651 static int _rtl92e_wx_set_encode_ext(struct net_device *dev,
652 				     struct iw_request_info *info,
653 				     union iwreq_data *wrqu, char *extra)
654 {
655 	int ret = 0;
656 	struct r8192_priv *priv = rtllib_priv(dev);
657 	struct rtllib_device *ieee = priv->rtllib;
658 
659 	if (priv->hw_radio_off)
660 		return 0;
661 
662 	mutex_lock(&priv->wx_mutex);
663 
664 	priv->rtllib->wx_set_enc = 1;
665 	mutex_lock(&priv->rtllib->ips_mutex);
666 	rtl92e_ips_leave(dev);
667 	mutex_unlock(&priv->rtllib->ips_mutex);
668 
669 	ret = rtllib_wx_set_encode_ext(ieee, info, wrqu, extra);
670 	{
671 		const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
672 		const u8 zero[ETH_ALEN] = {0};
673 		u32 key[4] = {0};
674 		struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
675 		struct iw_point *encoding = &wrqu->encoding;
676 		u8 idx = 0, alg = 0, group = 0;
677 
678 		if ((encoding->flags & IW_ENCODE_DISABLED) ||
679 		    ext->alg == IW_ENCODE_ALG_NONE) {
680 			ieee->pairwise_key_type = KEY_TYPE_NA;
681 			ieee->group_key_type = KEY_TYPE_NA;
682 			rtl92e_cam_reset(dev);
683 			memset(priv->rtllib->swcamtable, 0,
684 			       sizeof(struct sw_cam_table) * 32);
685 			goto end_hw_sec;
686 		}
687 		alg = (ext->alg == IW_ENCODE_ALG_CCMP) ? KEY_TYPE_CCMP :
688 		      ext->alg;
689 		idx = encoding->flags & IW_ENCODE_INDEX;
690 		if (idx)
691 			idx--;
692 		group = ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY;
693 
694 		if ((!group) || (alg ==  KEY_TYPE_WEP40)) {
695 			if ((ext->key_len == 13) && (alg == KEY_TYPE_WEP40))
696 				alg = KEY_TYPE_WEP104;
697 			ieee->pairwise_key_type = alg;
698 			rtl92e_enable_hw_security_config(dev);
699 		}
700 		memcpy((u8 *)key, ext->key, 16);
701 
702 		if ((alg & KEY_TYPE_WEP40) && (ieee->auth_mode != 2)) {
703 			if (ext->key_len == 13)
704 				ieee->pairwise_key_type = alg = KEY_TYPE_WEP104;
705 			rtl92e_set_key(dev, idx, idx, alg, zero, 0, key);
706 			rtl92e_set_swcam(dev, idx, idx, alg, zero, key);
707 		} else if (group) {
708 			ieee->group_key_type = alg;
709 			rtl92e_set_key(dev, idx, idx, alg, broadcast_addr, 0,
710 				       key);
711 			rtl92e_set_swcam(dev, idx, idx, alg, broadcast_addr, key);
712 		} else {
713 			if ((ieee->pairwise_key_type == KEY_TYPE_CCMP) &&
714 			    ieee->ht_info->current_ht_support)
715 				rtl92e_writeb(dev, 0x173, 1);
716 			rtl92e_set_key(dev, 4, idx, alg,
717 				       (u8 *)ieee->ap_mac_addr, 0, key);
718 			rtl92e_set_swcam(dev, 4, idx, alg, (u8 *)ieee->ap_mac_addr, key);
719 		}
720 	}
721 
722 end_hw_sec:
723 	priv->rtllib->wx_set_enc = 0;
724 	mutex_unlock(&priv->wx_mutex);
725 	return ret;
726 }
727 
_rtl92e_wx_set_auth(struct net_device * dev,struct iw_request_info * info,union iwreq_data * data,char * extra)728 static int _rtl92e_wx_set_auth(struct net_device *dev,
729 			       struct iw_request_info *info,
730 			       union iwreq_data *data, char *extra)
731 {
732 	int ret = 0;
733 
734 	struct r8192_priv *priv = rtllib_priv(dev);
735 
736 	if (priv->hw_radio_off)
737 		return 0;
738 
739 	mutex_lock(&priv->wx_mutex);
740 	ret = rtllib_wx_set_auth(priv->rtllib, info, &data->param, extra);
741 	mutex_unlock(&priv->wx_mutex);
742 	return ret;
743 }
744 
_rtl92e_wx_set_mlme(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)745 static int _rtl92e_wx_set_mlme(struct net_device *dev,
746 			       struct iw_request_info *info,
747 			       union iwreq_data *wrqu, char *extra)
748 {
749 	int ret = 0;
750 
751 	struct r8192_priv *priv = rtllib_priv(dev);
752 
753 	if (priv->hw_radio_off)
754 		return 0;
755 
756 	mutex_lock(&priv->wx_mutex);
757 	ret = rtllib_wx_set_mlme(priv->rtllib, info, wrqu, extra);
758 	mutex_unlock(&priv->wx_mutex);
759 	return ret;
760 }
761 
_rtl92e_wx_set_gen_ie(struct net_device * dev,struct iw_request_info * info,union iwreq_data * data,char * extra)762 static int _rtl92e_wx_set_gen_ie(struct net_device *dev,
763 				 struct iw_request_info *info,
764 				 union iwreq_data *data, char *extra)
765 {
766 	int ret = 0;
767 
768 	struct r8192_priv *priv = rtllib_priv(dev);
769 
770 	if (priv->hw_radio_off)
771 		return 0;
772 
773 	mutex_lock(&priv->wx_mutex);
774 	ret = rtllib_wx_set_gen_ie(priv->rtllib, extra, data->data.length);
775 	mutex_unlock(&priv->wx_mutex);
776 	return ret;
777 }
778 
_rtl92e_wx_get_gen_ie(struct net_device * dev,struct iw_request_info * info,union iwreq_data * data,char * extra)779 static int _rtl92e_wx_get_gen_ie(struct net_device *dev,
780 				 struct iw_request_info *info,
781 				 union iwreq_data *data, char *extra)
782 {
783 	int ret = 0;
784 	struct r8192_priv *priv = rtllib_priv(dev);
785 	struct rtllib_device *ieee = priv->rtllib;
786 
787 	if (ieee->wpa_ie_len == 0 || !ieee->wpa_ie) {
788 		data->data.length = 0;
789 		return 0;
790 	}
791 
792 	if (data->data.length < ieee->wpa_ie_len)
793 		return -E2BIG;
794 
795 	data->data.length = ieee->wpa_ie_len;
796 	memcpy(extra, ieee->wpa_ie, ieee->wpa_ie_len);
797 	return ret;
798 }
799 
800 #define IW_IOCTL(x) ((x) - SIOCSIWCOMMIT)
801 static iw_handler r8192_wx_handlers[] = {
802 	[IW_IOCTL(SIOCGIWNAME)] = _rtl92e_wx_get_name,
803 	[IW_IOCTL(SIOCSIWFREQ)] = _rtl92e_wx_set_freq,
804 	[IW_IOCTL(SIOCGIWFREQ)] = _rtl92e_wx_get_freq,
805 	[IW_IOCTL(SIOCSIWMODE)] = _rtl92e_wx_set_mode,
806 	[IW_IOCTL(SIOCGIWMODE)] = _rtl92e_wx_get_mode,
807 	[IW_IOCTL(SIOCGIWRANGE)] = _rtl92e_wx_get_range,
808 	[IW_IOCTL(SIOCSIWAP)] = _rtl92e_wx_set_wap,
809 	[IW_IOCTL(SIOCGIWAP)] = _rtl92e_wx_get_wap,
810 	[IW_IOCTL(SIOCSIWSCAN)] = _rtl92e_wx_set_scan,
811 	[IW_IOCTL(SIOCGIWSCAN)] = _rtl92e_wx_get_scan,
812 	[IW_IOCTL(SIOCSIWESSID)] = _rtl92e_wx_set_essid,
813 	[IW_IOCTL(SIOCGIWESSID)] = _rtl92e_wx_get_essid,
814 	[IW_IOCTL(SIOCSIWNICKN)] = _rtl92e_wx_set_nick,
815 	[IW_IOCTL(SIOCGIWNICKN)] = _rtl92e_wx_get_nick,
816 	[IW_IOCTL(SIOCSIWRATE)] = _rtl92e_wx_set_rate,
817 	[IW_IOCTL(SIOCGIWRATE)] = _rtl92e_wx_get_rate,
818 	[IW_IOCTL(SIOCSIWRTS)] = _rtl92e_wx_set_rts,
819 	[IW_IOCTL(SIOCGIWRTS)] = _rtl92e_wx_get_rts,
820 	[IW_IOCTL(SIOCSIWFRAG)] = _rtl92e_wx_set_frag,
821 	[IW_IOCTL(SIOCGIWFRAG)] = _rtl92e_wx_get_frag,
822 	[IW_IOCTL(SIOCSIWRETRY)] = _rtl92e_wx_set_retry,
823 	[IW_IOCTL(SIOCGIWRETRY)] = _rtl92e_wx_get_retry,
824 	[IW_IOCTL(SIOCSIWENCODE)] = _rtl92e_wx_set_enc,
825 	[IW_IOCTL(SIOCGIWENCODE)] = _rtl92e_wx_get_enc,
826 	[IW_IOCTL(SIOCSIWPOWER)] = _rtl92e_wx_set_power,
827 	[IW_IOCTL(SIOCGIWPOWER)] = _rtl92e_wx_get_power,
828 	[IW_IOCTL(SIOCSIWGENIE)] = _rtl92e_wx_set_gen_ie,
829 	[IW_IOCTL(SIOCGIWGENIE)] = _rtl92e_wx_get_gen_ie,
830 	[IW_IOCTL(SIOCSIWMLME)] = _rtl92e_wx_set_mlme,
831 	[IW_IOCTL(SIOCSIWAUTH)] = _rtl92e_wx_set_auth,
832 	[IW_IOCTL(SIOCSIWENCODEEXT)] = _rtl92e_wx_set_encode_ext,
833 };
834 
_rtl92e_get_wireless_stats(struct net_device * dev)835 static struct iw_statistics *_rtl92e_get_wireless_stats(struct net_device *dev)
836 {
837 	struct r8192_priv *priv = rtllib_priv(dev);
838 	struct rtllib_device *ieee = priv->rtllib;
839 	struct iw_statistics *wstats = &priv->wstats;
840 	int tmp_level = 0;
841 	int tmp_qual = 0;
842 	int tmp_noise = 0;
843 
844 	if (ieee->link_state < MAC80211_LINKED) {
845 		wstats->qual.qual = 10;
846 		wstats->qual.level = 0;
847 		wstats->qual.noise = 0x100 - 100;	/* -100 dBm */
848 		wstats->qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
849 		return wstats;
850 	}
851 
852 	tmp_level = (&ieee->current_network)->stats.rssi;
853 	tmp_qual = (&ieee->current_network)->stats.signal;
854 	tmp_noise = (&ieee->current_network)->stats.noise;
855 
856 	wstats->qual.level = tmp_level;
857 	wstats->qual.qual = tmp_qual;
858 	wstats->qual.noise = tmp_noise;
859 	wstats->qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
860 	return wstats;
861 }
862 
863 const struct iw_handler_def r8192_wx_handlers_def = {
864 	.standard = r8192_wx_handlers,
865 	.num_standard = ARRAY_SIZE(r8192_wx_handlers),
866 	.get_wireless_stats = _rtl92e_get_wireless_stats,
867 };
868