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, 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;
105 int ieee80211_wx_get_wap(struct ieee80211_device *ieee,
106 struct iw_request_info *info,
107 union iwreq_data *wrqu, char *extra)
110 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
112 if (ieee->iw_mode == IW_MODE_MONITOR)
115 /* We want avoid to give to the user inconsistent infos*/
116 spin_lock_irqsave(&ieee->lock, flags);
118 if (ieee->state != IEEE80211_LINKED &&
119 ieee->state != IEEE80211_LINKED_SCANNING &&
122 memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);
124 memcpy(wrqu->ap_addr.sa_data,
125 ieee->current_network.bssid, ETH_ALEN);
127 spin_unlock_irqrestore(&ieee->lock, flags);
133 int ieee80211_wx_set_wap(struct ieee80211_device *ieee,
134 struct iw_request_info *info,
135 union iwreq_data *awrq,
140 u8 zero[] = {0,0,0,0,0,0};
143 short ifup = ieee->proto_started;//dev->flags & IFF_UP;
144 struct sockaddr *temp = (struct sockaddr *)awrq;
146 ieee->sync_scan_hurryup = 1;
149 /* use ifconfig hw ether */
150 if (ieee->iw_mode == IW_MODE_MASTER){
155 if (temp->sa_family != ARPHRD_ETHER){
161 ieee80211_stop_protocol(ieee,true);
163 /* just to avoid to give inconsistent infos in the
164 * get wx method. not really needed otherwise
166 spin_lock_irqsave(&ieee->lock, flags);
168 memcpy(ieee->current_network.bssid, temp->sa_data, ETH_ALEN);
169 ieee->wap_set = memcmp(temp->sa_data, zero,ETH_ALEN)!=0;
171 spin_unlock_irqrestore(&ieee->lock, flags);
174 ieee80211_start_protocol(ieee);
180 int ieee80211_wx_get_essid(struct ieee80211_device *ieee, struct iw_request_info *a,union iwreq_data *wrqu,char *b)
185 if (ieee->iw_mode == IW_MODE_MONITOR)
188 /* We want avoid to give to the user inconsistent infos*/
189 spin_lock_irqsave(&ieee->lock, flags);
191 if (ieee->current_network.ssid[0] == '\0' ||
192 ieee->current_network.ssid_len == 0){
197 if (ieee->state != IEEE80211_LINKED &&
198 ieee->state != IEEE80211_LINKED_SCANNING &&
199 ieee->ssid_set == 0){
203 len = ieee->current_network.ssid_len;
204 wrqu->essid.length = len;
205 strncpy(b,ieee->current_network.ssid,len);
206 wrqu->essid.flags = 1;
209 spin_unlock_irqrestore(&ieee->lock, flags);
215 int ieee80211_wx_set_rate(struct ieee80211_device *ieee,
216 struct iw_request_info *info,
217 union iwreq_data *wrqu, char *extra)
220 u32 target_rate = wrqu->bitrate.value;
222 ieee->rate = target_rate/100000;
223 //FIXME: we might want to limit rate also in management protocols.
229 int ieee80211_wx_get_rate(struct ieee80211_device *ieee,
230 struct iw_request_info *info,
231 union iwreq_data *wrqu, char *extra)
234 tmp_rate = TxCountToDataRate(ieee, ieee->softmac_stats.CurrentShowTxate);
236 wrqu->bitrate.value = tmp_rate * 500000;
242 int ieee80211_wx_set_rts(struct ieee80211_device *ieee,
243 struct iw_request_info *info,
244 union iwreq_data *wrqu, char *extra)
246 if (wrqu->rts.disabled || !wrqu->rts.fixed)
247 ieee->rts = DEFAULT_RTS_THRESHOLD;
250 if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
251 wrqu->rts.value > MAX_RTS_THRESHOLD)
253 ieee->rts = wrqu->rts.value;
258 int ieee80211_wx_get_rts(struct ieee80211_device *ieee,
259 struct iw_request_info *info,
260 union iwreq_data *wrqu, char *extra)
262 wrqu->rts.value = ieee->rts;
263 wrqu->rts.fixed = 0; /* no auto select */
264 wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
267 int ieee80211_wx_set_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
268 union iwreq_data *wrqu, char *b)
271 ieee->sync_scan_hurryup = 1;
275 if (wrqu->mode == ieee->iw_mode)
278 if (wrqu->mode == IW_MODE_MONITOR){
280 ieee->dev->type = ARPHRD_IEEE80211;
282 ieee->dev->type = ARPHRD_ETHER;
285 if (!ieee->proto_started){
286 ieee->iw_mode = wrqu->mode;
288 ieee80211_stop_protocol(ieee,true);
289 ieee->iw_mode = wrqu->mode;
290 ieee80211_start_protocol(ieee);
298 void ieee80211_wx_sync_scan_wq(struct work_struct *work)
300 struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, wx_sync_scan_wq);
302 HT_EXTCHNL_OFFSET chan_offset=0;
303 HT_CHANNEL_WIDTH bandwidth=0;
305 static int count = 0;
306 chan = ieee->current_network.channel;
309 if (ieee->LeisurePSLeave) {
310 ieee->LeisurePSLeave(ieee);
313 /* notify AP to be in PS mode */
314 ieee80211_sta_ps_send_null_frame(ieee, 1);
315 ieee80211_sta_ps_send_null_frame(ieee, 1);
318 if (ieee->data_hard_stop)
319 ieee->data_hard_stop(ieee);
321 ieee80211_stop_send_beacons(ieee);
323 ieee->state = IEEE80211_LINKED_SCANNING;
324 ieee->link_change(ieee);
325 ieee->InitialGainHandler(ieee->dev,IG_Backup);
326 if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT && ieee->pHTInfo->bCurBW40MHz) {
328 chan_offset = ieee->pHTInfo->CurSTAExtChnlOffset;
329 bandwidth = (HT_CHANNEL_WIDTH)ieee->pHTInfo->bCurBW40MHz;
330 printk("Scan in 40M, force to 20M first:%d, %d\n", chan_offset, bandwidth);
331 ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
333 ieee80211_start_scan_syncro(ieee);
335 printk("Scan in 20M, back to 40M\n");
336 if (chan_offset == HT_EXTCHNL_OFFSET_UPPER)
337 ieee->set_chan(ieee, chan + 2);
338 else if (chan_offset == HT_EXTCHNL_OFFSET_LOWER)
339 ieee->set_chan(ieee, chan - 2);
341 ieee->set_chan(ieee, chan);
342 ieee->SetBWModeHandler(ieee->dev, bandwidth, chan_offset);
344 ieee->set_chan(ieee, chan);
347 ieee->InitialGainHandler(ieee->dev,IG_Restore);
348 ieee->state = IEEE80211_LINKED;
349 ieee->link_change(ieee);
352 /* Notify AP that I wake up again */
353 ieee80211_sta_ps_send_null_frame(ieee, 0);
356 // To prevent the immediately calling watch_dog after scan.
357 if(ieee->LinkDetectInfo.NumRecvBcnInPeriod==0||ieee->LinkDetectInfo.NumRecvDataInPeriod==0 )
359 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
360 ieee->LinkDetectInfo.NumRecvDataInPeriod= 1;
362 if (ieee->data_hard_resume)
363 ieee->data_hard_resume(ieee);
365 if(ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
366 ieee80211_start_send_beacons(ieee);
373 int ieee80211_wx_set_scan(struct ieee80211_device *ieee, struct iw_request_info *a,
374 union iwreq_data *wrqu, char *b)
380 if (ieee->iw_mode == IW_MODE_MONITOR || !(ieee->proto_started)){
385 if ( ieee->state == IEEE80211_LINKED){
386 queue_work(ieee->wq, &ieee->wx_sync_scan_wq);
387 /* intentionally forget to up sem */
396 int ieee80211_wx_set_essid(struct ieee80211_device *ieee,
397 struct iw_request_info *a,
398 union iwreq_data *wrqu, char *extra)
405 ieee->sync_scan_hurryup = 1;
408 proto_started = ieee->proto_started;
410 if (wrqu->essid.length > IW_ESSID_MAX_SIZE){
415 if (ieee->iw_mode == IW_MODE_MONITOR){
421 ieee80211_stop_protocol(ieee,true);
425 /* this is just to be sure that the GET wx callback
426 * has consisten infos. not needed otherwise
428 spin_lock_irqsave(&ieee->lock, flags);
430 if (wrqu->essid.flags && wrqu->essid.length) {
431 //first flush current network.ssid
432 len = ((wrqu->essid.length-1) < IW_ESSID_MAX_SIZE) ? (wrqu->essid.length-1) : IW_ESSID_MAX_SIZE;
433 strncpy(ieee->current_network.ssid, extra, len+1);
434 ieee->current_network.ssid_len = len+1;
439 ieee->current_network.ssid[0] = '\0';
440 ieee->current_network.ssid_len = 0;
442 spin_unlock_irqrestore(&ieee->lock, flags);
445 ieee80211_start_protocol(ieee);
451 int ieee80211_wx_get_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
452 union iwreq_data *wrqu, char *b)
455 wrqu->mode = ieee->iw_mode;
459 int ieee80211_wx_set_rawtx(struct ieee80211_device *ieee,
460 struct iw_request_info *info,
461 union iwreq_data *wrqu, char *extra)
464 int *parms = (int *)extra;
465 int enable = (parms[0] > 0);
466 short prev = ieee->raw_tx;
475 printk(KERN_INFO"raw TX is %s\n",
476 ieee->raw_tx ? "enabled" : "disabled");
478 if(ieee->iw_mode == IW_MODE_MONITOR)
480 if(prev == 0 && ieee->raw_tx){
481 if (ieee->data_hard_resume)
482 ieee->data_hard_resume(ieee);
484 netif_carrier_on(ieee->dev);
487 if(prev && ieee->raw_tx == 1)
488 netif_carrier_off(ieee->dev);
496 int ieee80211_wx_get_name(struct ieee80211_device *ieee,
497 struct iw_request_info *info,
498 union iwreq_data *wrqu, char *extra)
500 strcpy(wrqu->name, "802.11");
501 if(ieee->modulation & IEEE80211_CCK_MODULATION)
502 strcat(wrqu->name, "b");
503 if(ieee->modulation & IEEE80211_OFDM_MODULATION)
504 strcat(wrqu->name, "g");
505 if (ieee->mode & (IEEE_N_24G | IEEE_N_5G))
506 strcat(wrqu->name, "n");
511 /* this is mostly stolen from hostap */
512 int ieee80211_wx_set_power(struct ieee80211_device *ieee,
513 struct iw_request_info *info,
514 union iwreq_data *wrqu, char *extra)
519 (!ieee->sta_wake_up) ||
520 (!ieee->enter_sleep_state) ||
521 (!ieee->ps_is_queue_empty)){
528 if (wrqu->power.disabled){
529 ieee->ps = IEEE80211_PS_DISABLED;
532 if (wrqu->power.flags & IW_POWER_TIMEOUT) {
533 ieee->ps_timeout = wrqu->power.value / 1000;
536 if (wrqu->power.flags & IW_POWER_PERIOD) {
537 ieee->ps_period = wrqu->power.value / 1000;
539 switch (wrqu->power.flags & IW_POWER_MODE) {
540 case IW_POWER_UNICAST_R:
541 ieee->ps = IEEE80211_PS_UNICAST;
543 case IW_POWER_MULTICAST_R:
544 ieee->ps = IEEE80211_PS_MBCAST;
547 ieee->ps = IEEE80211_PS_UNICAST | IEEE80211_PS_MBCAST;
564 /* this is stolen from hostap */
565 int ieee80211_wx_get_power(struct ieee80211_device *ieee,
566 struct iw_request_info *info,
567 union iwreq_data *wrqu, char *extra)
573 if(ieee->ps == IEEE80211_PS_DISABLED){
574 wrqu->power.disabled = 1;
578 wrqu->power.disabled = 0;
580 if ((wrqu->power.flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
581 wrqu->power.flags = IW_POWER_TIMEOUT;
582 wrqu->power.value = ieee->ps_timeout * 1000;
584 wrqu->power.flags = IW_POWER_PERIOD;
585 wrqu->power.value = ieee->ps_period * 1000;
588 if ((ieee->ps & (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST)) == (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST))
589 wrqu->power.flags |= IW_POWER_ALL_R;
590 else if (ieee->ps & IEEE80211_PS_MBCAST)
591 wrqu->power.flags |= IW_POWER_MULTICAST_R;
593 wrqu->power.flags |= IW_POWER_UNICAST_R;