1 /* IEEE 802.11 SoftMAC layer
2 * Copyright (c) 2005 Andrea Merello <andreamrl@tiscali.it>
4 * Mostly extracted from the rtl8180-sa2400 driver for the
5 * in-kernel generic ieee802.11 stack.
7 * Some pieces of code might be stolen from ipw2100 driver
8 * copyright of who own it's copyright ;-)
10 * PS wx handler mostly stolen from hostap, copyright who
11 * own it's copyright ;-)
13 * released under the GPL
17 #include "ieee80211.h"
21 /* FIXME: add A freqs */
23 const long ieee80211_wlan_frequencies[] = {
24 2412, 2417, 2422, 2427,
25 2432, 2437, 2442, 2447,
26 2452, 2457, 2462, 2467,
31 int ieee80211_wx_set_freq(struct ieee80211_device *ieee, struct iw_request_info *a,
32 union iwreq_data *wrqu, char *b)
35 struct iw_freq *fwrq = & wrqu->freq;
39 if(ieee->iw_mode == IW_MODE_INFRA){
44 /* if setting by freq convert to channel */
46 if ((fwrq->m >= (int) 2.412e8 &&
47 fwrq->m <= (int) 2.487e8)) {
48 int f = fwrq->m / 100000;
51 while ((c < 14) && (f != ieee80211_wlan_frequencies[c]))
54 /* hack to fall through */
60 if (fwrq->e > 0 || fwrq->m > 14 || fwrq->m < 1 ){
64 }else { /* Set the channel */
67 if (!(GET_DOT11D_INFO(ieee)->channel_map)[fwrq->m]) {
72 ieee->current_network.channel = fwrq->m;
73 ieee->set_chan(ieee->dev, ieee->current_network.channel);
75 if(ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
76 if(ieee->state == IEEE80211_LINKED){
78 ieee80211_stop_send_beacons(ieee);
79 ieee80211_start_send_beacons(ieee);
90 int ieee80211_wx_get_freq(struct ieee80211_device *ieee,
91 struct iw_request_info *a,
92 union iwreq_data *wrqu, char *b)
94 struct iw_freq *fwrq = & wrqu->freq;
96 if (ieee->current_network.channel == 0)
98 //NM 0.7.0 will not accept channel any more.
99 fwrq->m = ieee80211_wlan_frequencies[ieee->current_network.channel-1] * 100000;
101 // fwrq->m = ieee->current_network.channel;
107 int ieee80211_wx_get_wap(struct ieee80211_device *ieee,
108 struct iw_request_info *info,
109 union iwreq_data *wrqu, char *extra)
112 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
114 if (ieee->iw_mode == IW_MODE_MONITOR)
117 /* We want avoid to give to the user inconsistent infos*/
118 spin_lock_irqsave(&ieee->lock, flags);
120 if (ieee->state != IEEE80211_LINKED &&
121 ieee->state != IEEE80211_LINKED_SCANNING &&
124 memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);
126 memcpy(wrqu->ap_addr.sa_data,
127 ieee->current_network.bssid, ETH_ALEN);
129 spin_unlock_irqrestore(&ieee->lock, flags);
135 int ieee80211_wx_set_wap(struct ieee80211_device *ieee,
136 struct iw_request_info *info,
137 union iwreq_data *awrq,
142 u8 zero[] = {0,0,0,0,0,0};
145 short ifup = ieee->proto_started;//dev->flags & IFF_UP;
146 struct sockaddr *temp = (struct sockaddr *)awrq;
148 ieee->sync_scan_hurryup = 1;
151 /* use ifconfig hw ether */
152 if (ieee->iw_mode == IW_MODE_MASTER){
157 if (temp->sa_family != ARPHRD_ETHER){
163 ieee80211_stop_protocol(ieee);
165 /* just to avoid to give inconsistent infos in the
166 * get wx method. not really needed otherwise
168 spin_lock_irqsave(&ieee->lock, flags);
170 memcpy(ieee->current_network.bssid, temp->sa_data, ETH_ALEN);
171 ieee->wap_set = memcmp(temp->sa_data, zero,ETH_ALEN)!=0;
173 spin_unlock_irqrestore(&ieee->lock, flags);
176 ieee80211_start_protocol(ieee);
182 int ieee80211_wx_get_essid(struct ieee80211_device *ieee, struct iw_request_info *a,union iwreq_data *wrqu,char *b)
187 if (ieee->iw_mode == IW_MODE_MONITOR)
190 /* We want avoid to give to the user inconsistent infos*/
191 spin_lock_irqsave(&ieee->lock, flags);
193 if (ieee->current_network.ssid[0] == '\0' ||
194 ieee->current_network.ssid_len == 0){
199 if (ieee->state != IEEE80211_LINKED &&
200 ieee->state != IEEE80211_LINKED_SCANNING &&
201 ieee->ssid_set == 0){
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;
211 spin_unlock_irqrestore(&ieee->lock, flags);
217 int ieee80211_wx_set_rate(struct ieee80211_device *ieee,
218 struct iw_request_info *info,
219 union iwreq_data *wrqu, char *extra)
222 u32 target_rate = wrqu->bitrate.value;
224 ieee->rate = target_rate/100000;
225 //FIXME: we might want to limit rate also in management protocols.
231 int ieee80211_wx_get_rate(struct ieee80211_device *ieee,
232 struct iw_request_info *info,
233 union iwreq_data *wrqu, char *extra)
237 printk("===>mode:%d, halfNmode:%d\n", ieee->mode, ieee->bHalfWirelessN24GMode);
238 if (ieee->mode & (IEEE_A | IEEE_B | IEEE_G))
239 tmp_rate = ieee->rate;
240 else if (ieee->mode & IEEE_N_5G)
242 else if (ieee->mode & IEEE_N_24G)
244 if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
245 tmp_rate = HTHalfMcsToDataRate(ieee, 15);
247 tmp_rate = HTMcsToDataRate(ieee, 15);
250 tmp_rate = TxCountToDataRate(ieee, ieee->softmac_stats.CurrentShowTxate);
253 wrqu->bitrate.value = tmp_rate * 500000;
259 int ieee80211_wx_set_rts(struct ieee80211_device *ieee,
260 struct iw_request_info *info,
261 union iwreq_data *wrqu, char *extra)
263 if (wrqu->rts.disabled || !wrqu->rts.fixed)
264 ieee->rts = DEFAULT_RTS_THRESHOLD;
267 if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
268 wrqu->rts.value > MAX_RTS_THRESHOLD)
270 ieee->rts = wrqu->rts.value;
275 int ieee80211_wx_get_rts(struct ieee80211_device *ieee,
276 struct iw_request_info *info,
277 union iwreq_data *wrqu, char *extra)
279 wrqu->rts.value = ieee->rts;
280 wrqu->rts.fixed = 0; /* no auto select */
281 wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
284 int ieee80211_wx_set_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
285 union iwreq_data *wrqu, char *b)
288 ieee->sync_scan_hurryup = 1;
292 if (wrqu->mode == ieee->iw_mode)
295 if (wrqu->mode == IW_MODE_MONITOR){
297 ieee->dev->type = ARPHRD_IEEE80211;
299 ieee->dev->type = ARPHRD_ETHER;
302 if (!ieee->proto_started){
303 ieee->iw_mode = wrqu->mode;
305 ieee80211_stop_protocol(ieee);
306 ieee->iw_mode = wrqu->mode;
307 ieee80211_start_protocol(ieee);
315 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
316 void ieee80211_wx_sync_scan_wq(struct work_struct *work)
318 struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, wx_sync_scan_wq);
320 void ieee80211_wx_sync_scan_wq(struct ieee80211_device *ieee)
324 HT_EXTCHNL_OFFSET chan_offset=0;
325 HT_CHANNEL_WIDTH bandwidth=0;
327 static int count = 0;
328 chan = ieee->current_network.channel;
329 netif_carrier_off(ieee->dev);
331 if (ieee->data_hard_stop)
332 ieee->data_hard_stop(ieee->dev);
334 ieee80211_stop_send_beacons(ieee);
336 ieee->state = IEEE80211_LINKED_SCANNING;
337 ieee->link_change(ieee->dev);
338 ieee->InitialGainHandler(ieee->dev,IG_Backup);
339 if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT && ieee->pHTInfo->bCurBW40MHz) {
341 chan_offset = ieee->pHTInfo->CurSTAExtChnlOffset;
342 bandwidth = (HT_CHANNEL_WIDTH)ieee->pHTInfo->bCurBW40MHz;
343 printk("Scan in 40M, force to 20M first:%d, %d\n", chan_offset, bandwidth);
344 ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
346 ieee80211_start_scan_syncro(ieee);
348 printk("Scan in 20M, back to 40M\n");
349 if (chan_offset == HT_EXTCHNL_OFFSET_UPPER)
350 ieee->set_chan(ieee->dev, chan + 2);
351 else if (chan_offset == HT_EXTCHNL_OFFSET_LOWER)
352 ieee->set_chan(ieee->dev, chan - 2);
354 ieee->set_chan(ieee->dev, chan);
355 ieee->SetBWModeHandler(ieee->dev, bandwidth, chan_offset);
357 ieee->set_chan(ieee->dev, chan);
360 ieee->InitialGainHandler(ieee->dev,IG_Restore);
361 ieee->state = IEEE80211_LINKED;
362 ieee->link_change(ieee->dev);
363 // To prevent the immediately calling watch_dog after scan.
364 if(ieee->LinkDetectInfo.NumRecvBcnInPeriod==0||ieee->LinkDetectInfo.NumRecvDataInPeriod==0 )
366 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
367 ieee->LinkDetectInfo.NumRecvDataInPeriod= 1;
369 if (ieee->data_hard_resume)
370 ieee->data_hard_resume(ieee->dev);
372 if(ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
373 ieee80211_start_send_beacons(ieee);
375 netif_carrier_on(ieee->dev);
381 int ieee80211_wx_set_scan(struct ieee80211_device *ieee, struct iw_request_info *a,
382 union iwreq_data *wrqu, char *b)
388 if (ieee->iw_mode == IW_MODE_MONITOR || !(ieee->proto_started)){
393 if ( ieee->state == IEEE80211_LINKED){
394 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
395 queue_work(ieee->wq, &ieee->wx_sync_scan_wq);
397 schedule_task(&ieee->wx_sync_scan_wq);
399 /* intentionally forget to up sem */
408 int ieee80211_wx_set_essid(struct ieee80211_device *ieee,
409 struct iw_request_info *a,
410 union iwreq_data *wrqu, char *extra)
417 ieee->sync_scan_hurryup = 1;
420 proto_started = ieee->proto_started;
422 if (wrqu->essid.length > IW_ESSID_MAX_SIZE){
427 if (ieee->iw_mode == IW_MODE_MONITOR){
433 ieee80211_stop_protocol(ieee);
436 /* this is just to be sure that the GET wx callback
437 * has consisten infos. not needed otherwise
439 spin_lock_irqsave(&ieee->lock, flags);
441 if (wrqu->essid.flags && wrqu->essid.length) {
442 //first flush current network.ssid
443 len = ((wrqu->essid.length-1) < IW_ESSID_MAX_SIZE) ? (wrqu->essid.length-1) : IW_ESSID_MAX_SIZE;
444 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
445 strncpy(ieee->current_network.ssid, extra, len);
446 ieee->current_network.ssid_len = len;
450 for (i=0; i<len; i++)
451 printk("%c ", extra[i]);
456 strncpy(ieee->current_network.ssid, extra, len+1);
457 ieee->current_network.ssid_len = len+1;
461 for (i=0; i<len + 1; i++)
462 printk("%c ", extra[i]);
471 ieee->current_network.ssid[0] = '\0';
472 ieee->current_network.ssid_len = 0;
474 spin_unlock_irqrestore(&ieee->lock, flags);
477 ieee80211_start_protocol(ieee);
483 int ieee80211_wx_get_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
484 union iwreq_data *wrqu, char *b)
487 wrqu->mode = ieee->iw_mode;
491 int ieee80211_wx_set_rawtx(struct ieee80211_device *ieee,
492 struct iw_request_info *info,
493 union iwreq_data *wrqu, char *extra)
496 int *parms = (int *)extra;
497 int enable = (parms[0] > 0);
498 short prev = ieee->raw_tx;
507 printk(KERN_INFO"raw TX is %s\n",
508 ieee->raw_tx ? "enabled" : "disabled");
510 if(ieee->iw_mode == IW_MODE_MONITOR)
512 if(prev == 0 && ieee->raw_tx){
513 if (ieee->data_hard_resume)
514 ieee->data_hard_resume(ieee->dev);
516 netif_carrier_on(ieee->dev);
519 if(prev && ieee->raw_tx == 1)
520 netif_carrier_off(ieee->dev);
528 int ieee80211_wx_get_name(struct ieee80211_device *ieee,
529 struct iw_request_info *info,
530 union iwreq_data *wrqu, char *extra)
532 strcpy(wrqu->name, "802.11");
533 if(ieee->modulation & IEEE80211_CCK_MODULATION){
534 strcat(wrqu->name, "b");
535 if(ieee->modulation & IEEE80211_OFDM_MODULATION)
536 strcat(wrqu->name, "/g");
537 }else if(ieee->modulation & IEEE80211_OFDM_MODULATION)
538 strcat(wrqu->name, "g");
539 if (ieee->mode & (IEEE_N_24G | IEEE_N_5G))
540 strcat(wrqu->name, "/n");
542 if((ieee->state == IEEE80211_LINKED) ||
543 (ieee->state == IEEE80211_LINKED_SCANNING))
544 strcat(wrqu->name," linked");
545 else if(ieee->state != IEEE80211_NOLINK)
546 strcat(wrqu->name," link..");
553 /* this is mostly stolen from hostap */
554 int ieee80211_wx_set_power(struct ieee80211_device *ieee,
555 struct iw_request_info *info,
556 union iwreq_data *wrqu, char *extra)
561 (!ieee->sta_wake_up) ||
562 // (!ieee->ps_request_tx_ack) ||
563 (!ieee->enter_sleep_state) ||
564 (!ieee->ps_is_queue_empty)){
566 // printk("ERROR. PS mode is tryied to be use but driver missed a callback\n\n");
573 if (wrqu->power.disabled){
574 ieee->ps = IEEE80211_PS_DISABLED;
577 if (wrqu->power.flags & IW_POWER_TIMEOUT) {
578 //ieee->ps_period = wrqu->power.value / 1000;
579 ieee->ps_timeout = wrqu->power.value / 1000;
582 if (wrqu->power.flags & IW_POWER_PERIOD) {
584 //ieee->ps_timeout = wrqu->power.value / 1000;
585 ieee->ps_period = wrqu->power.value / 1000;
589 switch (wrqu->power.flags & IW_POWER_MODE) {
590 case IW_POWER_UNICAST_R:
591 ieee->ps = IEEE80211_PS_UNICAST;
593 case IW_POWER_MULTICAST_R:
594 ieee->ps = IEEE80211_PS_MBCAST;
597 ieee->ps = IEEE80211_PS_UNICAST | IEEE80211_PS_MBCAST;
601 // ieee->ps = IEEE80211_PS_DISABLED;
615 /* this is stolen from hostap */
616 int ieee80211_wx_get_power(struct ieee80211_device *ieee,
617 struct iw_request_info *info,
618 union iwreq_data *wrqu, char *extra)
624 if(ieee->ps == IEEE80211_PS_DISABLED){
625 wrqu->power.disabled = 1;
629 wrqu->power.disabled = 0;
631 if ((wrqu->power.flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
632 wrqu->power.flags = IW_POWER_TIMEOUT;
633 wrqu->power.value = ieee->ps_timeout * 1000;
635 // ret = -EOPNOTSUPP;
637 wrqu->power.flags = IW_POWER_PERIOD;
638 wrqu->power.value = ieee->ps_period * 1000;
639 //ieee->current_network.dtim_period * ieee->current_network.beacon_interval * 1024;
642 if ((ieee->ps & (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST)) == (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST))
643 wrqu->power.flags |= IW_POWER_ALL_R;
644 else if (ieee->ps & IEEE80211_PS_MBCAST)
645 wrqu->power.flags |= IW_POWER_MULTICAST_R;
647 wrqu->power.flags |= IW_POWER_UNICAST_R;
654 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
655 //EXPORT_SYMBOL(ieee80211_wx_get_essid);
656 //EXPORT_SYMBOL(ieee80211_wx_set_essid);
657 //EXPORT_SYMBOL(ieee80211_wx_set_rate);
658 //EXPORT_SYMBOL(ieee80211_wx_get_rate);
659 //EXPORT_SYMBOL(ieee80211_wx_set_wap);
660 //EXPORT_SYMBOL(ieee80211_wx_get_wap);
661 //EXPORT_SYMBOL(ieee80211_wx_set_mode);
662 //EXPORT_SYMBOL(ieee80211_wx_get_mode);
663 //EXPORT_SYMBOL(ieee80211_wx_set_scan);
664 //EXPORT_SYMBOL(ieee80211_wx_get_freq);
665 //EXPORT_SYMBOL(ieee80211_wx_set_freq);
666 //EXPORT_SYMBOL(ieee80211_wx_set_rawtx);
667 //EXPORT_SYMBOL(ieee80211_wx_get_name);
668 //EXPORT_SYMBOL(ieee80211_wx_set_power);
669 //EXPORT_SYMBOL(ieee80211_wx_get_power);
670 //EXPORT_SYMBOL(ieee80211_wlan_frequencies);
671 //EXPORT_SYMBOL(ieee80211_wx_set_rts);
672 //EXPORT_SYMBOL(ieee80211_wx_get_rts);
674 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_essid);
675 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_essid);
676 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_rate);
677 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_rate);
678 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_wap);
679 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_wap);
680 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_mode);
681 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_mode);
682 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_scan);
683 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_freq);
684 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_freq);
685 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_rawtx);
686 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_name);
687 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_power);
688 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_power);
689 EXPORT_SYMBOL_NOVERS(ieee80211_wlan_frequencies);
690 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_rts);
691 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_rts);