45eb416bc2ad93cb476774625536a8ab647b0712
[firefly-linux-kernel-4.4.55.git] / drivers / staging / rtl8192u / ieee80211 / ieee80211_softmac_wx.c
1 /* IEEE 802.11 SoftMAC layer
2  * Copyright (c) 2005 Andrea Merello <andrea.merello@gmail.com>
3  *
4  * Mostly extracted from the rtl8180-sa2400 driver for the
5  * in-kernel generic ieee802.11 stack.
6  *
7  * Some pieces of code might be stolen from ipw2100 driver
8  * copyright of who own it's copyright ;-)
9  *
10  * PS wx handler mostly stolen from hostap, copyright who
11  * own it's copyright ;-)
12  *
13  * released under the GPL
14  */
15
16
17 #include <linux/etherdevice.h>
18
19 #include "ieee80211.h"
20 #include "dot11d.h"
21 /* FIXME: add A freqs */
22
23 const long ieee80211_wlan_frequencies[] = {
24         2412, 2417, 2422, 2427,
25         2432, 2437, 2442, 2447,
26         2452, 2457, 2462, 2467,
27         2472, 2484
28 };
29 EXPORT_SYMBOL(ieee80211_wlan_frequencies);
30
31 int ieee80211_wx_set_freq(struct ieee80211_device *ieee, struct iw_request_info *a,
32                              union iwreq_data *wrqu, char *b)
33 {
34         int ret;
35         struct iw_freq *fwrq = &wrqu->freq;
36
37         down(&ieee->wx_sem);
38
39         if (ieee->iw_mode == IW_MODE_INFRA) {
40                 ret = -EOPNOTSUPP;
41                 goto out;
42         }
43
44         /* if setting by freq convert to channel */
45         if (fwrq->e == 1) {
46                 if ((fwrq->m >= (int) 2.412e8 &&
47                      fwrq->m <= (int) 2.487e8)) {
48                         int f = fwrq->m / 100000;
49                         int c = 0;
50
51                         while ((c < 14) && (f != ieee80211_wlan_frequencies[c]))
52                                 c++;
53
54                         /* hack to fall through */
55                         fwrq->e = 0;
56                         fwrq->m = c + 1;
57                 }
58         }
59
60         if (fwrq->e > 0 || fwrq->m > 14 || fwrq->m < 1) {
61                 ret = -EOPNOTSUPP;
62                 goto out;
63
64         } else { /* Set the channel */
65
66                 if (!(GET_DOT11D_INFO(ieee)->channel_map)[fwrq->m]) {
67                         ret = -EINVAL;
68                         goto out;
69                 }
70                 ieee->current_network.channel = fwrq->m;
71                 ieee->set_chan(ieee->dev, ieee->current_network.channel);
72
73                 if (ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
74                         if (ieee->state == IEEE80211_LINKED) {
75
76                         ieee80211_stop_send_beacons(ieee);
77                         ieee80211_start_send_beacons(ieee);
78                         }
79         }
80
81         ret = 0;
82 out:
83         up(&ieee->wx_sem);
84         return ret;
85 }
86 EXPORT_SYMBOL(ieee80211_wx_set_freq);
87
88 int ieee80211_wx_get_freq(struct ieee80211_device *ieee,
89                              struct iw_request_info *a,
90                              union iwreq_data *wrqu, char *b)
91 {
92         struct iw_freq *fwrq = &wrqu->freq;
93
94         if (ieee->current_network.channel == 0)
95                 return -1;
96         //NM 0.7.0 will not accept channel any more.
97         fwrq->m = ieee80211_wlan_frequencies[ieee->current_network.channel-1] * 100000;
98         fwrq->e = 1;
99 //      fwrq->m = ieee->current_network.channel;
100 //      fwrq->e = 0;
101
102         return 0;
103 }
104 EXPORT_SYMBOL(ieee80211_wx_get_freq);
105
106 int ieee80211_wx_get_wap(struct ieee80211_device *ieee,
107                             struct iw_request_info *info,
108                             union iwreq_data *wrqu, char *extra)
109 {
110         unsigned long flags;
111
112         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
113
114         if (ieee->iw_mode == IW_MODE_MONITOR)
115                 return -1;
116
117         /* We want avoid to give to the user inconsistent infos*/
118         spin_lock_irqsave(&ieee->lock, flags);
119
120         if (ieee->state != IEEE80211_LINKED &&
121                 ieee->state != IEEE80211_LINKED_SCANNING &&
122                 ieee->wap_set == 0)
123
124                 eth_zero_addr(wrqu->ap_addr.sa_data);
125         else
126                 memcpy(wrqu->ap_addr.sa_data,
127                        ieee->current_network.bssid, ETH_ALEN);
128
129         spin_unlock_irqrestore(&ieee->lock, flags);
130
131         return 0;
132 }
133 EXPORT_SYMBOL(ieee80211_wx_get_wap);
134
135 int ieee80211_wx_set_wap(struct ieee80211_device *ieee,
136                          struct iw_request_info *info,
137                          union iwreq_data *awrq,
138                          char *extra)
139 {
140
141         int ret = 0;
142         unsigned long flags;
143
144         short ifup = ieee->proto_started;//dev->flags & IFF_UP;
145         struct sockaddr *temp = (struct sockaddr *)awrq;
146
147         ieee->sync_scan_hurryup = 1;
148
149         down(&ieee->wx_sem);
150         /* use ifconfig hw ether */
151         if (ieee->iw_mode == IW_MODE_MASTER) {
152                 ret = -1;
153                 goto out;
154         }
155
156         if (temp->sa_family != ARPHRD_ETHER) {
157                 ret = -EINVAL;
158                 goto out;
159         }
160
161         if (ifup)
162                 ieee80211_stop_protocol(ieee);
163
164         /* just to avoid to give inconsistent infos in the
165          * get wx method. not really needed otherwise
166          */
167         spin_lock_irqsave(&ieee->lock, flags);
168
169         memcpy(ieee->current_network.bssid, temp->sa_data, ETH_ALEN);
170         ieee->wap_set = !is_zero_ether_addr(temp->sa_data);
171
172         spin_unlock_irqrestore(&ieee->lock, flags);
173
174         if (ifup)
175                 ieee80211_start_protocol(ieee);
176 out:
177         up(&ieee->wx_sem);
178         return ret;
179 }
180 EXPORT_SYMBOL(ieee80211_wx_set_wap);
181
182  int ieee80211_wx_get_essid(struct ieee80211_device *ieee, struct iw_request_info *a,union iwreq_data *wrqu,char *b)
183 {
184         int len, ret = 0;
185         unsigned long flags;
186
187         if (ieee->iw_mode == IW_MODE_MONITOR)
188                 return -1;
189
190         /* We want avoid to give to the user inconsistent infos*/
191         spin_lock_irqsave(&ieee->lock, flags);
192
193         if (ieee->current_network.ssid[0] == '\0' ||
194                 ieee->current_network.ssid_len == 0) {
195                 ret = -1;
196                 goto out;
197         }
198
199         if (ieee->state != IEEE80211_LINKED &&
200                 ieee->state != IEEE80211_LINKED_SCANNING &&
201                 ieee->ssid_set == 0) {
202                 ret = -1;
203                 goto out;
204         }
205         len = ieee->current_network.ssid_len;
206         wrqu->essid.length = len;
207         strncpy(b, ieee->current_network.ssid, len);
208         wrqu->essid.flags = 1;
209
210 out:
211         spin_unlock_irqrestore(&ieee->lock, flags);
212
213         return ret;
214
215 }
216 EXPORT_SYMBOL(ieee80211_wx_get_essid);
217
218 int ieee80211_wx_set_rate(struct ieee80211_device *ieee,
219                              struct iw_request_info *info,
220                              union iwreq_data *wrqu, char *extra)
221 {
222
223         u32 target_rate = wrqu->bitrate.value;
224
225         ieee->rate = target_rate/100000;
226         //FIXME: we might want to limit rate also in management protocols.
227         return 0;
228 }
229 EXPORT_SYMBOL(ieee80211_wx_set_rate);
230
231 int ieee80211_wx_get_rate(struct ieee80211_device *ieee,
232                              struct iw_request_info *info,
233                              union iwreq_data *wrqu, char *extra)
234 {
235         u32 tmp_rate;
236         tmp_rate = TxCountToDataRate(ieee, ieee->softmac_stats.CurrentShowTxate);
237
238         wrqu->bitrate.value = tmp_rate * 500000;
239
240         return 0;
241 }
242 EXPORT_SYMBOL(ieee80211_wx_get_rate);
243
244 int ieee80211_wx_set_rts(struct ieee80211_device *ieee,
245                              struct iw_request_info *info,
246                              union iwreq_data *wrqu, char *extra)
247 {
248         if (wrqu->rts.disabled || !wrqu->rts.fixed)
249                 ieee->rts = DEFAULT_RTS_THRESHOLD;
250         else
251         {
252                 if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
253                                 wrqu->rts.value > MAX_RTS_THRESHOLD)
254                         return -EINVAL;
255                 ieee->rts = wrqu->rts.value;
256         }
257         return 0;
258 }
259 EXPORT_SYMBOL(ieee80211_wx_set_rts);
260
261 int ieee80211_wx_get_rts(struct ieee80211_device *ieee,
262                              struct iw_request_info *info,
263                              union iwreq_data *wrqu, char *extra)
264 {
265         wrqu->rts.value = ieee->rts;
266         wrqu->rts.fixed = 0;    /* no auto select */
267         wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
268         return 0;
269 }
270 EXPORT_SYMBOL(ieee80211_wx_get_rts);
271
272 int ieee80211_wx_set_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
273                              union iwreq_data *wrqu, char *b)
274 {
275
276         ieee->sync_scan_hurryup = 1;
277
278         down(&ieee->wx_sem);
279
280         if (wrqu->mode == ieee->iw_mode)
281                 goto out;
282
283         if (wrqu->mode == IW_MODE_MONITOR) {
284
285                 ieee->dev->type = ARPHRD_IEEE80211;
286         } else {
287                 ieee->dev->type = ARPHRD_ETHER;
288         }
289
290         if (!ieee->proto_started) {
291                 ieee->iw_mode = wrqu->mode;
292         } else {
293                 ieee80211_stop_protocol(ieee);
294                 ieee->iw_mode = wrqu->mode;
295                 ieee80211_start_protocol(ieee);
296         }
297
298 out:
299         up(&ieee->wx_sem);
300         return 0;
301 }
302 EXPORT_SYMBOL(ieee80211_wx_set_mode);
303
304 void ieee80211_wx_sync_scan_wq(struct work_struct *work)
305 {
306         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, wx_sync_scan_wq);
307         short chan;
308         HT_EXTCHNL_OFFSET chan_offset=0;
309         HT_CHANNEL_WIDTH bandwidth=0;
310         int b40M = 0;
311         chan = ieee->current_network.channel;
312         netif_carrier_off(ieee->dev);
313
314         if (ieee->data_hard_stop)
315                 ieee->data_hard_stop(ieee->dev);
316
317         ieee80211_stop_send_beacons(ieee);
318
319         ieee->state = IEEE80211_LINKED_SCANNING;
320         ieee->link_change(ieee->dev);
321         ieee->InitialGainHandler(ieee->dev, IG_Backup);
322         if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT && ieee->pHTInfo->bCurBW40MHz) {
323                 b40M = 1;
324                 chan_offset = ieee->pHTInfo->CurSTAExtChnlOffset;
325                 bandwidth = (HT_CHANNEL_WIDTH)ieee->pHTInfo->bCurBW40MHz;
326                 printk("Scan in 40M, force to 20M first:%d, %d\n", chan_offset, bandwidth);
327                 ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
328                 }
329         ieee80211_start_scan_syncro(ieee);
330         if (b40M) {
331                 printk("Scan in 20M, back to 40M\n");
332                 if (chan_offset == HT_EXTCHNL_OFFSET_UPPER)
333                         ieee->set_chan(ieee->dev, chan + 2);
334                 else if (chan_offset == HT_EXTCHNL_OFFSET_LOWER)
335                         ieee->set_chan(ieee->dev, chan - 2);
336                 else
337                         ieee->set_chan(ieee->dev, chan);
338                 ieee->SetBWModeHandler(ieee->dev, bandwidth, chan_offset);
339         } else {
340                 ieee->set_chan(ieee->dev, chan);
341         }
342
343         ieee->InitialGainHandler(ieee->dev, IG_Restore);
344         ieee->state = IEEE80211_LINKED;
345         ieee->link_change(ieee->dev);
346         // To prevent the immediately calling watch_dog after scan.
347         if (ieee->LinkDetectInfo.NumRecvBcnInPeriod==0||ieee->LinkDetectInfo.NumRecvDataInPeriod==0)
348         {
349                 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
350                 ieee->LinkDetectInfo.NumRecvDataInPeriod= 1;
351         }
352         if (ieee->data_hard_resume)
353                 ieee->data_hard_resume(ieee->dev);
354
355         if (ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
356                 ieee80211_start_send_beacons(ieee);
357
358         netif_carrier_on(ieee->dev);
359         up(&ieee->wx_sem);
360
361 }
362
363 int ieee80211_wx_set_scan(struct ieee80211_device *ieee, struct iw_request_info *a,
364                              union iwreq_data *wrqu, char *b)
365 {
366         int ret = 0;
367
368         down(&ieee->wx_sem);
369
370         if (ieee->iw_mode == IW_MODE_MONITOR || !(ieee->proto_started)) {
371                 ret = -1;
372                 goto out;
373         }
374
375         if (ieee->state == IEEE80211_LINKED) {
376                 queue_work(ieee->wq, &ieee->wx_sync_scan_wq);
377                 /* intentionally forget to up sem */
378                 return 0;
379         }
380
381 out:
382         up(&ieee->wx_sem);
383         return ret;
384 }
385 EXPORT_SYMBOL(ieee80211_wx_set_scan);
386
387 int ieee80211_wx_set_essid(struct ieee80211_device *ieee,
388                               struct iw_request_info *a,
389                               union iwreq_data *wrqu, char *extra)
390 {
391
392         int ret = 0, len;
393         short proto_started;
394         unsigned long flags;
395
396         ieee->sync_scan_hurryup = 1;
397         down(&ieee->wx_sem);
398
399         proto_started = ieee->proto_started;
400
401         if (wrqu->essid.length > IW_ESSID_MAX_SIZE) {
402                 ret= -E2BIG;
403                 goto out;
404         }
405
406         if (ieee->iw_mode == IW_MODE_MONITOR) {
407                 ret= -1;
408                 goto out;
409         }
410
411         if (proto_started)
412                 ieee80211_stop_protocol(ieee);
413
414
415         /* this is just to be sure that the GET wx callback
416          * has consisten infos. not needed otherwise
417          */
418         spin_lock_irqsave(&ieee->lock, flags);
419
420         if (wrqu->essid.flags && wrqu->essid.length) {
421                 //first flush current network.ssid
422                 len = ((wrqu->essid.length-1) < IW_ESSID_MAX_SIZE) ? (wrqu->essid.length-1) : IW_ESSID_MAX_SIZE;
423                 strncpy(ieee->current_network.ssid, extra, len+1);
424                 ieee->current_network.ssid_len = len+1;
425                 ieee->ssid_set = 1;
426         }
427         else {
428                 ieee->ssid_set = 0;
429                 ieee->current_network.ssid[0] = '\0';
430                 ieee->current_network.ssid_len = 0;
431         }
432         spin_unlock_irqrestore(&ieee->lock, flags);
433
434         if (proto_started)
435                 ieee80211_start_protocol(ieee);
436 out:
437         up(&ieee->wx_sem);
438         return ret;
439 }
440 EXPORT_SYMBOL(ieee80211_wx_set_essid);
441
442  int ieee80211_wx_get_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
443                              union iwreq_data *wrqu, char *b)
444 {
445
446         wrqu->mode = ieee->iw_mode;
447         return 0;
448 }
449 EXPORT_SYMBOL(ieee80211_wx_get_mode);
450
451  int ieee80211_wx_set_rawtx(struct ieee80211_device *ieee,
452                                struct iw_request_info *info,
453                                union iwreq_data *wrqu, char *extra)
454 {
455
456         int *parms = (int *)extra;
457         int enable = (parms[0] > 0);
458         short prev = ieee->raw_tx;
459
460         down(&ieee->wx_sem);
461
462         if (enable)
463                 ieee->raw_tx = 1;
464         else
465                 ieee->raw_tx = 0;
466
467         printk(KERN_INFO"raw TX is %s\n",
468               ieee->raw_tx ? "enabled" : "disabled");
469
470         if (ieee->iw_mode == IW_MODE_MONITOR)
471         {
472                 if (prev == 0 && ieee->raw_tx) {
473                         if (ieee->data_hard_resume)
474                                 ieee->data_hard_resume(ieee->dev);
475
476                         netif_carrier_on(ieee->dev);
477                 }
478
479                 if (prev && ieee->raw_tx == 1)
480                         netif_carrier_off(ieee->dev);
481         }
482
483         up(&ieee->wx_sem);
484
485         return 0;
486 }
487 EXPORT_SYMBOL(ieee80211_wx_set_rawtx);
488
489 int ieee80211_wx_get_name(struct ieee80211_device *ieee,
490                              struct iw_request_info *info,
491                              union iwreq_data *wrqu, char *extra)
492 {
493         strlcpy(wrqu->name, "802.11", IFNAMSIZ);
494         if (ieee->modulation & IEEE80211_CCK_MODULATION) {
495                 strlcat(wrqu->name, "b", IFNAMSIZ);
496                 if (ieee->modulation & IEEE80211_OFDM_MODULATION)
497                         strlcat(wrqu->name, "/g", IFNAMSIZ);
498         } else if (ieee->modulation & IEEE80211_OFDM_MODULATION) {
499                 strlcat(wrqu->name, "g", IFNAMSIZ);
500         }
501
502         if (ieee->mode & (IEEE_N_24G | IEEE_N_5G))
503                 strlcat(wrqu->name, "/n", IFNAMSIZ);
504
505         if ((ieee->state == IEEE80211_LINKED) ||
506             (ieee->state == IEEE80211_LINKED_SCANNING))
507                 strlcat(wrqu->name, " linked", IFNAMSIZ);
508         else if (ieee->state != IEEE80211_NOLINK)
509                 strlcat(wrqu->name, " link..", IFNAMSIZ);
510
511         return 0;
512 }
513 EXPORT_SYMBOL(ieee80211_wx_get_name);
514
515 /* this is mostly stolen from hostap */
516 int ieee80211_wx_set_power(struct ieee80211_device *ieee,
517                                  struct iw_request_info *info,
518                                  union iwreq_data *wrqu, char *extra)
519 {
520         int ret = 0;
521         down(&ieee->wx_sem);
522
523         if (wrqu->power.disabled) {
524                 ieee->ps = IEEE80211_PS_DISABLED;
525                 goto exit;
526         }
527         if (wrqu->power.flags & IW_POWER_TIMEOUT) {
528                 //ieee->ps_period = wrqu->power.value / 1000;
529                 ieee->ps_timeout = wrqu->power.value / 1000;
530         }
531
532         if (wrqu->power.flags & IW_POWER_PERIOD) {
533
534                 //ieee->ps_timeout = wrqu->power.value / 1000;
535                 ieee->ps_period = wrqu->power.value / 1000;
536                 //wrq->value / 1024;
537
538         }
539         switch (wrqu->power.flags & IW_POWER_MODE) {
540         case IW_POWER_UNICAST_R:
541                 ieee->ps = IEEE80211_PS_UNICAST;
542                 break;
543         case IW_POWER_MULTICAST_R:
544                 ieee->ps = IEEE80211_PS_MBCAST;
545                 break;
546         case IW_POWER_ALL_R:
547                 ieee->ps = IEEE80211_PS_UNICAST | IEEE80211_PS_MBCAST;
548                 break;
549
550         case IW_POWER_ON:
551         //      ieee->ps = IEEE80211_PS_DISABLED;
552                 break;
553
554         default:
555                 ret = -EINVAL;
556                 goto exit;
557
558         }
559 exit:
560         up(&ieee->wx_sem);
561         return ret;
562
563 }
564 EXPORT_SYMBOL(ieee80211_wx_set_power);
565
566 /* this is stolen from hostap */
567 int ieee80211_wx_get_power(struct ieee80211_device *ieee,
568                                  struct iw_request_info *info,
569                                  union iwreq_data *wrqu, char *extra)
570 {
571         down(&ieee->wx_sem);
572
573         if (ieee->ps == IEEE80211_PS_DISABLED) {
574                 wrqu->power.disabled = 1;
575                 goto exit;
576         }
577
578         wrqu->power.disabled = 0;
579
580         if ((wrqu->power.flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
581                 wrqu->power.flags = IW_POWER_TIMEOUT;
582                 wrqu->power.value = ieee->ps_timeout * 1000;
583         } else {
584 //              ret = -EOPNOTSUPP;
585 //              goto exit;
586                 wrqu->power.flags = IW_POWER_PERIOD;
587                 wrqu->power.value = ieee->ps_period * 1000;
588 //ieee->current_network.dtim_period * ieee->current_network.beacon_interval * 1024;
589         }
590
591        if ((ieee->ps & (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST)) == (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST))
592                 wrqu->power.flags |= IW_POWER_ALL_R;
593         else if (ieee->ps & IEEE80211_PS_MBCAST)
594                 wrqu->power.flags |= IW_POWER_MULTICAST_R;
595         else
596                 wrqu->power.flags |= IW_POWER_UNICAST_R;
597
598 exit:
599         up(&ieee->wx_sem);
600         return 0;
601
602 }
603 EXPORT_SYMBOL(ieee80211_wx_get_power);