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
,true);
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
,true);
306 ieee
->iw_mode
= wrqu
->mode
;
307 ieee80211_start_protocol(ieee
);
315 void ieee80211_wx_sync_scan_wq(struct work_struct
*work
)
317 struct ieee80211_device
*ieee
= container_of(work
, struct ieee80211_device
, wx_sync_scan_wq
);
319 HT_EXTCHNL_OFFSET chan_offset
=0;
320 HT_CHANNEL_WIDTH bandwidth
=0;
322 static int count
= 0;
323 chan
= ieee
->current_network
.channel
;
326 if (ieee
->LeisurePSLeave
) {
327 ieee
->LeisurePSLeave(ieee
->dev
);
330 /* notify AP to be in PS mode */
331 ieee80211_sta_ps_send_null_frame(ieee
, 1);
332 ieee80211_sta_ps_send_null_frame(ieee
, 1);
335 if (ieee
->data_hard_stop
)
336 ieee
->data_hard_stop(ieee
->dev
);
338 ieee80211_stop_send_beacons(ieee
);
340 ieee
->state
= IEEE80211_LINKED_SCANNING
;
341 ieee
->link_change(ieee
->dev
);
342 ieee
->InitialGainHandler(ieee
->dev
,IG_Backup
);
343 if (ieee
->pHTInfo
->bCurrentHTSupport
&& ieee
->pHTInfo
->bEnableHT
&& ieee
->pHTInfo
->bCurBW40MHz
) {
345 chan_offset
= ieee
->pHTInfo
->CurSTAExtChnlOffset
;
346 bandwidth
= (HT_CHANNEL_WIDTH
)ieee
->pHTInfo
->bCurBW40MHz
;
347 printk("Scan in 40M, force to 20M first:%d, %d\n", chan_offset
, bandwidth
);
348 ieee
->SetBWModeHandler(ieee
->dev
, HT_CHANNEL_WIDTH_20
, HT_EXTCHNL_OFFSET_NO_EXT
);
350 ieee80211_start_scan_syncro(ieee
);
352 printk("Scan in 20M, back to 40M\n");
353 if (chan_offset
== HT_EXTCHNL_OFFSET_UPPER
)
354 ieee
->set_chan(ieee
->dev
, chan
+ 2);
355 else if (chan_offset
== HT_EXTCHNL_OFFSET_LOWER
)
356 ieee
->set_chan(ieee
->dev
, chan
- 2);
358 ieee
->set_chan(ieee
->dev
, chan
);
359 ieee
->SetBWModeHandler(ieee
->dev
, bandwidth
, chan_offset
);
361 ieee
->set_chan(ieee
->dev
, chan
);
364 ieee
->InitialGainHandler(ieee
->dev
,IG_Restore
);
365 ieee
->state
= IEEE80211_LINKED
;
366 ieee
->link_change(ieee
->dev
);
369 /* Notify AP that I wake up again */
370 ieee80211_sta_ps_send_null_frame(ieee
, 0);
373 // To prevent the immediately calling watch_dog after scan.
374 if(ieee
->LinkDetectInfo
.NumRecvBcnInPeriod
==0||ieee
->LinkDetectInfo
.NumRecvDataInPeriod
==0 )
376 ieee
->LinkDetectInfo
.NumRecvBcnInPeriod
= 1;
377 ieee
->LinkDetectInfo
.NumRecvDataInPeriod
= 1;
379 if (ieee
->data_hard_resume
)
380 ieee
->data_hard_resume(ieee
->dev
);
382 if(ieee
->iw_mode
== IW_MODE_ADHOC
|| ieee
->iw_mode
== IW_MODE_MASTER
)
383 ieee80211_start_send_beacons(ieee
);
390 int ieee80211_wx_set_scan(struct ieee80211_device
*ieee
, struct iw_request_info
*a
,
391 union iwreq_data
*wrqu
, char *b
)
397 if (ieee
->iw_mode
== IW_MODE_MONITOR
|| !(ieee
->proto_started
)){
402 if ( ieee
->state
== IEEE80211_LINKED
){
403 queue_work(ieee
->wq
, &ieee
->wx_sync_scan_wq
);
404 /* intentionally forget to up sem */
413 int ieee80211_wx_set_essid(struct ieee80211_device
*ieee
,
414 struct iw_request_info
*a
,
415 union iwreq_data
*wrqu
, char *extra
)
422 ieee
->sync_scan_hurryup
= 1;
425 proto_started
= ieee
->proto_started
;
427 if (wrqu
->essid
.length
> IW_ESSID_MAX_SIZE
){
432 if (ieee
->iw_mode
== IW_MODE_MONITOR
){
438 ieee80211_stop_protocol(ieee
,true);
442 /* this is just to be sure that the GET wx callback
443 * has consisten infos. not needed otherwise
445 spin_lock_irqsave(&ieee
->lock
, flags
);
447 if (wrqu
->essid
.flags
&& wrqu
->essid
.length
) {
448 //first flush current network.ssid
449 len
= ((wrqu
->essid
.length
-1) < IW_ESSID_MAX_SIZE
) ? (wrqu
->essid
.length
-1) : IW_ESSID_MAX_SIZE
;
450 strncpy(ieee
->current_network
.ssid
, extra
, len
+1);
451 ieee
->current_network
.ssid_len
= len
+1;
456 ieee
->current_network
.ssid
[0] = '\0';
457 ieee
->current_network
.ssid_len
= 0;
459 spin_unlock_irqrestore(&ieee
->lock
, flags
);
462 ieee80211_start_protocol(ieee
);
468 int ieee80211_wx_get_mode(struct ieee80211_device
*ieee
, struct iw_request_info
*a
,
469 union iwreq_data
*wrqu
, char *b
)
472 wrqu
->mode
= ieee
->iw_mode
;
476 int ieee80211_wx_set_rawtx(struct ieee80211_device
*ieee
,
477 struct iw_request_info
*info
,
478 union iwreq_data
*wrqu
, char *extra
)
481 int *parms
= (int *)extra
;
482 int enable
= (parms
[0] > 0);
483 short prev
= ieee
->raw_tx
;
492 printk(KERN_INFO
"raw TX is %s\n",
493 ieee
->raw_tx
? "enabled" : "disabled");
495 if(ieee
->iw_mode
== IW_MODE_MONITOR
)
497 if(prev
== 0 && ieee
->raw_tx
){
498 if (ieee
->data_hard_resume
)
499 ieee
->data_hard_resume(ieee
->dev
);
501 netif_carrier_on(ieee
->dev
);
504 if(prev
&& ieee
->raw_tx
== 1)
505 netif_carrier_off(ieee
->dev
);
513 int ieee80211_wx_get_name(struct ieee80211_device
*ieee
,
514 struct iw_request_info
*info
,
515 union iwreq_data
*wrqu
, char *extra
)
517 strcpy(wrqu
->name
, "802.11");
518 if(ieee
->modulation
& IEEE80211_CCK_MODULATION
)
519 strcat(wrqu
->name
, "b");
520 if(ieee
->modulation
& IEEE80211_OFDM_MODULATION
)
521 strcat(wrqu
->name
, "g");
522 if (ieee
->mode
& (IEEE_N_24G
| IEEE_N_5G
))
523 strcat(wrqu
->name
, "n");
528 /* this is mostly stolen from hostap */
529 int ieee80211_wx_set_power(struct ieee80211_device
*ieee
,
530 struct iw_request_info
*info
,
531 union iwreq_data
*wrqu
, char *extra
)
536 (!ieee
->sta_wake_up
) ||
537 // (!ieee->ps_request_tx_ack) ||
538 (!ieee
->enter_sleep_state
) ||
539 (!ieee
->ps_is_queue_empty
)){
546 if (wrqu
->power
.disabled
){
547 ieee
->ps
= IEEE80211_PS_DISABLED
;
550 if (wrqu
->power
.flags
& IW_POWER_TIMEOUT
) {
551 //ieee->ps_period = wrqu->power.value / 1000;
552 ieee
->ps_timeout
= wrqu
->power
.value
/ 1000;
555 if (wrqu
->power
.flags
& IW_POWER_PERIOD
) {
557 //ieee->ps_timeout = wrqu->power.value / 1000;
558 ieee
->ps_period
= wrqu
->power
.value
/ 1000;
562 switch (wrqu
->power
.flags
& IW_POWER_MODE
) {
563 case IW_POWER_UNICAST_R
:
564 ieee
->ps
= IEEE80211_PS_UNICAST
;
566 case IW_POWER_MULTICAST_R
:
567 ieee
->ps
= IEEE80211_PS_MBCAST
;
570 ieee
->ps
= IEEE80211_PS_UNICAST
| IEEE80211_PS_MBCAST
;
574 // ieee->ps = IEEE80211_PS_DISABLED;
588 /* this is stolen from hostap */
589 int ieee80211_wx_get_power(struct ieee80211_device
*ieee
,
590 struct iw_request_info
*info
,
591 union iwreq_data
*wrqu
, char *extra
)
597 if(ieee
->ps
== IEEE80211_PS_DISABLED
){
598 wrqu
->power
.disabled
= 1;
602 wrqu
->power
.disabled
= 0;
604 if ((wrqu
->power
.flags
& IW_POWER_TYPE
) == IW_POWER_TIMEOUT
) {
605 wrqu
->power
.flags
= IW_POWER_TIMEOUT
;
606 wrqu
->power
.value
= ieee
->ps_timeout
* 1000;
608 // ret = -EOPNOTSUPP;
610 wrqu
->power
.flags
= IW_POWER_PERIOD
;
611 wrqu
->power
.value
= ieee
->ps_period
* 1000;
612 //ieee->current_network.dtim_period * ieee->current_network.beacon_interval * 1024;
615 if ((ieee
->ps
& (IEEE80211_PS_MBCAST
| IEEE80211_PS_UNICAST
)) == (IEEE80211_PS_MBCAST
| IEEE80211_PS_UNICAST
))
616 wrqu
->power
.flags
|= IW_POWER_ALL_R
;
617 else if (ieee
->ps
& IEEE80211_PS_MBCAST
)
618 wrqu
->power
.flags
|= IW_POWER_MULTICAST_R
;
620 wrqu
->power
.flags
|= IW_POWER_UNICAST_R
;