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)
113 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
115 if (ieee->iw_mode == IW_MODE_MONITOR)
118 /* We want avoid to give to the user inconsistent infos*/
119 spin_lock_irqsave(&ieee->lock, flags);
121 if (ieee->state != IEEE80211_LINKED &&
122 ieee->state != IEEE80211_LINKED_SCANNING &&
125 memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);
127 memcpy(wrqu->ap_addr.sa_data,
128 ieee->current_network.bssid, ETH_ALEN);
130 spin_unlock_irqrestore(&ieee->lock, flags);
136 int ieee80211_wx_set_wap(struct ieee80211_device *ieee,
137 struct iw_request_info *info,
138 union iwreq_data *awrq,
143 u8 zero[] = {0,0,0,0,0,0};
146 short ifup = ieee->proto_started;//dev->flags & IFF_UP;
147 struct sockaddr *temp = (struct sockaddr *)awrq;
149 ieee->sync_scan_hurryup = 1;
152 /* use ifconfig hw ether */
153 if (ieee->iw_mode == IW_MODE_MASTER){
158 if (temp->sa_family != ARPHRD_ETHER){
164 ieee80211_stop_protocol(ieee);
166 /* just to avoid to give inconsistent infos in the
167 * get wx method. not really needed otherwise
169 spin_lock_irqsave(&ieee->lock, flags);
171 memcpy(ieee->current_network.bssid, temp->sa_data, ETH_ALEN);
172 ieee->wap_set = memcmp(temp->sa_data, zero,ETH_ALEN)!=0;
174 spin_unlock_irqrestore(&ieee->lock, flags);
177 ieee80211_start_protocol(ieee);
183 int ieee80211_wx_get_essid(struct ieee80211_device *ieee, struct iw_request_info *a,union iwreq_data *wrqu,char *b)
188 if (ieee->iw_mode == IW_MODE_MONITOR)
191 /* We want avoid to give to the user inconsistent infos*/
192 spin_lock_irqsave(&ieee->lock, flags);
194 if (ieee->current_network.ssid[0] == '\0' ||
195 ieee->current_network.ssid_len == 0){
200 if (ieee->state != IEEE80211_LINKED &&
201 ieee->state != IEEE80211_LINKED_SCANNING &&
202 ieee->ssid_set == 0){
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;
212 spin_unlock_irqrestore(&ieee->lock, flags);
218 int ieee80211_wx_set_rate(struct ieee80211_device *ieee,
219 struct iw_request_info *info,
220 union iwreq_data *wrqu, char *extra)
223 u32 target_rate = wrqu->bitrate.value;
225 ieee->rate = target_rate/100000;
226 //FIXME: we might want to limit rate also in management protocols.
232 int ieee80211_wx_get_rate(struct ieee80211_device *ieee,
233 struct iw_request_info *info,
234 union iwreq_data *wrqu, char *extra)
238 //printk("===>mode:%d, halfNmode:%d\n", ieee->mode, ieee->bHalfWirelessN24GMode);
239 if (ieee->mode & (IEEE_A | IEEE_B | IEEE_G))
240 tmp_rate = ieee->rate;
241 else if (ieee->mode & IEEE_N_5G)
243 else if (ieee->mode & IEEE_N_24G)
245 if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
246 tmp_rate = HTHalfMcsToDataRate(ieee, 15);
248 tmp_rate = HTMcsToDataRate(ieee, 15);
251 tmp_rate = TxCountToDataRate(ieee, ieee->softmac_stats.CurrentShowTxate);
254 wrqu->bitrate.value = tmp_rate * 500000;
260 int ieee80211_wx_set_rts(struct ieee80211_device *ieee,
261 struct iw_request_info *info,
262 union iwreq_data *wrqu, char *extra)
264 if (wrqu->rts.disabled || !wrqu->rts.fixed)
265 ieee->rts = DEFAULT_RTS_THRESHOLD;
268 if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
269 wrqu->rts.value > MAX_RTS_THRESHOLD)
271 ieee->rts = wrqu->rts.value;
276 int ieee80211_wx_get_rts(struct ieee80211_device *ieee,
277 struct iw_request_info *info,
278 union iwreq_data *wrqu, char *extra)
280 wrqu->rts.value = ieee->rts;
281 wrqu->rts.fixed = 0; /* no auto select */
282 wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
285 int ieee80211_wx_set_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
286 union iwreq_data *wrqu, char *b)
289 ieee->sync_scan_hurryup = 1;
293 if (wrqu->mode == ieee->iw_mode)
296 if (wrqu->mode == IW_MODE_MONITOR){
298 ieee->dev->type = ARPHRD_IEEE80211;
300 ieee->dev->type = ARPHRD_ETHER;
303 if (!ieee->proto_started){
304 ieee->iw_mode = wrqu->mode;
306 ieee80211_stop_protocol(ieee);
307 ieee->iw_mode = wrqu->mode;
308 ieee80211_start_protocol(ieee);
316 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
317 void ieee80211_wx_sync_scan_wq(struct work_struct *work)
319 struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, wx_sync_scan_wq);
321 void ieee80211_wx_sync_scan_wq(struct ieee80211_device *ieee)
325 HT_EXTCHNL_OFFSET chan_offset=0;
326 HT_CHANNEL_WIDTH bandwidth=0;
328 static int count = 0;
329 chan = ieee->current_network.channel;
330 netif_carrier_off(ieee->dev);
332 if (ieee->data_hard_stop)
333 ieee->data_hard_stop(ieee->dev);
335 ieee80211_stop_send_beacons(ieee);
337 ieee->state = IEEE80211_LINKED_SCANNING;
338 ieee->link_change(ieee->dev);
340 ieee->InitialGainHandler(ieee->dev,IG_Backup);
342 #if(RTL8192S_DISABLE_FW_DM == 0)
343 if (ieee->SetFwCmdHandler)
345 ieee->SetFwCmdHandler(ieee->dev, FW_CMD_DIG_HALT);
346 ieee->SetFwCmdHandler(ieee->dev, FW_CMD_HIGH_PWR_DISABLE);
349 if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT && ieee->pHTInfo->bCurBW40MHz) {
351 chan_offset = ieee->pHTInfo->CurSTAExtChnlOffset;
352 bandwidth = (HT_CHANNEL_WIDTH)ieee->pHTInfo->bCurBW40MHz;
353 printk("Scan in 40M, force to 20M first:%d, %d\n", chan_offset, bandwidth);
354 ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
356 ieee80211_start_scan_syncro(ieee);
358 printk("Scan in 20M, back to 40M\n");
359 if (chan_offset == HT_EXTCHNL_OFFSET_UPPER)
360 ieee->set_chan(ieee->dev, chan + 2);
361 else if (chan_offset == HT_EXTCHNL_OFFSET_LOWER)
362 ieee->set_chan(ieee->dev, chan - 2);
364 ieee->set_chan(ieee->dev, chan);
365 ieee->SetBWModeHandler(ieee->dev, bandwidth, chan_offset);
367 ieee->set_chan(ieee->dev, chan);
371 ieee->InitialGainHandler(ieee->dev,IG_Restore);
373 #if(RTL8192S_DISABLE_FW_DM == 0)
374 if (ieee->SetFwCmdHandler)
376 ieee->SetFwCmdHandler(ieee->dev, FW_CMD_DIG_RESUME);
377 ieee->SetFwCmdHandler(ieee->dev, FW_CMD_HIGH_PWR_ENABLE);
380 ieee->state = IEEE80211_LINKED;
381 ieee->link_change(ieee->dev);
382 // To prevent the immediately calling watch_dog after scan.
383 if(ieee->LinkDetectInfo.NumRecvBcnInPeriod==0||ieee->LinkDetectInfo.NumRecvDataInPeriod==0 )
385 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
386 ieee->LinkDetectInfo.NumRecvDataInPeriod= 1;
388 if (ieee->data_hard_resume)
389 ieee->data_hard_resume(ieee->dev);
391 if(ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
392 ieee80211_start_send_beacons(ieee);
394 netif_carrier_on(ieee->dev);
400 int ieee80211_wx_set_scan(struct ieee80211_device *ieee, struct iw_request_info *a,
401 union iwreq_data *wrqu, char *b)
407 if (ieee->iw_mode == IW_MODE_MONITOR || !(ieee->proto_started)){
412 if ( ieee->state == IEEE80211_LINKED){
413 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
414 queue_work(ieee->wq, &ieee->wx_sync_scan_wq);
416 schedule_task(&ieee->wx_sync_scan_wq);
418 /* intentionally forget to up sem */
427 int ieee80211_wx_set_essid(struct ieee80211_device *ieee,
428 struct iw_request_info *a,
429 union iwreq_data *wrqu, char *extra)
436 ieee->sync_scan_hurryup = 1;
439 proto_started = ieee->proto_started;
441 if (wrqu->essid.length > IW_ESSID_MAX_SIZE){
446 if (ieee->iw_mode == IW_MODE_MONITOR){
452 ieee80211_stop_protocol(ieee);
455 /* this is just to be sure that the GET wx callback
456 * has consisten infos. not needed otherwise
458 spin_lock_irqsave(&ieee->lock, flags);
460 if (wrqu->essid.flags && wrqu->essid.length) {
461 //first flush current network.ssid
462 len = ((wrqu->essid.length-1) < IW_ESSID_MAX_SIZE) ? (wrqu->essid.length-1) : IW_ESSID_MAX_SIZE;
463 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
464 strncpy(ieee->current_network.ssid, extra, len);
465 ieee->current_network.ssid_len = len;
469 for (i=0; i<len; i++)
470 printk("%c ", extra[i]);
475 strncpy(ieee->current_network.ssid, extra, len+1);
476 ieee->current_network.ssid_len = len+1;
480 for (i=0; i<len + 1; i++)
481 printk("%c ", extra[i]);
490 ieee->current_network.ssid[0] = '\0';
491 ieee->current_network.ssid_len = 0;
493 spin_unlock_irqrestore(&ieee->lock, flags);
496 ieee80211_start_protocol(ieee);
502 int ieee80211_wx_get_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
503 union iwreq_data *wrqu, char *b)
506 wrqu->mode = ieee->iw_mode;
510 int ieee80211_wx_set_rawtx(struct ieee80211_device *ieee,
511 struct iw_request_info *info,
512 union iwreq_data *wrqu, char *extra)
515 int *parms = (int *)extra;
516 int enable = (parms[0] > 0);
517 short prev = ieee->raw_tx;
526 printk(KERN_INFO"raw TX is %s\n",
527 ieee->raw_tx ? "enabled" : "disabled");
529 if(ieee->iw_mode == IW_MODE_MONITOR)
531 if(prev == 0 && ieee->raw_tx){
532 if (ieee->data_hard_resume)
533 ieee->data_hard_resume(ieee->dev);
535 netif_carrier_on(ieee->dev);
538 if(prev && ieee->raw_tx == 1)
539 netif_carrier_off(ieee->dev);
547 int ieee80211_wx_get_name(struct ieee80211_device *ieee,
548 struct iw_request_info *info,
549 union iwreq_data *wrqu, char *extra)
551 strcpy(wrqu->name, "802.11");
552 if(ieee->modulation & IEEE80211_CCK_MODULATION){
553 strcat(wrqu->name, "b");
554 if(ieee->modulation & IEEE80211_OFDM_MODULATION)
555 strcat(wrqu->name, "/g");
556 }else if(ieee->modulation & IEEE80211_OFDM_MODULATION)
557 strcat(wrqu->name, "g");
558 if (ieee->mode & (IEEE_N_24G | IEEE_N_5G))
559 strcat(wrqu->name, "/n");
561 if((ieee->state == IEEE80211_LINKED) ||
562 (ieee->state == IEEE80211_LINKED_SCANNING))
563 strcat(wrqu->name," linked");
564 else if(ieee->state != IEEE80211_NOLINK)
565 strcat(wrqu->name," link..");
572 /* this is mostly stolen from hostap */
573 int ieee80211_wx_set_power(struct ieee80211_device *ieee,
574 struct iw_request_info *info,
575 union iwreq_data *wrqu, char *extra)
580 (!ieee->sta_wake_up) ||
581 // (!ieee->ps_request_tx_ack) ||
582 (!ieee->enter_sleep_state) ||
583 (!ieee->ps_is_queue_empty)){
585 // printk("ERROR. PS mode is tryied to be use but driver missed a callback\n\n");
592 if (wrqu->power.disabled){
593 ieee->ps = IEEE80211_PS_DISABLED;
596 if (wrqu->power.flags & IW_POWER_TIMEOUT) {
597 //ieee->ps_period = wrqu->power.value / 1000;
598 ieee->ps_timeout = wrqu->power.value / 1000;
601 if (wrqu->power.flags & IW_POWER_PERIOD) {
603 //ieee->ps_timeout = wrqu->power.value / 1000;
604 ieee->ps_period = wrqu->power.value / 1000;
608 switch (wrqu->power.flags & IW_POWER_MODE) {
609 case IW_POWER_UNICAST_R:
610 ieee->ps = IEEE80211_PS_UNICAST;
612 case IW_POWER_MULTICAST_R:
613 ieee->ps = IEEE80211_PS_MBCAST;
616 ieee->ps = IEEE80211_PS_UNICAST | IEEE80211_PS_MBCAST;
620 // ieee->ps = IEEE80211_PS_DISABLED;
634 /* this is stolen from hostap */
635 int ieee80211_wx_get_power(struct ieee80211_device *ieee,
636 struct iw_request_info *info,
637 union iwreq_data *wrqu, char *extra)
643 if(ieee->ps == IEEE80211_PS_DISABLED){
644 wrqu->power.disabled = 1;
648 wrqu->power.disabled = 0;
650 if ((wrqu->power.flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
651 wrqu->power.flags = IW_POWER_TIMEOUT;
652 wrqu->power.value = ieee->ps_timeout * 1000;
654 // ret = -EOPNOTSUPP;
656 wrqu->power.flags = IW_POWER_PERIOD;
657 wrqu->power.value = ieee->ps_period * 1000;
658 //ieee->current_network.dtim_period * ieee->current_network.beacon_interval * 1024;
661 if ((ieee->ps & (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST)) == (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST))
662 wrqu->power.flags |= IW_POWER_ALL_R;
663 else if (ieee->ps & IEEE80211_PS_MBCAST)
664 wrqu->power.flags |= IW_POWER_MULTICAST_R;
666 wrqu->power.flags |= IW_POWER_UNICAST_R;
673 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
674 EXPORT_SYMBOL(ieee80211_wx_get_essid);
675 EXPORT_SYMBOL(ieee80211_wx_set_essid);
676 EXPORT_SYMBOL(ieee80211_wx_set_rate);
677 EXPORT_SYMBOL(ieee80211_wx_get_rate);
678 EXPORT_SYMBOL(ieee80211_wx_set_wap);
679 EXPORT_SYMBOL(ieee80211_wx_get_wap);
680 EXPORT_SYMBOL(ieee80211_wx_set_mode);
681 EXPORT_SYMBOL(ieee80211_wx_get_mode);
682 EXPORT_SYMBOL(ieee80211_wx_set_scan);
683 EXPORT_SYMBOL(ieee80211_wx_get_freq);
684 EXPORT_SYMBOL(ieee80211_wx_set_freq);
685 EXPORT_SYMBOL(ieee80211_wx_set_rawtx);
686 EXPORT_SYMBOL(ieee80211_wx_get_name);
687 EXPORT_SYMBOL(ieee80211_wx_set_power);
688 EXPORT_SYMBOL(ieee80211_wx_get_power);
689 EXPORT_SYMBOL(ieee80211_wlan_frequencies);
690 EXPORT_SYMBOL(ieee80211_wx_set_rts);
691 EXPORT_SYMBOL(ieee80211_wx_get_rts);
693 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_essid);
694 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_essid);
695 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_rate);
696 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_rate);
697 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_wap);
698 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_wap);
699 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_mode);
700 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_mode);
701 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_scan);
702 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_freq);
703 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_freq);
704 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_rawtx);
705 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_name);
706 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_power);
707 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_power);
708 EXPORT_SYMBOL_NOVERS(ieee80211_wlan_frequencies);
709 EXPORT_SYMBOL_NOVERS(ieee80211_wx_set_rts);
710 EXPORT_SYMBOL_NOVERS(ieee80211_wx_get_rts);