Merge git://git.kernel.org/pub/scm/linux/kernel/git/lethal/sh-2.6
[pandora-kernel.git] / 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         //printk("===>mode:%d, halfNmode:%d\n", ieee->mode, ieee->bHalfWirelessN24GMode);
234         if (ieee->mode & (IEEE_A | IEEE_B | IEEE_G))
235                 tmp_rate = ieee->rate;
236         else if (ieee->mode & IEEE_N_5G)
237                 tmp_rate = 580;
238         else if (ieee->mode & IEEE_N_24G)
239         {
240                 if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
241                         tmp_rate = HTHalfMcsToDataRate(ieee, 15);
242                 else
243                         tmp_rate = HTMcsToDataRate(ieee, 15);
244         }
245         wrqu->bitrate.value = tmp_rate * 500000;
246
247         return 0;
248 }
249
250
251 int ieee80211_wx_set_rts(struct ieee80211_device *ieee,
252                              struct iw_request_info *info,
253                              union iwreq_data *wrqu, char *extra)
254 {
255         if (wrqu->rts.disabled || !wrqu->rts.fixed)
256                 ieee->rts = DEFAULT_RTS_THRESHOLD;
257         else
258         {
259                 if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
260                                 wrqu->rts.value > MAX_RTS_THRESHOLD)
261                         return -EINVAL;
262                 ieee->rts = wrqu->rts.value;
263         }
264         return 0;
265 }
266
267 int ieee80211_wx_get_rts(struct ieee80211_device *ieee,
268                              struct iw_request_info *info,
269                              union iwreq_data *wrqu, char *extra)
270 {
271         wrqu->rts.value = ieee->rts;
272         wrqu->rts.fixed = 0;    /* no auto select */
273         wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
274         return 0;
275 }
276 int ieee80211_wx_set_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
277                              union iwreq_data *wrqu, char *b)
278 {
279
280         ieee->sync_scan_hurryup = 1;
281
282         down(&ieee->wx_sem);
283
284         if (wrqu->mode == ieee->iw_mode)
285                 goto out;
286
287         if (wrqu->mode == IW_MODE_MONITOR){
288
289                 ieee->dev->type = ARPHRD_IEEE80211;
290         }else{
291                 ieee->dev->type = ARPHRD_ETHER;
292         }
293
294         if (!ieee->proto_started){
295                 ieee->iw_mode = wrqu->mode;
296         }else{
297                 ieee80211_stop_protocol(ieee);
298                 ieee->iw_mode = wrqu->mode;
299                 ieee80211_start_protocol(ieee);
300         }
301
302 out:
303         up(&ieee->wx_sem);
304         return 0;
305 }
306
307 void ieee80211_wx_sync_scan_wq(struct work_struct *work)
308 {
309         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, wx_sync_scan_wq);
310         short chan;
311         HT_EXTCHNL_OFFSET chan_offset=0;
312         HT_CHANNEL_WIDTH bandwidth=0;
313         int b40M = 0;
314         static int count = 0;
315         chan = ieee->current_network.channel;
316         netif_carrier_off(ieee->dev);
317
318         if (ieee->data_hard_stop)
319                 ieee->data_hard_stop(ieee->dev);
320
321         ieee80211_stop_send_beacons(ieee);
322
323         ieee->state = IEEE80211_LINKED_SCANNING;
324         ieee->link_change(ieee->dev);
325         ieee->InitialGainHandler(ieee->dev,IG_Backup);
326         if (ieee->SetFwCmdHandler)
327         {
328                 ieee->SetFwCmdHandler(ieee->dev, FW_CMD_DIG_HALT);
329                 ieee->SetFwCmdHandler(ieee->dev, FW_CMD_HIGH_PWR_DISABLE);
330         }
331         if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT && ieee->pHTInfo->bCurBW40MHz) {
332                 b40M = 1;
333                 chan_offset = ieee->pHTInfo->CurSTAExtChnlOffset;
334                 bandwidth = (HT_CHANNEL_WIDTH)ieee->pHTInfo->bCurBW40MHz;
335                 printk("Scan in 40M, force to 20M first:%d, %d\n", chan_offset, bandwidth);
336                 ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
337                 }
338         ieee80211_start_scan_syncro(ieee);
339         if (b40M) {
340                 printk("Scan in 20M, back to 40M\n");
341                 if (chan_offset == HT_EXTCHNL_OFFSET_UPPER)
342                         ieee->set_chan(ieee->dev, chan + 2);
343                 else if (chan_offset == HT_EXTCHNL_OFFSET_LOWER)
344                         ieee->set_chan(ieee->dev, chan - 2);
345                 else
346                         ieee->set_chan(ieee->dev, chan);
347                 ieee->SetBWModeHandler(ieee->dev, bandwidth, chan_offset);
348         } else {
349                 ieee->set_chan(ieee->dev, chan);
350         }
351
352         ieee->InitialGainHandler(ieee->dev,IG_Restore);
353         if (ieee->SetFwCmdHandler)
354         {
355                 ieee->SetFwCmdHandler(ieee->dev, FW_CMD_DIG_RESUME);
356                 ieee->SetFwCmdHandler(ieee->dev, FW_CMD_HIGH_PWR_ENABLE);
357         }
358         ieee->state = IEEE80211_LINKED;
359         ieee->link_change(ieee->dev);
360         // To prevent the immediately calling watch_dog after scan.
361         if(ieee->LinkDetectInfo.NumRecvBcnInPeriod==0||ieee->LinkDetectInfo.NumRecvDataInPeriod==0 )
362         {
363                 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
364                 ieee->LinkDetectInfo.NumRecvDataInPeriod= 1;
365         }
366         if (ieee->data_hard_resume)
367                 ieee->data_hard_resume(ieee->dev);
368
369         if(ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
370                 ieee80211_start_send_beacons(ieee);
371
372         netif_carrier_on(ieee->dev);
373         count = 0;
374         up(&ieee->wx_sem);
375
376 }
377
378 int ieee80211_wx_set_scan(struct ieee80211_device *ieee, struct iw_request_info *a,
379                              union iwreq_data *wrqu, char *b)
380 {
381         int ret = 0;
382
383         down(&ieee->wx_sem);
384
385         if (ieee->iw_mode == IW_MODE_MONITOR || !(ieee->proto_started)){
386                 ret = -1;
387                 goto out;
388         }
389
390         if ( ieee->state == IEEE80211_LINKED){
391                 queue_work(ieee->wq, &ieee->wx_sync_scan_wq);
392                 /* intentionally forget to up sem */
393                 return 0;
394         }
395
396 out:
397         up(&ieee->wx_sem);
398         return ret;
399 }
400
401 int ieee80211_wx_set_essid(struct ieee80211_device *ieee,
402                               struct iw_request_info *a,
403                               union iwreq_data *wrqu, char *extra)
404 {
405
406         int ret=0,len;
407         short proto_started;
408         unsigned long flags;
409
410         ieee->sync_scan_hurryup = 1;
411         down(&ieee->wx_sem);
412
413         proto_started = ieee->proto_started;
414
415         if (wrqu->essid.length > IW_ESSID_MAX_SIZE){
416                 ret= -E2BIG;
417                 goto out;
418         }
419
420         if (ieee->iw_mode == IW_MODE_MONITOR){
421                 ret= -1;
422                 goto out;
423         }
424
425         if(proto_started)
426                 ieee80211_stop_protocol(ieee);
427
428
429         /* this is just to be sure that the GET wx callback
430          * has consisten infos. not needed otherwise
431          */
432         spin_lock_irqsave(&ieee->lock, flags);
433
434         if (wrqu->essid.flags && wrqu->essid.length) {
435                 //first flush current network.ssid
436                 len = ((wrqu->essid.length-1) < IW_ESSID_MAX_SIZE) ? (wrqu->essid.length-1) : IW_ESSID_MAX_SIZE;
437                 strncpy(ieee->current_network.ssid, extra, len+1);
438                 ieee->current_network.ssid_len = len+1;
439                 ieee->ssid_set = 1;
440         }
441         else{
442                 ieee->ssid_set = 0;
443                 ieee->current_network.ssid[0] = '\0';
444                 ieee->current_network.ssid_len = 0;
445         }
446         spin_unlock_irqrestore(&ieee->lock, flags);
447
448         if (proto_started)
449                 ieee80211_start_protocol(ieee);
450 out:
451         up(&ieee->wx_sem);
452         return ret;
453 }
454
455  int ieee80211_wx_get_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
456                              union iwreq_data *wrqu, char *b)
457 {
458
459         wrqu->mode = ieee->iw_mode;
460         return 0;
461 }
462
463  int ieee80211_wx_set_rawtx(struct ieee80211_device *ieee,
464                                struct iw_request_info *info,
465                                union iwreq_data *wrqu, char *extra)
466 {
467
468         int *parms = (int *)extra;
469         int enable = (parms[0] > 0);
470         short prev = ieee->raw_tx;
471
472         down(&ieee->wx_sem);
473
474         if(enable)
475                 ieee->raw_tx = 1;
476         else
477                 ieee->raw_tx = 0;
478
479         printk(KERN_INFO"raw TX is %s\n",
480               ieee->raw_tx ? "enabled" : "disabled");
481
482         if(ieee->iw_mode == IW_MODE_MONITOR)
483         {
484                 if(prev == 0 && ieee->raw_tx){
485                         if (ieee->data_hard_resume)
486                                 ieee->data_hard_resume(ieee->dev);
487
488                         netif_carrier_on(ieee->dev);
489                 }
490
491                 if(prev && ieee->raw_tx == 1)
492                         netif_carrier_off(ieee->dev);
493         }
494
495         up(&ieee->wx_sem);
496
497         return 0;
498 }
499
500 int ieee80211_wx_get_name(struct ieee80211_device *ieee,
501                              struct iw_request_info *info,
502                              union iwreq_data *wrqu, char *extra)
503 {
504         strlcpy(wrqu->name, "802.11", IFNAMSIZ);
505         if(ieee->modulation & IEEE80211_CCK_MODULATION){
506                 strlcat(wrqu->name, "b", IFNAMSIZ);
507                 if(ieee->modulation & IEEE80211_OFDM_MODULATION)
508                         strlcat(wrqu->name, "/g", IFNAMSIZ);
509         }else if(ieee->modulation & IEEE80211_OFDM_MODULATION)
510                 strlcat(wrqu->name, "g", IFNAMSIZ);
511         if (ieee->mode & (IEEE_N_24G | IEEE_N_5G))
512                 strlcat(wrqu->name, "/n", IFNAMSIZ);
513
514         if((ieee->state == IEEE80211_LINKED) ||
515                 (ieee->state == IEEE80211_LINKED_SCANNING))
516                 strlcat(wrqu->name, "  link", IFNAMSIZ);
517         else if(ieee->state != IEEE80211_NOLINK)
518                 strlcat(wrqu->name, " .....", IFNAMSIZ);
519
520
521         return 0;
522 }
523
524
525 /* this is mostly stolen from hostap */
526 int ieee80211_wx_set_power(struct ieee80211_device *ieee,
527                                  struct iw_request_info *info,
528                                  union iwreq_data *wrqu, char *extra)
529 {
530         int ret = 0;
531 #if 1
532         if(
533                 (!ieee->sta_wake_up) ||
534         //      (!ieee->ps_request_tx_ack) ||
535                 (!ieee->enter_sleep_state) ||
536                 (!ieee->ps_is_queue_empty)){
537
538         //      printk("ERROR. PS mode is tryied to be use but driver missed a callback\n\n");
539
540                 return -1;
541         }
542 #endif
543         down(&ieee->wx_sem);
544
545         if (wrqu->power.disabled){
546                 ieee->ps = IEEE80211_PS_DISABLED;
547                 goto exit;
548         }
549         if (wrqu->power.flags & IW_POWER_TIMEOUT) {
550                 //ieee->ps_period = wrqu->power.value / 1000;
551                 ieee->ps_timeout = wrqu->power.value / 1000;
552         }
553
554         if (wrqu->power.flags & IW_POWER_PERIOD) {
555
556                 //ieee->ps_timeout = wrqu->power.value / 1000;
557                 ieee->ps_period = wrqu->power.value / 1000;
558                 //wrq->value / 1024;
559
560         }
561         switch (wrqu->power.flags & IW_POWER_MODE) {
562         case IW_POWER_UNICAST_R:
563                 ieee->ps = IEEE80211_PS_UNICAST;
564                 break;
565         case IW_POWER_MULTICAST_R:
566                 ieee->ps = IEEE80211_PS_MBCAST;
567                 break;
568         case IW_POWER_ALL_R:
569                 ieee->ps = IEEE80211_PS_UNICAST | IEEE80211_PS_MBCAST;
570                 break;
571
572         case IW_POWER_ON:
573         //      ieee->ps = IEEE80211_PS_DISABLED;
574                 break;
575
576         default:
577                 ret = -EINVAL;
578                 goto exit;
579
580         }
581 exit:
582         up(&ieee->wx_sem);
583         return ret;
584
585 }
586
587 /* this is stolen from hostap */
588 int ieee80211_wx_get_power(struct ieee80211_device *ieee,
589                                  struct iw_request_info *info,
590                                  union iwreq_data *wrqu, char *extra)
591 {
592         int ret =0;
593
594         down(&ieee->wx_sem);
595
596         if(ieee->ps == IEEE80211_PS_DISABLED){
597                 wrqu->power.disabled = 1;
598                 goto exit;
599         }
600
601         wrqu->power.disabled = 0;
602
603         if ((wrqu->power.flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
604                 wrqu->power.flags = IW_POWER_TIMEOUT;
605                 wrqu->power.value = ieee->ps_timeout * 1000;
606         } else {
607 //              ret = -EOPNOTSUPP;
608 //              goto exit;
609                 wrqu->power.flags = IW_POWER_PERIOD;
610                 wrqu->power.value = ieee->ps_period * 1000;
611 //ieee->current_network.dtim_period * ieee->current_network.beacon_interval * 1024;
612         }
613
614        if ((ieee->ps & (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST)) == (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST))
615                 wrqu->power.flags |= IW_POWER_ALL_R;
616         else if (ieee->ps & IEEE80211_PS_MBCAST)
617                 wrqu->power.flags |= IW_POWER_MULTICAST_R;
618         else
619                 wrqu->power.flags |= IW_POWER_UNICAST_R;
620
621 exit:
622         up(&ieee->wx_sem);
623         return ret;
624
625 }