Merge tag 'driver-core-6.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / staging / rtl8192e / rtl8192e / rtl_wx.c
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
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
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
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
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
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
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
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
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
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
161 struct  iw_range_with_scan_capa {
162         /* Informative stuff (to choose between different interface) */
163         __u32      throughput;     /* To give an idea... */
164         /* In theory this value should be the maximum benchmarked
165          * TCP/IP throughput, because with most of these devices the
166          * bit rate is meaningless (overhead an co) to estimate how
167          * fast the connection will go and pick the fastest one.
168          * I suggest people to play with Netperf or any benchmark...
169          */
170
171         /* NWID (or domain id) */
172         __u32      min_nwid;    /* Minimal NWID we are able to set */
173         __u32      max_nwid;    /* Maximal NWID we are able to set */
174
175         /* Old Frequency (backward compat - moved lower ) */
176         __u16      old_num_channels;
177         __u8        old_num_frequency;
178
179         /* Scan capabilities */
180         __u8        scan_capa;
181 };
182
183 static int _rtl92e_wx_get_range(struct net_device *dev,
184                                 struct iw_request_info *info,
185                                 union iwreq_data *wrqu, char *extra)
186 {
187         struct iw_range *range = (struct iw_range *)extra;
188         struct r8192_priv *priv = rtllib_priv(dev);
189         u16 val;
190         int i;
191
192         wrqu->data.length = sizeof(*range);
193         memset(range, 0, sizeof(*range));
194
195         /* ~130 Mb/s real (802.11n) */
196         range->throughput = 130 * 1000 * 1000;
197
198         range->max_qual.qual = 100;
199         range->max_qual.level = 0;
200         range->max_qual.noise = 0;
201         range->max_qual.updated = 7; /* Updated all three */
202
203         range->avg_qual.qual = 70; /* > 8% missed beacons is 'bad' */
204         range->avg_qual.level = 0;
205         range->avg_qual.noise = 0;
206         range->avg_qual.updated = 7; /* Updated all three */
207
208         range->num_bitrates = min(RATE_COUNT, IW_MAX_BITRATES);
209
210         for (i = 0; i < range->num_bitrates; i++)
211                 range->bitrate[i] = rtl8192_rates[i];
212
213         range->max_rts = DEFAULT_RTS_THRESHOLD;
214         range->min_frag = MIN_FRAG_THRESHOLD;
215         range->max_frag = MAX_FRAG_THRESHOLD;
216
217         range->min_pmp = 0;
218         range->max_pmp = 5000000;
219         range->min_pmt = 0;
220         range->max_pmt = 65535 * 1000;
221         range->pmp_flags = IW_POWER_PERIOD;
222         range->pmt_flags = IW_POWER_TIMEOUT;
223         range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_ALL_R;
224         range->we_version_compiled = WIRELESS_EXT;
225         range->we_version_source = 18;
226
227         for (i = 0, val = 0; i < 14; i++) {
228                 if ((priv->rtllib->active_channel_map)[i + 1]) {
229                         s32 freq_khz;
230
231                         range->freq[val].i = i + 1;
232                         freq_khz = ieee80211_channel_to_freq_khz(i + 1, NL80211_BAND_2GHZ);
233                         range->freq[val].m = freq_khz * 100;
234                         range->freq[val].e = 1;
235                         val++;
236                 }
237
238                 if (val == IW_MAX_FREQUENCIES)
239                         break;
240         }
241         range->num_frequency = val;
242         range->num_channels = val;
243         range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
244                           IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
245         range->scan_capa = IW_SCAN_CAPA_ESSID | IW_SCAN_CAPA_TYPE;
246
247         /* Event capability (kernel + driver) */
248
249         return 0;
250 }
251
252 static int _rtl92e_wx_set_scan(struct net_device *dev,
253                                struct iw_request_info *a,
254                                union iwreq_data *wrqu, char *b)
255 {
256         struct r8192_priv *priv = rtllib_priv(dev);
257         struct rtllib_device *ieee = priv->rtllib;
258         enum rt_rf_power_state rt_state;
259         int ret;
260
261         if (!(ieee->softmac_features & IEEE_SOFTMAC_SCAN)) {
262                 if ((ieee->link_state >= RTLLIB_ASSOCIATING) &&
263                     (ieee->link_state <= RTLLIB_ASSOCIATING_AUTHENTICATED))
264                         return 0;
265                 if ((priv->rtllib->link_state == MAC80211_LINKED) &&
266                     (priv->rtllib->CntAfterLink < 2))
267                         return 0;
268         }
269
270         if (priv->hw_radio_off) {
271                 netdev_info(dev, "================>%s(): hwradio off\n",
272                             __func__);
273                 return 0;
274         }
275         rt_state = priv->rtllib->rf_power_state;
276         if (!priv->up)
277                 return -ENETDOWN;
278         if (priv->rtllib->link_detect_info.bBusyTraffic)
279                 return -EAGAIN;
280
281         if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
282                 struct iw_scan_req *req = (struct iw_scan_req *)b;
283
284                 if (req->essid_len) {
285                         int len = min_t(int, req->essid_len, IW_ESSID_MAX_SIZE);
286
287                         ieee->current_network.ssid_len = len;
288                         memcpy(ieee->current_network.ssid, req->essid, len);
289                 }
290         }
291
292         mutex_lock(&priv->wx_mutex);
293
294         priv->rtllib->FirstIe_InScan = true;
295
296         if (priv->rtllib->link_state != MAC80211_LINKED) {
297                 if (rt_state == rf_off) {
298                         if (priv->rtllib->rf_off_reason >
299                             RF_CHANGE_BY_IPS) {
300                                 netdev_warn(dev, "%s(): RF is OFF.\n",
301                                             __func__);
302                                 mutex_unlock(&priv->wx_mutex);
303                                 return -1;
304                         }
305                         mutex_lock(&priv->rtllib->ips_mutex);
306                         rtl92e_ips_leave(dev);
307                         mutex_unlock(&priv->rtllib->ips_mutex);
308                 }
309                 rtllib_stop_scan(priv->rtllib);
310                 if (priv->rtllib->rf_power_state != rf_off) {
311                         priv->rtllib->actscanning = true;
312
313                         ieee->ScanOperationBackupHandler(ieee->dev, SCAN_OPT_BACKUP);
314
315                         rtllib_start_scan_syncro(priv->rtllib);
316
317                         ieee->ScanOperationBackupHandler(ieee->dev, SCAN_OPT_RESTORE);
318                 }
319                 ret = 0;
320         } else {
321                 priv->rtllib->actscanning = true;
322                 ret = rtllib_wx_set_scan(priv->rtllib, a, wrqu, b);
323         }
324
325         mutex_unlock(&priv->wx_mutex);
326         return ret;
327 }
328
329 static int _rtl92e_wx_get_scan(struct net_device *dev,
330                                struct iw_request_info *a,
331                                union iwreq_data *wrqu, char *b)
332 {
333         int ret;
334         struct r8192_priv *priv = rtllib_priv(dev);
335
336         if (!priv->up)
337                 return -ENETDOWN;
338
339         if (priv->hw_radio_off)
340                 return 0;
341
342         mutex_lock(&priv->wx_mutex);
343
344         ret = rtllib_wx_get_scan(priv->rtllib, a, wrqu, b);
345
346         mutex_unlock(&priv->wx_mutex);
347
348         return ret;
349 }
350
351 static int _rtl92e_wx_set_essid(struct net_device *dev,
352                                 struct iw_request_info *a,
353                                 union iwreq_data *wrqu, char *b)
354 {
355         struct r8192_priv *priv = rtllib_priv(dev);
356         int ret;
357
358         if (priv->hw_radio_off) {
359                 netdev_info(dev,
360                             "=========>%s():hw radio off,or Rf state is rf_off, return\n",
361                             __func__);
362                 return 0;
363         }
364         mutex_lock(&priv->wx_mutex);
365         ret = rtllib_wx_set_essid(priv->rtllib, a, wrqu, b);
366
367         mutex_unlock(&priv->wx_mutex);
368
369         return ret;
370 }
371
372 static int _rtl92e_wx_get_essid(struct net_device *dev,
373                                 struct iw_request_info *a,
374                                 union iwreq_data *wrqu, char *b)
375 {
376         int ret;
377         struct r8192_priv *priv = rtllib_priv(dev);
378
379         mutex_lock(&priv->wx_mutex);
380
381         ret = rtllib_wx_get_essid(priv->rtllib, a, wrqu, b);
382
383         mutex_unlock(&priv->wx_mutex);
384
385         return ret;
386 }
387
388 static int _rtl92e_wx_set_nick(struct net_device *dev,
389                                struct iw_request_info *info,
390                                union iwreq_data *wrqu, char *extra)
391 {
392         struct r8192_priv *priv = rtllib_priv(dev);
393
394         if (wrqu->data.length > IW_ESSID_MAX_SIZE)
395                 return -E2BIG;
396         mutex_lock(&priv->wx_mutex);
397         wrqu->data.length = min_t(size_t, wrqu->data.length,
398                                   sizeof(priv->nick));
399         memset(priv->nick, 0, sizeof(priv->nick));
400         memcpy(priv->nick, extra, wrqu->data.length);
401         mutex_unlock(&priv->wx_mutex);
402         return 0;
403 }
404
405 static int _rtl92e_wx_get_nick(struct net_device *dev,
406                                struct iw_request_info *info,
407                                union iwreq_data *wrqu, char *extra)
408 {
409         struct r8192_priv *priv = rtllib_priv(dev);
410
411         mutex_lock(&priv->wx_mutex);
412         wrqu->data.length = strlen(priv->nick);
413         memcpy(extra, priv->nick, wrqu->data.length);
414         wrqu->data.flags = 1;   /* active */
415         mutex_unlock(&priv->wx_mutex);
416         return 0;
417 }
418
419 static int _rtl92e_wx_set_freq(struct net_device *dev,
420                                struct iw_request_info *a,
421                                union iwreq_data *wrqu, char *b)
422 {
423         int ret;
424         struct r8192_priv *priv = rtllib_priv(dev);
425
426         if (priv->hw_radio_off)
427                 return 0;
428
429         mutex_lock(&priv->wx_mutex);
430
431         ret = rtllib_wx_set_freq(priv->rtllib, a, wrqu, b);
432
433         mutex_unlock(&priv->wx_mutex);
434         return ret;
435 }
436
437 static int _rtl92e_wx_get_name(struct net_device *dev,
438                                struct iw_request_info *info,
439                                union iwreq_data *wrqu, char *extra)
440 {
441         struct r8192_priv *priv = rtllib_priv(dev);
442
443         return rtllib_wx_get_name(priv->rtllib, info, wrqu, extra);
444 }
445
446 static int _rtl92e_wx_set_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         if (priv->hw_radio_off)
453                 return 0;
454
455         if (wrqu->frag.disabled) {
456                 priv->rtllib->fts = DEFAULT_FRAG_THRESHOLD;
457         } else {
458                 if (wrqu->frag.value < MIN_FRAG_THRESHOLD ||
459                     wrqu->frag.value > MAX_FRAG_THRESHOLD)
460                         return -EINVAL;
461
462                 priv->rtllib->fts = wrqu->frag.value & ~0x1;
463         }
464
465         return 0;
466 }
467
468 static int _rtl92e_wx_get_frag(struct net_device *dev,
469                                struct iw_request_info *info,
470                                union iwreq_data *wrqu, char *extra)
471 {
472         struct r8192_priv *priv = rtllib_priv(dev);
473
474         wrqu->frag.value = priv->rtllib->fts;
475         wrqu->frag.fixed = 0;   /* no auto select */
476         wrqu->frag.disabled = (wrqu->frag.value == DEFAULT_FRAG_THRESHOLD);
477
478         return 0;
479 }
480
481 static int _rtl92e_wx_set_wap(struct net_device *dev,
482                               struct iw_request_info *info,
483                               union iwreq_data *awrq, char *extra)
484 {
485         int ret;
486         struct r8192_priv *priv = rtllib_priv(dev);
487
488         if (priv->hw_radio_off)
489                 return 0;
490
491         mutex_lock(&priv->wx_mutex);
492
493         ret = rtllib_wx_set_wap(priv->rtllib, info, awrq, extra);
494
495         mutex_unlock(&priv->wx_mutex);
496
497         return ret;
498 }
499
500 static int _rtl92e_wx_get_wap(struct net_device *dev,
501                               struct iw_request_info *info,
502                               union iwreq_data *wrqu, char *extra)
503 {
504         struct r8192_priv *priv = rtllib_priv(dev);
505
506         return rtllib_wx_get_wap(priv->rtllib, info, wrqu, extra);
507 }
508
509 static int _rtl92e_wx_get_enc(struct net_device *dev,
510                               struct iw_request_info *info,
511                               union iwreq_data *wrqu, char *key)
512 {
513         struct r8192_priv *priv = rtllib_priv(dev);
514
515         return rtllib_wx_get_encode(priv->rtllib, info, wrqu, key);
516 }
517
518 static int _rtl92e_wx_set_enc(struct net_device *dev,
519                               struct iw_request_info *info,
520                               union iwreq_data *wrqu, char *key)
521 {
522         struct r8192_priv *priv = rtllib_priv(dev);
523         int ret;
524
525         struct rtllib_device *ieee = priv->rtllib;
526         u32 hwkey[4] = {0, 0, 0, 0};
527         u8 mask = 0xff;
528         u32 key_idx = 0;
529         u8 zero_addr[4][6] = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
530                              {0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
531                              {0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
532                              {0x00, 0x00, 0x00, 0x00, 0x00, 0x03} };
533         int i;
534
535         if (priv->hw_radio_off)
536                 return 0;
537
538         if (!priv->up)
539                 return -ENETDOWN;
540
541         priv->rtllib->wx_set_enc = 1;
542         mutex_lock(&priv->rtllib->ips_mutex);
543         rtl92e_ips_leave(dev);
544         mutex_unlock(&priv->rtllib->ips_mutex);
545         mutex_lock(&priv->wx_mutex);
546
547         ret = rtllib_wx_set_encode(priv->rtllib, info, wrqu, key);
548         mutex_unlock(&priv->wx_mutex);
549
550         if (wrqu->encoding.flags & IW_ENCODE_DISABLED) {
551                 ieee->pairwise_key_type = ieee->group_key_type = KEY_TYPE_NA;
552                 rtl92e_cam_reset(dev);
553                 memset(priv->rtllib->swcamtable, 0,
554                        sizeof(struct sw_cam_table) * 32);
555                 goto end_hw_sec;
556         }
557         if (wrqu->encoding.length != 0) {
558                 for (i = 0; i < 4; i++) {
559                         hwkey[i] |=  key[4 * i + 0] & mask;
560                         if (i == 1 && (4 * i + 1) == wrqu->encoding.length)
561                                 mask = 0x00;
562                         if (i == 3 && (4 * i + 1) == wrqu->encoding.length)
563                                 mask = 0x00;
564                         hwkey[i] |= (key[4 * i + 1] & mask) << 8;
565                         hwkey[i] |= (key[4 * i + 2] & mask) << 16;
566                         hwkey[i] |= (key[4 * i + 3] & mask) << 24;
567                 }
568
569                 switch (wrqu->encoding.flags & IW_ENCODE_INDEX) {
570                 case 0:
571                         key_idx = ieee->crypt_info.tx_keyidx;
572                         break;
573                 case 1:
574                         key_idx = 0;
575                         break;
576                 case 2:
577                         key_idx = 1;
578                         break;
579                 case 3:
580                         key_idx = 2;
581                         break;
582                 case 4:
583                         key_idx = 3;
584                         break;
585                 default:
586                         break;
587                 }
588                 if (wrqu->encoding.length == 0x5) {
589                         ieee->pairwise_key_type = KEY_TYPE_WEP40;
590                         rtl92e_enable_hw_security_config(dev);
591                 }
592
593                 else if (wrqu->encoding.length == 0xd) {
594                         ieee->pairwise_key_type = KEY_TYPE_WEP104;
595                         rtl92e_enable_hw_security_config(dev);
596                         rtl92e_set_key(dev, key_idx, key_idx, KEY_TYPE_WEP104,
597                                        zero_addr[key_idx], 0, hwkey);
598                         rtl92e_set_swcam(dev, key_idx, key_idx, KEY_TYPE_WEP104,
599                                          zero_addr[key_idx], hwkey);
600                 } else {
601                         netdev_info(dev,
602                                     "wrong type in WEP, not WEP40 and WEP104\n");
603                 }
604         }
605
606 end_hw_sec:
607         priv->rtllib->wx_set_enc = 0;
608         return ret;
609 }
610
611 #define R8192_MAX_RETRY 255
612 static int _rtl92e_wx_set_retry(struct net_device *dev,
613                                 struct iw_request_info *info,
614                                 union iwreq_data *wrqu, char *extra)
615 {
616         struct r8192_priv *priv = rtllib_priv(dev);
617         int err = 0;
618
619         if (priv->hw_radio_off)
620                 return 0;
621
622         mutex_lock(&priv->wx_mutex);
623
624         if (wrqu->retry.flags & IW_RETRY_LIFETIME ||
625             wrqu->retry.disabled) {
626                 err = -EINVAL;
627                 goto exit;
628         }
629         if (!(wrqu->retry.flags & IW_RETRY_LIMIT)) {
630                 err = -EINVAL;
631                 goto exit;
632         }
633
634         if (wrqu->retry.value > R8192_MAX_RETRY) {
635                 err = -EINVAL;
636                 goto exit;
637         }
638         if (wrqu->retry.flags & IW_RETRY_MAX)
639                 priv->retry_rts = wrqu->retry.value;
640         else
641                 priv->retry_data = wrqu->retry.value;
642
643         rtl92e_commit(dev);
644 exit:
645         mutex_unlock(&priv->wx_mutex);
646
647         return err;
648 }
649
650 static int _rtl92e_wx_get_retry(struct net_device *dev,
651                                 struct iw_request_info *info,
652                                 union iwreq_data *wrqu, char *extra)
653 {
654         struct r8192_priv *priv = rtllib_priv(dev);
655
656         wrqu->retry.disabled = 0; /* can't be disabled */
657
658         if ((wrqu->retry.flags & IW_RETRY_TYPE) ==
659             IW_RETRY_LIFETIME)
660                 return -EINVAL;
661
662         if (wrqu->retry.flags & IW_RETRY_MAX) {
663                 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
664                 wrqu->retry.value = priv->retry_rts;
665         } else {
666                 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
667                 wrqu->retry.value = priv->retry_data;
668         }
669         return 0;
670 }
671
672 static int _rtl92e_wx_set_encode_ext(struct net_device *dev,
673                                      struct iw_request_info *info,
674                                      union iwreq_data *wrqu, char *extra)
675 {
676         int ret = 0;
677         struct r8192_priv *priv = rtllib_priv(dev);
678         struct rtllib_device *ieee = priv->rtllib;
679
680         if (priv->hw_radio_off)
681                 return 0;
682
683         mutex_lock(&priv->wx_mutex);
684
685         priv->rtllib->wx_set_enc = 1;
686         mutex_lock(&priv->rtllib->ips_mutex);
687         rtl92e_ips_leave(dev);
688         mutex_unlock(&priv->rtllib->ips_mutex);
689
690         ret = rtllib_wx_set_encode_ext(ieee, info, wrqu, extra);
691         {
692                 const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
693                 const u8 zero[ETH_ALEN] = {0};
694                 u32 key[4] = {0};
695                 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
696                 struct iw_point *encoding = &wrqu->encoding;
697                 u8 idx = 0, alg = 0, group = 0;
698
699                 if ((encoding->flags & IW_ENCODE_DISABLED) ||
700                      ext->alg == IW_ENCODE_ALG_NONE) {
701                         ieee->pairwise_key_type = ieee->group_key_type
702                                                 = KEY_TYPE_NA;
703                         rtl92e_cam_reset(dev);
704                         memset(priv->rtllib->swcamtable, 0,
705                                sizeof(struct sw_cam_table) * 32);
706                         goto end_hw_sec;
707                 }
708                 alg = (ext->alg == IW_ENCODE_ALG_CCMP) ? KEY_TYPE_CCMP :
709                       ext->alg;
710                 idx = encoding->flags & IW_ENCODE_INDEX;
711                 if (idx)
712                         idx--;
713                 group = ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY;
714
715                 if ((!group) || (alg ==  KEY_TYPE_WEP40)) {
716                         if ((ext->key_len == 13) && (alg == KEY_TYPE_WEP40))
717                                 alg = KEY_TYPE_WEP104;
718                         ieee->pairwise_key_type = alg;
719                         rtl92e_enable_hw_security_config(dev);
720                 }
721                 memcpy((u8 *)key, ext->key, 16);
722
723                 if ((alg & KEY_TYPE_WEP40) && (ieee->auth_mode != 2)) {
724                         if (ext->key_len == 13)
725                                 ieee->pairwise_key_type = alg = KEY_TYPE_WEP104;
726                         rtl92e_set_key(dev, idx, idx, alg, zero, 0, key);
727                         rtl92e_set_swcam(dev, idx, idx, alg, zero, key);
728                 } else if (group) {
729                         ieee->group_key_type = alg;
730                         rtl92e_set_key(dev, idx, idx, alg, broadcast_addr, 0,
731                                        key);
732                         rtl92e_set_swcam(dev, idx, idx, alg, broadcast_addr, key);
733                 } else {
734                         if ((ieee->pairwise_key_type == KEY_TYPE_CCMP) &&
735                              ieee->ht_info->current_ht_support)
736                                 rtl92e_writeb(dev, 0x173, 1);
737                         rtl92e_set_key(dev, 4, idx, alg,
738                                        (u8 *)ieee->ap_mac_addr, 0, key);
739                         rtl92e_set_swcam(dev, 4, idx, alg, (u8 *)ieee->ap_mac_addr, key);
740                 }
741         }
742
743 end_hw_sec:
744         priv->rtllib->wx_set_enc = 0;
745         mutex_unlock(&priv->wx_mutex);
746         return ret;
747 }
748
749 static int _rtl92e_wx_set_auth(struct net_device *dev,
750                                struct iw_request_info *info,
751                                union iwreq_data *data, char *extra)
752 {
753         int ret = 0;
754
755         struct r8192_priv *priv = rtllib_priv(dev);
756
757         if (priv->hw_radio_off)
758                 return 0;
759
760         mutex_lock(&priv->wx_mutex);
761         ret = rtllib_wx_set_auth(priv->rtllib, info, &data->param, extra);
762         mutex_unlock(&priv->wx_mutex);
763         return ret;
764 }
765
766 static int _rtl92e_wx_set_mlme(struct net_device *dev,
767                                struct iw_request_info *info,
768                                union iwreq_data *wrqu, char *extra)
769 {
770         int ret = 0;
771
772         struct r8192_priv *priv = rtllib_priv(dev);
773
774         if (priv->hw_radio_off)
775                 return 0;
776
777         mutex_lock(&priv->wx_mutex);
778         ret = rtllib_wx_set_mlme(priv->rtllib, info, wrqu, extra);
779         mutex_unlock(&priv->wx_mutex);
780         return ret;
781 }
782
783 static int _rtl92e_wx_set_gen_ie(struct net_device *dev,
784                                  struct iw_request_info *info,
785                                  union iwreq_data *data, char *extra)
786 {
787         int ret = 0;
788
789         struct r8192_priv *priv = rtllib_priv(dev);
790
791         if (priv->hw_radio_off)
792                 return 0;
793
794         mutex_lock(&priv->wx_mutex);
795         ret = rtllib_wx_set_gen_ie(priv->rtllib, extra, data->data.length);
796         mutex_unlock(&priv->wx_mutex);
797         return ret;
798 }
799
800 static int _rtl92e_wx_get_gen_ie(struct net_device *dev,
801                                  struct iw_request_info *info,
802                                  union iwreq_data *data, char *extra)
803 {
804         int ret = 0;
805         struct r8192_priv *priv = rtllib_priv(dev);
806         struct rtllib_device *ieee = priv->rtllib;
807
808         if (ieee->wpa_ie_len == 0 || !ieee->wpa_ie) {
809                 data->data.length = 0;
810                 return 0;
811         }
812
813         if (data->data.length < ieee->wpa_ie_len)
814                 return -E2BIG;
815
816         data->data.length = ieee->wpa_ie_len;
817         memcpy(extra, ieee->wpa_ie, ieee->wpa_ie_len);
818         return ret;
819 }
820
821 #define IW_IOCTL(x) ((x) - SIOCSIWCOMMIT)
822 static iw_handler r8192_wx_handlers[] = {
823         [IW_IOCTL(SIOCGIWNAME)] = _rtl92e_wx_get_name,
824         [IW_IOCTL(SIOCSIWFREQ)] = _rtl92e_wx_set_freq,
825         [IW_IOCTL(SIOCGIWFREQ)] = _rtl92e_wx_get_freq,
826         [IW_IOCTL(SIOCSIWMODE)] = _rtl92e_wx_set_mode,
827         [IW_IOCTL(SIOCGIWMODE)] = _rtl92e_wx_get_mode,
828         [IW_IOCTL(SIOCGIWRANGE)] = _rtl92e_wx_get_range,
829         [IW_IOCTL(SIOCSIWAP)] = _rtl92e_wx_set_wap,
830         [IW_IOCTL(SIOCGIWAP)] = _rtl92e_wx_get_wap,
831         [IW_IOCTL(SIOCSIWSCAN)] = _rtl92e_wx_set_scan,
832         [IW_IOCTL(SIOCGIWSCAN)] = _rtl92e_wx_get_scan,
833         [IW_IOCTL(SIOCSIWESSID)] = _rtl92e_wx_set_essid,
834         [IW_IOCTL(SIOCGIWESSID)] = _rtl92e_wx_get_essid,
835         [IW_IOCTL(SIOCSIWNICKN)] = _rtl92e_wx_set_nick,
836         [IW_IOCTL(SIOCGIWNICKN)] = _rtl92e_wx_get_nick,
837         [IW_IOCTL(SIOCSIWRATE)] = _rtl92e_wx_set_rate,
838         [IW_IOCTL(SIOCGIWRATE)] = _rtl92e_wx_get_rate,
839         [IW_IOCTL(SIOCSIWRTS)] = _rtl92e_wx_set_rts,
840         [IW_IOCTL(SIOCGIWRTS)] = _rtl92e_wx_get_rts,
841         [IW_IOCTL(SIOCSIWFRAG)] = _rtl92e_wx_set_frag,
842         [IW_IOCTL(SIOCGIWFRAG)] = _rtl92e_wx_get_frag,
843         [IW_IOCTL(SIOCSIWRETRY)] = _rtl92e_wx_set_retry,
844         [IW_IOCTL(SIOCGIWRETRY)] = _rtl92e_wx_get_retry,
845         [IW_IOCTL(SIOCSIWENCODE)] = _rtl92e_wx_set_enc,
846         [IW_IOCTL(SIOCGIWENCODE)] = _rtl92e_wx_get_enc,
847         [IW_IOCTL(SIOCSIWPOWER)] = _rtl92e_wx_set_power,
848         [IW_IOCTL(SIOCGIWPOWER)] = _rtl92e_wx_get_power,
849         [IW_IOCTL(SIOCSIWGENIE)] = _rtl92e_wx_set_gen_ie,
850         [IW_IOCTL(SIOCGIWGENIE)] = _rtl92e_wx_get_gen_ie,
851         [IW_IOCTL(SIOCSIWMLME)] = _rtl92e_wx_set_mlme,
852         [IW_IOCTL(SIOCSIWAUTH)] = _rtl92e_wx_set_auth,
853         [IW_IOCTL(SIOCSIWENCODEEXT)] = _rtl92e_wx_set_encode_ext,
854 };
855
856 static struct iw_statistics *_rtl92e_get_wireless_stats(struct net_device *dev)
857 {
858         struct r8192_priv *priv = rtllib_priv(dev);
859         struct rtllib_device *ieee = priv->rtllib;
860         struct iw_statistics *wstats = &priv->wstats;
861         int tmp_level = 0;
862         int tmp_qual = 0;
863         int tmp_noise = 0;
864
865         if (ieee->link_state < MAC80211_LINKED) {
866                 wstats->qual.qual = 10;
867                 wstats->qual.level = 0;
868                 wstats->qual.noise = 0x100 - 100;       /* -100 dBm */
869                 wstats->qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
870                 return wstats;
871         }
872
873         tmp_level = (&ieee->current_network)->stats.rssi;
874         tmp_qual = (&ieee->current_network)->stats.signal;
875         tmp_noise = (&ieee->current_network)->stats.noise;
876
877         wstats->qual.level = tmp_level;
878         wstats->qual.qual = tmp_qual;
879         wstats->qual.noise = tmp_noise;
880         wstats->qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
881         return wstats;
882 }
883
884 const struct iw_handler_def r8192_wx_handlers_def = {
885         .standard = r8192_wx_handlers,
886         .num_standard = ARRAY_SIZE(r8192_wx_handlers),
887         .get_wireless_stats = _rtl92e_get_wireless_stats,
888 };