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