upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / staging / rtl8192u / 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 #ifdef ENABLE_DOT11D
19 #include "dot11d.h"
20 #endif
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
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 #ifdef ENABLE_DOT11D
67                 if (!(GET_DOT11D_INFO(ieee)->channel_map)[fwrq->m]) {
68                         ret = -EINVAL;
69                         goto out;
70                 }
71 #endif
72                 ieee->current_network.channel = fwrq->m;
73                 ieee->set_chan(ieee->dev, ieee->current_network.channel);
74
75                 if(ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
76                         if(ieee->state == IEEE80211_LINKED){
77
78                         ieee80211_stop_send_beacons(ieee);
79                         ieee80211_start_send_beacons(ieee);
80                         }
81         }
82
83         ret = 0;
84 out:
85         up(&ieee->wx_sem);
86         return ret;
87 }
88
89
90 int ieee80211_wx_get_freq(struct ieee80211_device *ieee,
91                              struct iw_request_info *a,
92                              union iwreq_data *wrqu, char *b)
93 {
94         struct iw_freq *fwrq = & wrqu->freq;
95
96         if (ieee->current_network.channel == 0)
97                 return -1;
98         //NM 0.7.0 will not accept channel any more.
99         fwrq->m = ieee80211_wlan_frequencies[ieee->current_network.channel-1] * 100000;
100         fwrq->e = 1;
101 //      fwrq->m = ieee->current_network.channel;
102 //      fwrq->e = 0;
103
104         return 0;
105 }
106
107 int ieee80211_wx_get_wap(struct ieee80211_device *ieee,
108                             struct iw_request_info *info,
109                             union iwreq_data *wrqu, char *extra)
110 {
111         unsigned long flags;
112
113         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
114
115         if (ieee->iw_mode == IW_MODE_MONITOR)
116                 return -1;
117
118         /* We want avoid to give to the user inconsistent infos*/
119         spin_lock_irqsave(&ieee->lock, flags);
120
121         if (ieee->state != IEEE80211_LINKED &&
122                 ieee->state != IEEE80211_LINKED_SCANNING &&
123                 ieee->wap_set == 0)
124
125                 memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);
126         else
127                 memcpy(wrqu->ap_addr.sa_data,
128                        ieee->current_network.bssid, ETH_ALEN);
129
130         spin_unlock_irqrestore(&ieee->lock, flags);
131
132         return 0;
133 }
134
135
136 int ieee80211_wx_set_wap(struct ieee80211_device *ieee,
137                          struct iw_request_info *info,
138                          union iwreq_data *awrq,
139                          char *extra)
140 {
141
142         int ret = 0;
143         u8 zero[] = {0,0,0,0,0,0};
144         unsigned long flags;
145
146         short ifup = ieee->proto_started;//dev->flags & IFF_UP;
147         struct sockaddr *temp = (struct sockaddr *)awrq;
148
149         ieee->sync_scan_hurryup = 1;
150
151         down(&ieee->wx_sem);
152         /* use ifconfig hw ether */
153         if (ieee->iw_mode == IW_MODE_MASTER){
154                 ret = -1;
155                 goto out;
156         }
157
158         if (temp->sa_family != ARPHRD_ETHER){
159                 ret = -EINVAL;
160                 goto out;
161         }
162
163         if (ifup)
164                 ieee80211_stop_protocol(ieee);
165
166         /* just to avoid to give inconsistent infos in the
167          * get wx method. not really needed otherwise
168          */
169         spin_lock_irqsave(&ieee->lock, flags);
170
171         memcpy(ieee->current_network.bssid, temp->sa_data, ETH_ALEN);
172         ieee->wap_set = memcmp(temp->sa_data, zero,ETH_ALEN)!=0;
173
174         spin_unlock_irqrestore(&ieee->lock, flags);
175
176         if (ifup)
177                 ieee80211_start_protocol(ieee);
178 out:
179         up(&ieee->wx_sem);
180         return ret;
181 }
182
183  int ieee80211_wx_get_essid(struct ieee80211_device *ieee, struct iw_request_info *a,union iwreq_data *wrqu,char *b)
184 {
185         int len,ret = 0;
186         unsigned long flags;
187
188         if (ieee->iw_mode == IW_MODE_MONITOR)
189                 return -1;
190
191         /* We want avoid to give to the user inconsistent infos*/
192         spin_lock_irqsave(&ieee->lock, flags);
193
194         if (ieee->current_network.ssid[0] == '\0' ||
195                 ieee->current_network.ssid_len == 0){
196                 ret = -1;
197                 goto out;
198         }
199
200         if (ieee->state != IEEE80211_LINKED &&
201                 ieee->state != IEEE80211_LINKED_SCANNING &&
202                 ieee->ssid_set == 0){
203                 ret = -1;
204                 goto out;
205         }
206         len = ieee->current_network.ssid_len;
207         wrqu->essid.length = len;
208         strncpy(b,ieee->current_network.ssid,len);
209         wrqu->essid.flags = 1;
210
211 out:
212         spin_unlock_irqrestore(&ieee->lock, flags);
213
214         return ret;
215
216 }
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
230
231
232 int ieee80211_wx_get_rate(struct ieee80211_device *ieee,
233                              struct iw_request_info *info,
234                              union iwreq_data *wrqu, char *extra)
235 {
236         u32 tmp_rate;
237         tmp_rate = TxCountToDataRate(ieee, ieee->softmac_stats.CurrentShowTxate);
238
239         wrqu->bitrate.value = tmp_rate * 500000;
240
241         return 0;
242 }
243
244
245 int ieee80211_wx_set_rts(struct ieee80211_device *ieee,
246                              struct iw_request_info *info,
247                              union iwreq_data *wrqu, char *extra)
248 {
249         if (wrqu->rts.disabled || !wrqu->rts.fixed)
250                 ieee->rts = DEFAULT_RTS_THRESHOLD;
251         else
252         {
253                 if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
254                                 wrqu->rts.value > MAX_RTS_THRESHOLD)
255                         return -EINVAL;
256                 ieee->rts = wrqu->rts.value;
257         }
258         return 0;
259 }
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 int ieee80211_wx_set_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
271                              union iwreq_data *wrqu, char *b)
272 {
273
274         ieee->sync_scan_hurryup = 1;
275
276         down(&ieee->wx_sem);
277
278         if (wrqu->mode == ieee->iw_mode)
279                 goto out;
280
281         if (wrqu->mode == IW_MODE_MONITOR){
282
283                 ieee->dev->type = ARPHRD_IEEE80211;
284         }else{
285                 ieee->dev->type = ARPHRD_ETHER;
286         }
287
288         if (!ieee->proto_started){
289                 ieee->iw_mode = wrqu->mode;
290         }else{
291                 ieee80211_stop_protocol(ieee);
292                 ieee->iw_mode = wrqu->mode;
293                 ieee80211_start_protocol(ieee);
294         }
295
296 out:
297         up(&ieee->wx_sem);
298         return 0;
299 }
300
301 void ieee80211_wx_sync_scan_wq(struct work_struct *work)
302 {
303         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, wx_sync_scan_wq);
304         short chan;
305         HT_EXTCHNL_OFFSET chan_offset=0;
306         HT_CHANNEL_WIDTH bandwidth=0;
307         int b40M = 0;
308         static int count = 0;
309         chan = ieee->current_network.channel;
310         netif_carrier_off(ieee->dev);
311
312         if (ieee->data_hard_stop)
313                 ieee->data_hard_stop(ieee->dev);
314
315         ieee80211_stop_send_beacons(ieee);
316
317         ieee->state = IEEE80211_LINKED_SCANNING;
318         ieee->link_change(ieee->dev);
319         ieee->InitialGainHandler(ieee->dev,IG_Backup);
320         if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT && ieee->pHTInfo->bCurBW40MHz) {
321                 b40M = 1;
322                 chan_offset = ieee->pHTInfo->CurSTAExtChnlOffset;
323                 bandwidth = (HT_CHANNEL_WIDTH)ieee->pHTInfo->bCurBW40MHz;
324                 printk("Scan in 40M, force to 20M first:%d, %d\n", chan_offset, bandwidth);
325                 ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
326                 }
327         ieee80211_start_scan_syncro(ieee);
328         if (b40M) {
329                 printk("Scan in 20M, back to 40M\n");
330                 if (chan_offset == HT_EXTCHNL_OFFSET_UPPER)
331                         ieee->set_chan(ieee->dev, chan + 2);
332                 else if (chan_offset == HT_EXTCHNL_OFFSET_LOWER)
333                         ieee->set_chan(ieee->dev, chan - 2);
334                 else
335                         ieee->set_chan(ieee->dev, chan);
336                 ieee->SetBWModeHandler(ieee->dev, bandwidth, chan_offset);
337         } else {
338                 ieee->set_chan(ieee->dev, chan);
339         }
340
341         ieee->InitialGainHandler(ieee->dev,IG_Restore);
342         ieee->state = IEEE80211_LINKED;
343         ieee->link_change(ieee->dev);
344         // To prevent the immediately calling watch_dog after scan.
345         if(ieee->LinkDetectInfo.NumRecvBcnInPeriod==0||ieee->LinkDetectInfo.NumRecvDataInPeriod==0 )
346         {
347                 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
348                 ieee->LinkDetectInfo.NumRecvDataInPeriod= 1;
349         }
350         if (ieee->data_hard_resume)
351                 ieee->data_hard_resume(ieee->dev);
352
353         if(ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
354                 ieee80211_start_send_beacons(ieee);
355
356         netif_carrier_on(ieee->dev);
357         count = 0;
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
385 int ieee80211_wx_set_essid(struct ieee80211_device *ieee,
386                               struct iw_request_info *a,
387                               union iwreq_data *wrqu, char *extra)
388 {
389
390         int ret=0,len;
391         short proto_started;
392         unsigned long flags;
393
394         ieee->sync_scan_hurryup = 1;
395         down(&ieee->wx_sem);
396
397         proto_started = ieee->proto_started;
398
399         if (wrqu->essid.length > IW_ESSID_MAX_SIZE){
400                 ret= -E2BIG;
401                 goto out;
402         }
403
404         if (ieee->iw_mode == IW_MODE_MONITOR){
405                 ret= -1;
406                 goto out;
407         }
408
409         if(proto_started)
410                 ieee80211_stop_protocol(ieee);
411
412
413         /* this is just to be sure that the GET wx callback
414          * has consisten infos. not needed otherwise
415          */
416         spin_lock_irqsave(&ieee->lock, flags);
417
418         if (wrqu->essid.flags && wrqu->essid.length) {
419                 //first flush current network.ssid
420                 len = ((wrqu->essid.length-1) < IW_ESSID_MAX_SIZE) ? (wrqu->essid.length-1) : IW_ESSID_MAX_SIZE;
421                 strncpy(ieee->current_network.ssid, extra, len+1);
422                 ieee->current_network.ssid_len = len+1;
423                 ieee->ssid_set = 1;
424         }
425         else{
426                 ieee->ssid_set = 0;
427                 ieee->current_network.ssid[0] = '\0';
428                 ieee->current_network.ssid_len = 0;
429         }
430         spin_unlock_irqrestore(&ieee->lock, flags);
431
432         if (proto_started)
433                 ieee80211_start_protocol(ieee);
434 out:
435         up(&ieee->wx_sem);
436         return ret;
437 }
438
439  int ieee80211_wx_get_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
440                              union iwreq_data *wrqu, char *b)
441 {
442
443         wrqu->mode = ieee->iw_mode;
444         return 0;
445 }
446
447  int ieee80211_wx_set_rawtx(struct ieee80211_device *ieee,
448                                struct iw_request_info *info,
449                                union iwreq_data *wrqu, char *extra)
450 {
451
452         int *parms = (int *)extra;
453         int enable = (parms[0] > 0);
454         short prev = ieee->raw_tx;
455
456         down(&ieee->wx_sem);
457
458         if(enable)
459                 ieee->raw_tx = 1;
460         else
461                 ieee->raw_tx = 0;
462
463         printk(KERN_INFO"raw TX is %s\n",
464               ieee->raw_tx ? "enabled" : "disabled");
465
466         if(ieee->iw_mode == IW_MODE_MONITOR)
467         {
468                 if(prev == 0 && ieee->raw_tx){
469                         if (ieee->data_hard_resume)
470                                 ieee->data_hard_resume(ieee->dev);
471
472                         netif_carrier_on(ieee->dev);
473                 }
474
475                 if(prev && ieee->raw_tx == 1)
476                         netif_carrier_off(ieee->dev);
477         }
478
479         up(&ieee->wx_sem);
480
481         return 0;
482 }
483
484 int ieee80211_wx_get_name(struct ieee80211_device *ieee,
485                              struct iw_request_info *info,
486                              union iwreq_data *wrqu, char *extra)
487 {
488         strcpy(wrqu->name, "802.11");
489         if(ieee->modulation & IEEE80211_CCK_MODULATION){
490                 strcat(wrqu->name, "b");
491                 if(ieee->modulation & IEEE80211_OFDM_MODULATION)
492                         strcat(wrqu->name, "/g");
493         }else if(ieee->modulation & IEEE80211_OFDM_MODULATION)
494                 strcat(wrqu->name, "g");
495         if (ieee->mode & (IEEE_N_24G | IEEE_N_5G))
496                 strcat(wrqu->name, "/n");
497
498         if((ieee->state == IEEE80211_LINKED) ||
499                 (ieee->state == IEEE80211_LINKED_SCANNING))
500                 strcat(wrqu->name," linked");
501         else if(ieee->state != IEEE80211_NOLINK)
502                 strcat(wrqu->name," link..");
503
504
505         return 0;
506 }
507
508
509 /* this is mostly stolen from hostap */
510 int ieee80211_wx_set_power(struct ieee80211_device *ieee,
511                                  struct iw_request_info *info,
512                                  union iwreq_data *wrqu, char *extra)
513 {
514         int ret = 0;
515         down(&ieee->wx_sem);
516
517         if (wrqu->power.disabled){
518                 ieee->ps = IEEE80211_PS_DISABLED;
519                 goto exit;
520         }
521         if (wrqu->power.flags & IW_POWER_TIMEOUT) {
522                 //ieee->ps_period = wrqu->power.value / 1000;
523                 ieee->ps_timeout = wrqu->power.value / 1000;
524         }
525
526         if (wrqu->power.flags & IW_POWER_PERIOD) {
527
528                 //ieee->ps_timeout = wrqu->power.value / 1000;
529                 ieee->ps_period = wrqu->power.value / 1000;
530                 //wrq->value / 1024;
531
532         }
533         switch (wrqu->power.flags & IW_POWER_MODE) {
534         case IW_POWER_UNICAST_R:
535                 ieee->ps = IEEE80211_PS_UNICAST;
536                 break;
537         case IW_POWER_MULTICAST_R:
538                 ieee->ps = IEEE80211_PS_MBCAST;
539                 break;
540         case IW_POWER_ALL_R:
541                 ieee->ps = IEEE80211_PS_UNICAST | IEEE80211_PS_MBCAST;
542                 break;
543
544         case IW_POWER_ON:
545         //      ieee->ps = IEEE80211_PS_DISABLED;
546                 break;
547
548         default:
549                 ret = -EINVAL;
550                 goto exit;
551
552         }
553 exit:
554         up(&ieee->wx_sem);
555         return ret;
556
557 }
558
559 /* this is stolen from hostap */
560 int ieee80211_wx_get_power(struct ieee80211_device *ieee,
561                                  struct iw_request_info *info,
562                                  union iwreq_data *wrqu, char *extra)
563 {
564         int ret =0;
565
566         down(&ieee->wx_sem);
567
568         if(ieee->ps == IEEE80211_PS_DISABLED){
569                 wrqu->power.disabled = 1;
570                 goto exit;
571         }
572
573         wrqu->power.disabled = 0;
574
575         if ((wrqu->power.flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
576                 wrqu->power.flags = IW_POWER_TIMEOUT;
577                 wrqu->power.value = ieee->ps_timeout * 1000;
578         } else {
579 //              ret = -EOPNOTSUPP;
580 //              goto exit;
581                 wrqu->power.flags = IW_POWER_PERIOD;
582                 wrqu->power.value = ieee->ps_period * 1000;
583 //ieee->current_network.dtim_period * ieee->current_network.beacon_interval * 1024;
584         }
585
586        if ((ieee->ps & (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST)) == (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST))
587                 wrqu->power.flags |= IW_POWER_ALL_R;
588         else if (ieee->ps & IEEE80211_PS_MBCAST)
589                 wrqu->power.flags |= IW_POWER_MULTICAST_R;
590         else
591                 wrqu->power.flags |= IW_POWER_UNICAST_R;
592
593 exit:
594         up(&ieee->wx_sem);
595         return ret;
596
597 }
598 EXPORT_SYMBOL(ieee80211_wx_get_essid);
599 EXPORT_SYMBOL(ieee80211_wx_set_essid);
600 EXPORT_SYMBOL(ieee80211_wx_set_rate);
601 EXPORT_SYMBOL(ieee80211_wx_get_rate);
602 EXPORT_SYMBOL(ieee80211_wx_set_wap);
603 EXPORT_SYMBOL(ieee80211_wx_get_wap);
604 EXPORT_SYMBOL(ieee80211_wx_set_mode);
605 EXPORT_SYMBOL(ieee80211_wx_get_mode);
606 EXPORT_SYMBOL(ieee80211_wx_set_scan);
607 EXPORT_SYMBOL(ieee80211_wx_get_freq);
608 EXPORT_SYMBOL(ieee80211_wx_set_freq);
609 EXPORT_SYMBOL(ieee80211_wx_set_rawtx);
610 EXPORT_SYMBOL(ieee80211_wx_get_name);
611 EXPORT_SYMBOL(ieee80211_wx_set_power);
612 EXPORT_SYMBOL(ieee80211_wx_get_power);
613 EXPORT_SYMBOL(ieee80211_wlan_frequencies);
614 EXPORT_SYMBOL(ieee80211_wx_set_rts);
615 EXPORT_SYMBOL(ieee80211_wx_get_rts);