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)
237 tmp_rate = TxCountToDataRate(ieee, ieee->softmac_stats.CurrentShowTxate);
239 wrqu->bitrate.value = tmp_rate * 500000;
245 int ieee80211_wx_set_rts(struct ieee80211_device *ieee,
246 struct iw_request_info *info,
247 union iwreq_data *wrqu, char *extra)
249 if (wrqu->rts.disabled || !wrqu->rts.fixed)
250 ieee->rts = DEFAULT_RTS_THRESHOLD;
253 if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
254 wrqu->rts.value > MAX_RTS_THRESHOLD)
256 ieee->rts = wrqu->rts.value;
261 int ieee80211_wx_get_rts(struct ieee80211_device *ieee,
262 struct iw_request_info *info,
263 union iwreq_data *wrqu, char *extra)
265 wrqu->rts.value = ieee->rts;
266 wrqu->rts.fixed = 0; /* no auto select */
267 wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
270 int ieee80211_wx_set_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
271 union iwreq_data *wrqu, char *b)
274 ieee->sync_scan_hurryup = 1;
278 if (wrqu->mode == ieee->iw_mode)
281 if (wrqu->mode == IW_MODE_MONITOR){
283 ieee->dev->type = ARPHRD_IEEE80211;
285 ieee->dev->type = ARPHRD_ETHER;
288 if (!ieee->proto_started){
289 ieee->iw_mode = wrqu->mode;
291 ieee80211_stop_protocol(ieee);
292 ieee->iw_mode = wrqu->mode;
293 ieee80211_start_protocol(ieee);
301 void ieee80211_wx_sync_scan_wq(struct work_struct *work)
303 struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, wx_sync_scan_wq);
305 HT_EXTCHNL_OFFSET chan_offset=0;
306 HT_CHANNEL_WIDTH bandwidth=0;
308 static int count = 0;
309 chan = ieee->current_network.channel;
310 netif_carrier_off(ieee->dev);
312 if (ieee->data_hard_stop)
313 ieee->data_hard_stop(ieee->dev);
315 ieee80211_stop_send_beacons(ieee);
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) {
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);
327 ieee80211_start_scan_syncro(ieee);
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);
335 ieee->set_chan(ieee->dev, chan);
336 ieee->SetBWModeHandler(ieee->dev, bandwidth, chan_offset);
338 ieee->set_chan(ieee->dev, chan);
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 )
347 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
348 ieee->LinkDetectInfo.NumRecvDataInPeriod= 1;
350 if (ieee->data_hard_resume)
351 ieee->data_hard_resume(ieee->dev);
353 if(ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
354 ieee80211_start_send_beacons(ieee);
356 netif_carrier_on(ieee->dev);
362 int ieee80211_wx_set_scan(struct ieee80211_device *ieee, struct iw_request_info *a,
363 union iwreq_data *wrqu, char *b)
369 if (ieee->iw_mode == IW_MODE_MONITOR || !(ieee->proto_started)){
374 if ( ieee->state == IEEE80211_LINKED){
375 queue_work(ieee->wq, &ieee->wx_sync_scan_wq);
376 /* intentionally forget to up sem */
385 int ieee80211_wx_set_essid(struct ieee80211_device *ieee,
386 struct iw_request_info *a,
387 union iwreq_data *wrqu, char *extra)
394 ieee->sync_scan_hurryup = 1;
397 proto_started = ieee->proto_started;
399 if (wrqu->essid.length > IW_ESSID_MAX_SIZE){
404 if (ieee->iw_mode == IW_MODE_MONITOR){
410 ieee80211_stop_protocol(ieee);
413 /* this is just to be sure that the GET wx callback
414 * has consisten infos. not needed otherwise
416 spin_lock_irqsave(&ieee->lock, flags);
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;
427 ieee->current_network.ssid[0] = '\0';
428 ieee->current_network.ssid_len = 0;
430 spin_unlock_irqrestore(&ieee->lock, flags);
433 ieee80211_start_protocol(ieee);
439 int ieee80211_wx_get_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
440 union iwreq_data *wrqu, char *b)
443 wrqu->mode = ieee->iw_mode;
447 int ieee80211_wx_set_rawtx(struct ieee80211_device *ieee,
448 struct iw_request_info *info,
449 union iwreq_data *wrqu, char *extra)
452 int *parms = (int *)extra;
453 int enable = (parms[0] > 0);
454 short prev = ieee->raw_tx;
463 printk(KERN_INFO"raw TX is %s\n",
464 ieee->raw_tx ? "enabled" : "disabled");
466 if(ieee->iw_mode == IW_MODE_MONITOR)
468 if(prev == 0 && ieee->raw_tx){
469 if (ieee->data_hard_resume)
470 ieee->data_hard_resume(ieee->dev);
472 netif_carrier_on(ieee->dev);
475 if(prev && ieee->raw_tx == 1)
476 netif_carrier_off(ieee->dev);
484 int ieee80211_wx_get_name(struct ieee80211_device *ieee,
485 struct iw_request_info *info,
486 union iwreq_data *wrqu, char *extra)
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");
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..");
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)
517 if (wrqu->power.disabled){
518 ieee->ps = IEEE80211_PS_DISABLED;
521 if (wrqu->power.flags & IW_POWER_TIMEOUT) {
522 //ieee->ps_period = wrqu->power.value / 1000;
523 ieee->ps_timeout = wrqu->power.value / 1000;
526 if (wrqu->power.flags & IW_POWER_PERIOD) {
528 //ieee->ps_timeout = wrqu->power.value / 1000;
529 ieee->ps_period = wrqu->power.value / 1000;
533 switch (wrqu->power.flags & IW_POWER_MODE) {
534 case IW_POWER_UNICAST_R:
535 ieee->ps = IEEE80211_PS_UNICAST;
537 case IW_POWER_MULTICAST_R:
538 ieee->ps = IEEE80211_PS_MBCAST;
541 ieee->ps = IEEE80211_PS_UNICAST | IEEE80211_PS_MBCAST;
545 // ieee->ps = IEEE80211_PS_DISABLED;
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)
568 if(ieee->ps == IEEE80211_PS_DISABLED){
569 wrqu->power.disabled = 1;
573 wrqu->power.disabled = 0;
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;
579 // ret = -EOPNOTSUPP;
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;
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;
591 wrqu->power.flags |= IW_POWER_UNICAST_R;
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);