Staging: rtl8192u: spaces required around = operator
[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                                 ieee80211_stop_send_beacons(ieee);
76                                 ieee80211_start_send_beacons(ieee);
77                         }
78         }
79
80         ret = 0;
81 out:
82         up(&ieee->wx_sem);
83         return ret;
84 }
85 EXPORT_SYMBOL(ieee80211_wx_set_freq);
86
87 int ieee80211_wx_get_freq(struct ieee80211_device *ieee,
88                              struct iw_request_info *a,
89                              union iwreq_data *wrqu, char *b)
90 {
91         struct iw_freq *fwrq = &wrqu->freq;
92
93         if (ieee->current_network.channel == 0)
94                 return -1;
95         /* NM 0.7.0 will not accept channel any more. */
96         fwrq->m = ieee80211_wlan_frequencies[ieee->current_network.channel-1] * 100000;
97         fwrq->e = 1;
98         /* fwrq->m = ieee->current_network.channel; */
99         /* fwrq->e = 0; */
100
101         return 0;
102 }
103 EXPORT_SYMBOL(ieee80211_wx_get_freq);
104
105 int ieee80211_wx_get_wap(struct ieee80211_device *ieee,
106                             struct iw_request_info *info,
107                             union iwreq_data *wrqu, char *extra)
108 {
109         unsigned long flags;
110
111         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
112
113         if (ieee->iw_mode == IW_MODE_MONITOR)
114                 return -1;
115
116         /* We want avoid to give to the user inconsistent infos*/
117         spin_lock_irqsave(&ieee->lock, flags);
118
119         if (ieee->state != IEEE80211_LINKED &&
120                 ieee->state != IEEE80211_LINKED_SCANNING &&
121                 ieee->wap_set == 0)
122
123                 eth_zero_addr(wrqu->ap_addr.sa_data);
124         else
125                 memcpy(wrqu->ap_addr.sa_data,
126                        ieee->current_network.bssid, ETH_ALEN);
127
128         spin_unlock_irqrestore(&ieee->lock, flags);
129
130         return 0;
131 }
132 EXPORT_SYMBOL(ieee80211_wx_get_wap);
133
134 int ieee80211_wx_set_wap(struct ieee80211_device *ieee,
135                          struct iw_request_info *info,
136                          union iwreq_data *awrq,
137                          char *extra)
138 {
139
140         int ret = 0;
141         unsigned long flags;
142
143         short ifup = ieee->proto_started; /* dev->flags & IFF_UP; */
144         struct sockaddr *temp = (struct sockaddr *)awrq;
145
146         ieee->sync_scan_hurryup = 1;
147
148         down(&ieee->wx_sem);
149         /* use ifconfig hw ether */
150         if (ieee->iw_mode == IW_MODE_MASTER) {
151                 ret = -1;
152                 goto out;
153         }
154
155         if (temp->sa_family != ARPHRD_ETHER) {
156                 ret = -EINVAL;
157                 goto out;
158         }
159
160         if (ifup)
161                 ieee80211_stop_protocol(ieee);
162
163         /* just to avoid to give inconsistent infos in the
164          * get wx method. not really needed otherwise
165          */
166         spin_lock_irqsave(&ieee->lock, flags);
167
168         memcpy(ieee->current_network.bssid, temp->sa_data, ETH_ALEN);
169         ieee->wap_set = !is_zero_ether_addr(temp->sa_data);
170
171         spin_unlock_irqrestore(&ieee->lock, flags);
172
173         if (ifup)
174                 ieee80211_start_protocol(ieee);
175 out:
176         up(&ieee->wx_sem);
177         return ret;
178 }
179 EXPORT_SYMBOL(ieee80211_wx_set_wap);
180
181 int ieee80211_wx_get_essid(struct ieee80211_device *ieee, struct iw_request_info *a, union iwreq_data *wrqu, char *b)
182 {
183         int len, ret = 0;
184         unsigned long flags;
185
186         if (ieee->iw_mode == IW_MODE_MONITOR)
187                 return -1;
188
189         /* We want avoid to give to the user inconsistent infos*/
190         spin_lock_irqsave(&ieee->lock, flags);
191
192         if (ieee->current_network.ssid[0] == '\0' ||
193                 ieee->current_network.ssid_len == 0) {
194                 ret = -1;
195                 goto out;
196         }
197
198         if (ieee->state != IEEE80211_LINKED &&
199                 ieee->state != IEEE80211_LINKED_SCANNING &&
200                 ieee->ssid_set == 0) {
201                 ret = -1;
202                 goto out;
203         }
204         len = ieee->current_network.ssid_len;
205         wrqu->essid.length = len;
206         strncpy(b, ieee->current_network.ssid, len);
207         wrqu->essid.flags = 1;
208
209 out:
210         spin_unlock_irqrestore(&ieee->lock, flags);
211
212         return ret;
213
214 }
215 EXPORT_SYMBOL(ieee80211_wx_get_essid);
216
217 int ieee80211_wx_set_rate(struct ieee80211_device *ieee,
218                              struct iw_request_info *info,
219                              union iwreq_data *wrqu, char *extra)
220 {
221
222         u32 target_rate = wrqu->bitrate.value;
223
224         ieee->rate = target_rate/100000;
225         /* FIXME: we might want to limit rate also in management protocols. */
226         return 0;
227 }
228 EXPORT_SYMBOL(ieee80211_wx_set_rate);
229
230 int ieee80211_wx_get_rate(struct ieee80211_device *ieee,
231                              struct iw_request_info *info,
232                              union iwreq_data *wrqu, char *extra)
233 {
234         u32 tmp_rate;
235
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                 if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
252                                 wrqu->rts.value > MAX_RTS_THRESHOLD)
253                         return -EINVAL;
254                 ieee->rts = wrqu->rts.value;
255         }
256         return 0;
257 }
258 EXPORT_SYMBOL(ieee80211_wx_set_rts);
259
260 int ieee80211_wx_get_rts(struct ieee80211_device *ieee,
261                              struct iw_request_info *info,
262                              union iwreq_data *wrqu, char *extra)
263 {
264         wrqu->rts.value = ieee->rts;
265         wrqu->rts.fixed = 0;    /* no auto select */
266         wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
267         return 0;
268 }
269 EXPORT_SYMBOL(ieee80211_wx_get_rts);
270
271 int ieee80211_wx_set_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
272                              union iwreq_data *wrqu, char *b)
273 {
274
275         ieee->sync_scan_hurryup = 1;
276
277         down(&ieee->wx_sem);
278
279         if (wrqu->mode == ieee->iw_mode)
280                 goto out;
281
282         if (wrqu->mode == IW_MODE_MONITOR) {
283
284                 ieee->dev->type = ARPHRD_IEEE80211;
285         } else {
286                 ieee->dev->type = ARPHRD_ETHER;
287         }
288
289         if (!ieee->proto_started) {
290                 ieee->iw_mode = wrqu->mode;
291         } else {
292                 ieee80211_stop_protocol(ieee);
293                 ieee->iw_mode = wrqu->mode;
294                 ieee80211_start_protocol(ieee);
295         }
296
297 out:
298         up(&ieee->wx_sem);
299         return 0;
300 }
301 EXPORT_SYMBOL(ieee80211_wx_set_mode);
302
303 void ieee80211_wx_sync_scan_wq(struct work_struct *work)
304 {
305         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, wx_sync_scan_wq);
306         short chan;
307         HT_EXTCHNL_OFFSET chan_offset = 0;
308         HT_CHANNEL_WIDTH bandwidth = 0;
309         int b40M = 0;
310
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                 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
349                 ieee->LinkDetectInfo.NumRecvDataInPeriod = 1;
350         }
351         if (ieee->data_hard_resume)
352                 ieee->data_hard_resume(ieee->dev);
353
354         if (ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
355                 ieee80211_start_send_beacons(ieee);
356
357         netif_carrier_on(ieee->dev);
358         up(&ieee->wx_sem);
359
360 }
361
362 int ieee80211_wx_set_scan(struct ieee80211_device *ieee, struct iw_request_info *a,
363                              union iwreq_data *wrqu, char *b)
364 {
365         int ret = 0;
366
367         down(&ieee->wx_sem);
368
369         if (ieee->iw_mode == IW_MODE_MONITOR || !(ieee->proto_started)) {
370                 ret = -1;
371                 goto out;
372         }
373
374         if (ieee->state == IEEE80211_LINKED) {
375                 queue_work(ieee->wq, &ieee->wx_sync_scan_wq);
376                 /* intentionally forget to up sem */
377                 return 0;
378         }
379
380 out:
381         up(&ieee->wx_sem);
382         return ret;
383 }
384 EXPORT_SYMBOL(ieee80211_wx_set_scan);
385
386 int ieee80211_wx_set_essid(struct ieee80211_device *ieee,
387                               struct iw_request_info *a,
388                               union iwreq_data *wrqu, char *extra)
389 {
390
391         int ret = 0, len;
392         short proto_started;
393         unsigned long flags;
394
395         ieee->sync_scan_hurryup = 1;
396         down(&ieee->wx_sem);
397
398         proto_started = ieee->proto_started;
399
400         if (wrqu->essid.length > IW_ESSID_MAX_SIZE) {
401                 ret = -E2BIG;
402                 goto out;
403         }
404
405         if (ieee->iw_mode == IW_MODE_MONITOR) {
406                 ret = -1;
407                 goto out;
408         }
409
410         if (proto_started)
411                 ieee80211_stop_protocol(ieee);
412
413
414         /* this is just to be sure that the GET wx callback
415          * has consisten infos. not needed otherwise
416          */
417         spin_lock_irqsave(&ieee->lock, flags);
418
419         if (wrqu->essid.flags && wrqu->essid.length) {
420                 /* first flush current network.ssid */
421                 len = ((wrqu->essid.length-1) < IW_ESSID_MAX_SIZE) ? (wrqu->essid.length-1) : IW_ESSID_MAX_SIZE;
422                 strncpy(ieee->current_network.ssid, extra, len+1);
423                 ieee->current_network.ssid_len = len+1;
424                 ieee->ssid_set = 1;
425         }
426         else {
427                 ieee->ssid_set = 0;
428                 ieee->current_network.ssid[0] = '\0';
429                 ieee->current_network.ssid_len = 0;
430         }
431         spin_unlock_irqrestore(&ieee->lock, flags);
432
433         if (proto_started)
434                 ieee80211_start_protocol(ieee);
435 out:
436         up(&ieee->wx_sem);
437         return ret;
438 }
439 EXPORT_SYMBOL(ieee80211_wx_set_essid);
440
441 int ieee80211_wx_get_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
442                              union iwreq_data *wrqu, char *b)
443 {
444
445         wrqu->mode = ieee->iw_mode;
446         return 0;
447 }
448 EXPORT_SYMBOL(ieee80211_wx_get_mode);
449
450 int ieee80211_wx_set_rawtx(struct ieee80211_device *ieee,
451                                struct iw_request_info *info,
452                                union iwreq_data *wrqu, char *extra)
453 {
454
455         int *parms = (int *)extra;
456         int enable = (parms[0] > 0);
457         short prev = ieee->raw_tx;
458
459         down(&ieee->wx_sem);
460
461         if (enable)
462                 ieee->raw_tx = 1;
463         else
464                 ieee->raw_tx = 0;
465
466         printk(KERN_INFO"raw TX is %s\n",
467               ieee->raw_tx ? "enabled" : "disabled");
468
469         if (ieee->iw_mode == IW_MODE_MONITOR) {
470                 if (prev == 0 && ieee->raw_tx) {
471                         if (ieee->data_hard_resume)
472                                 ieee->data_hard_resume(ieee->dev);
473
474                         netif_carrier_on(ieee->dev);
475                 }
476
477                 if (prev && ieee->raw_tx == 1)
478                         netif_carrier_off(ieee->dev);
479         }
480
481         up(&ieee->wx_sem);
482
483         return 0;
484 }
485 EXPORT_SYMBOL(ieee80211_wx_set_rawtx);
486
487 int ieee80211_wx_get_name(struct ieee80211_device *ieee,
488                              struct iw_request_info *info,
489                              union iwreq_data *wrqu, char *extra)
490 {
491         strlcpy(wrqu->name, "802.11", IFNAMSIZ);
492         if (ieee->modulation & IEEE80211_CCK_MODULATION) {
493                 strlcat(wrqu->name, "b", IFNAMSIZ);
494                 if (ieee->modulation & IEEE80211_OFDM_MODULATION)
495                         strlcat(wrqu->name, "/g", IFNAMSIZ);
496         } else if (ieee->modulation & IEEE80211_OFDM_MODULATION) {
497                 strlcat(wrqu->name, "g", IFNAMSIZ);
498         }
499
500         if (ieee->mode & (IEEE_N_24G | IEEE_N_5G))
501                 strlcat(wrqu->name, "/n", IFNAMSIZ);
502
503         if ((ieee->state == IEEE80211_LINKED) ||
504             (ieee->state == IEEE80211_LINKED_SCANNING))
505                 strlcat(wrqu->name, " linked", IFNAMSIZ);
506         else if (ieee->state != IEEE80211_NOLINK)
507                 strlcat(wrqu->name, " link..", IFNAMSIZ);
508
509         return 0;
510 }
511 EXPORT_SYMBOL(ieee80211_wx_get_name);
512
513 /* this is mostly stolen from hostap */
514 int ieee80211_wx_set_power(struct ieee80211_device *ieee,
515                                  struct iw_request_info *info,
516                                  union iwreq_data *wrqu, char *extra)
517 {
518         int ret = 0;
519
520         down(&ieee->wx_sem);
521
522         if (wrqu->power.disabled) {
523                 ieee->ps = IEEE80211_PS_DISABLED;
524                 goto exit;
525         }
526         if (wrqu->power.flags & IW_POWER_TIMEOUT) {
527                 /* ieee->ps_period = wrqu->power.value / 1000; */
528                 ieee->ps_timeout = wrqu->power.value / 1000;
529         }
530
531         if (wrqu->power.flags & IW_POWER_PERIOD) {
532
533                 /* ieee->ps_timeout = wrqu->power.value / 1000; */
534                 ieee->ps_period = wrqu->power.value / 1000;
535                 /* wrq->value / 1024; */
536
537         }
538         switch (wrqu->power.flags & IW_POWER_MODE) {
539         case IW_POWER_UNICAST_R:
540                 ieee->ps = IEEE80211_PS_UNICAST;
541                 break;
542         case IW_POWER_MULTICAST_R:
543                 ieee->ps = IEEE80211_PS_MBCAST;
544                 break;
545         case IW_POWER_ALL_R:
546                 ieee->ps = IEEE80211_PS_UNICAST | IEEE80211_PS_MBCAST;
547                 break;
548
549         case IW_POWER_ON:
550                 /* ieee->ps = IEEE80211_PS_DISABLED; */
551                 break;
552
553         default:
554                 ret = -EINVAL;
555                 goto exit;
556
557         }
558 exit:
559         up(&ieee->wx_sem);
560         return ret;
561
562 }
563 EXPORT_SYMBOL(ieee80211_wx_set_power);
564
565 /* this is stolen from hostap */
566 int ieee80211_wx_get_power(struct ieee80211_device *ieee,
567                                  struct iw_request_info *info,
568                                  union iwreq_data *wrqu, char *extra)
569 {
570         down(&ieee->wx_sem);
571
572         if (ieee->ps == IEEE80211_PS_DISABLED) {
573                 wrqu->power.disabled = 1;
574                 goto exit;
575         }
576
577         wrqu->power.disabled = 0;
578
579         if ((wrqu->power.flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
580                 wrqu->power.flags = IW_POWER_TIMEOUT;
581                 wrqu->power.value = ieee->ps_timeout * 1000;
582         } else {
583                 /* ret = -EOPNOTSUPP; */
584                 /* goto exit; */
585                 wrqu->power.flags = IW_POWER_PERIOD;
586                 wrqu->power.value = ieee->ps_period * 1000;
587                 /* ieee->current_network.dtim_period * ieee->current_network.beacon_interval * 1024; */
588         }
589
590         if ((ieee->ps & (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST)) == (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST))
591                 wrqu->power.flags |= IW_POWER_ALL_R;
592         else if (ieee->ps & IEEE80211_PS_MBCAST)
593                 wrqu->power.flags |= IW_POWER_MULTICAST_R;
594         else
595                 wrqu->power.flags |= IW_POWER_UNICAST_R;
596
597 exit:
598         up(&ieee->wx_sem);
599         return 0;
600
601 }
602 EXPORT_SYMBOL(ieee80211_wx_get_power);