1 /* IEEE 802.11 SoftMAC layer
2 * Copyright (c) 2005 Andrea Merello <andrea.merello@gmail.com>
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 <linux/etherdevice.h>
19 #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,
29 EXPORT_SYMBOL(ieee80211_wlan_frequencies);
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 */
66 if (!(GET_DOT11D_INFO(ieee)->channel_map)[fwrq->m]) {
70 ieee->current_network.channel = fwrq->m;
71 ieee->set_chan(ieee->dev, ieee->current_network.channel);
73 if(ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
74 if(ieee->state == IEEE80211_LINKED){
76 ieee80211_stop_send_beacons(ieee);
77 ieee80211_start_send_beacons(ieee);
86 EXPORT_SYMBOL(ieee80211_wx_set_freq);
88 int ieee80211_wx_get_freq(struct ieee80211_device *ieee,
89 struct iw_request_info *a,
90 union iwreq_data *wrqu, char *b)
92 struct iw_freq *fwrq = & wrqu->freq;
94 if (ieee->current_network.channel == 0)
96 //NM 0.7.0 will not accept channel any more.
97 fwrq->m = ieee80211_wlan_frequencies[ieee->current_network.channel-1] * 100000;
99 // fwrq->m = ieee->current_network.channel;
104 EXPORT_SYMBOL(ieee80211_wx_get_freq);
106 int ieee80211_wx_get_wap(struct ieee80211_device *ieee,
107 struct iw_request_info *info,
108 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);
133 EXPORT_SYMBOL(ieee80211_wx_get_wap);
135 int ieee80211_wx_set_wap(struct ieee80211_device *ieee,
136 struct iw_request_info *info,
137 union iwreq_data *awrq,
144 short ifup = ieee->proto_started;//dev->flags & IFF_UP;
145 struct sockaddr *temp = (struct sockaddr *)awrq;
147 ieee->sync_scan_hurryup = 1;
150 /* use ifconfig hw ether */
151 if (ieee->iw_mode == IW_MODE_MASTER){
156 if (temp->sa_family != ARPHRD_ETHER){
162 ieee80211_stop_protocol(ieee);
164 /* just to avoid to give inconsistent infos in the
165 * get wx method. not really needed otherwise
167 spin_lock_irqsave(&ieee->lock, flags);
169 memcpy(ieee->current_network.bssid, temp->sa_data, ETH_ALEN);
170 ieee->wap_set = !is_zero_ether_addr(temp->sa_data);
172 spin_unlock_irqrestore(&ieee->lock, flags);
175 ieee80211_start_protocol(ieee);
180 EXPORT_SYMBOL(ieee80211_wx_set_wap);
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);
216 EXPORT_SYMBOL(ieee80211_wx_get_essid);
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.
229 EXPORT_SYMBOL(ieee80211_wx_set_rate);
231 int ieee80211_wx_get_rate(struct ieee80211_device *ieee,
232 struct iw_request_info *info,
233 union iwreq_data *wrqu, char *extra)
236 tmp_rate = TxCountToDataRate(ieee, ieee->softmac_stats.CurrentShowTxate);
238 wrqu->bitrate.value = tmp_rate * 500000;
242 EXPORT_SYMBOL(ieee80211_wx_get_rate);
244 int ieee80211_wx_set_rts(struct ieee80211_device *ieee,
245 struct iw_request_info *info,
246 union iwreq_data *wrqu, char *extra)
248 if (wrqu->rts.disabled || !wrqu->rts.fixed)
249 ieee->rts = DEFAULT_RTS_THRESHOLD;
252 if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
253 wrqu->rts.value > MAX_RTS_THRESHOLD)
255 ieee->rts = wrqu->rts.value;
259 EXPORT_SYMBOL(ieee80211_wx_set_rts);
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 EXPORT_SYMBOL(ieee80211_wx_get_rts);
272 int ieee80211_wx_set_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
273 union iwreq_data *wrqu, char *b)
276 ieee->sync_scan_hurryup = 1;
280 if (wrqu->mode == ieee->iw_mode)
283 if (wrqu->mode == IW_MODE_MONITOR){
285 ieee->dev->type = ARPHRD_IEEE80211;
287 ieee->dev->type = ARPHRD_ETHER;
290 if (!ieee->proto_started){
291 ieee->iw_mode = wrqu->mode;
293 ieee80211_stop_protocol(ieee);
294 ieee->iw_mode = wrqu->mode;
295 ieee80211_start_protocol(ieee);
302 EXPORT_SYMBOL(ieee80211_wx_set_mode);
304 void ieee80211_wx_sync_scan_wq(struct work_struct *work)
306 struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, wx_sync_scan_wq);
308 HT_EXTCHNL_OFFSET chan_offset=0;
309 HT_CHANNEL_WIDTH bandwidth=0;
312 chan = ieee->current_network.channel;
313 netif_carrier_off(ieee->dev);
315 if (ieee->data_hard_stop)
316 ieee->data_hard_stop(ieee->dev);
318 ieee80211_stop_send_beacons(ieee);
320 ieee->state = IEEE80211_LINKED_SCANNING;
321 ieee->link_change(ieee->dev);
322 ieee->InitialGainHandler(ieee->dev, IG_Backup);
323 if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT && ieee->pHTInfo->bCurBW40MHz) {
325 chan_offset = ieee->pHTInfo->CurSTAExtChnlOffset;
326 bandwidth = (HT_CHANNEL_WIDTH)ieee->pHTInfo->bCurBW40MHz;
327 printk("Scan in 40M, force to 20M first:%d, %d\n", chan_offset, bandwidth);
328 ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
330 ieee80211_start_scan_syncro(ieee);
332 printk("Scan in 20M, back to 40M\n");
333 if (chan_offset == HT_EXTCHNL_OFFSET_UPPER)
334 ieee->set_chan(ieee->dev, chan + 2);
335 else if (chan_offset == HT_EXTCHNL_OFFSET_LOWER)
336 ieee->set_chan(ieee->dev, chan - 2);
338 ieee->set_chan(ieee->dev, chan);
339 ieee->SetBWModeHandler(ieee->dev, bandwidth, chan_offset);
341 ieee->set_chan(ieee->dev, chan);
344 ieee->InitialGainHandler(ieee->dev, IG_Restore);
345 ieee->state = IEEE80211_LINKED;
346 ieee->link_change(ieee->dev);
347 // To prevent the immediately calling watch_dog after scan.
348 if(ieee->LinkDetectInfo.NumRecvBcnInPeriod==0||ieee->LinkDetectInfo.NumRecvDataInPeriod==0 )
350 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
351 ieee->LinkDetectInfo.NumRecvDataInPeriod= 1;
353 if (ieee->data_hard_resume)
354 ieee->data_hard_resume(ieee->dev);
356 if(ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
357 ieee80211_start_send_beacons(ieee);
359 netif_carrier_on(ieee->dev);
365 int ieee80211_wx_set_scan(struct ieee80211_device *ieee, struct iw_request_info *a,
366 union iwreq_data *wrqu, char *b)
372 if (ieee->iw_mode == IW_MODE_MONITOR || !(ieee->proto_started)){
377 if ( ieee->state == IEEE80211_LINKED){
378 queue_work(ieee->wq, &ieee->wx_sync_scan_wq);
379 /* intentionally forget to up sem */
387 EXPORT_SYMBOL(ieee80211_wx_set_scan);
389 int ieee80211_wx_set_essid(struct ieee80211_device *ieee,
390 struct iw_request_info *a,
391 union iwreq_data *wrqu, char *extra)
398 ieee->sync_scan_hurryup = 1;
401 proto_started = ieee->proto_started;
403 if (wrqu->essid.length > IW_ESSID_MAX_SIZE){
408 if (ieee->iw_mode == IW_MODE_MONITOR){
414 ieee80211_stop_protocol(ieee);
417 /* this is just to be sure that the GET wx callback
418 * has consisten infos. not needed otherwise
420 spin_lock_irqsave(&ieee->lock, flags);
422 if (wrqu->essid.flags && wrqu->essid.length) {
423 //first flush current network.ssid
424 len = ((wrqu->essid.length-1) < IW_ESSID_MAX_SIZE) ? (wrqu->essid.length-1) : IW_ESSID_MAX_SIZE;
425 strncpy(ieee->current_network.ssid, extra, len+1);
426 ieee->current_network.ssid_len = len+1;
431 ieee->current_network.ssid[0] = '\0';
432 ieee->current_network.ssid_len = 0;
434 spin_unlock_irqrestore(&ieee->lock, flags);
437 ieee80211_start_protocol(ieee);
442 EXPORT_SYMBOL(ieee80211_wx_set_essid);
444 int ieee80211_wx_get_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
445 union iwreq_data *wrqu, char *b)
448 wrqu->mode = ieee->iw_mode;
451 EXPORT_SYMBOL(ieee80211_wx_get_mode);
453 int ieee80211_wx_set_rawtx(struct ieee80211_device *ieee,
454 struct iw_request_info *info,
455 union iwreq_data *wrqu, char *extra)
458 int *parms = (int *)extra;
459 int enable = (parms[0] > 0);
460 short prev = ieee->raw_tx;
469 printk(KERN_INFO"raw TX is %s\n",
470 ieee->raw_tx ? "enabled" : "disabled");
472 if(ieee->iw_mode == IW_MODE_MONITOR)
474 if(prev == 0 && ieee->raw_tx){
475 if (ieee->data_hard_resume)
476 ieee->data_hard_resume(ieee->dev);
478 netif_carrier_on(ieee->dev);
481 if(prev && ieee->raw_tx == 1)
482 netif_carrier_off(ieee->dev);
489 EXPORT_SYMBOL(ieee80211_wx_set_rawtx);
491 int ieee80211_wx_get_name(struct ieee80211_device *ieee,
492 struct iw_request_info *info,
493 union iwreq_data *wrqu, char *extra)
495 strlcpy(wrqu->name, "802.11", IFNAMSIZ);
496 if (ieee->modulation & IEEE80211_CCK_MODULATION) {
497 strlcat(wrqu->name, "b", IFNAMSIZ);
498 if (ieee->modulation & IEEE80211_OFDM_MODULATION)
499 strlcat(wrqu->name, "/g", IFNAMSIZ);
500 } else if (ieee->modulation & IEEE80211_OFDM_MODULATION) {
501 strlcat(wrqu->name, "g", IFNAMSIZ);
504 if (ieee->mode & (IEEE_N_24G | IEEE_N_5G))
505 strlcat(wrqu->name, "/n", IFNAMSIZ);
507 if ((ieee->state == IEEE80211_LINKED) ||
508 (ieee->state == IEEE80211_LINKED_SCANNING))
509 strlcat(wrqu->name, " linked", IFNAMSIZ);
510 else if (ieee->state != IEEE80211_NOLINK)
511 strlcat(wrqu->name, " link..", IFNAMSIZ);
515 EXPORT_SYMBOL(ieee80211_wx_get_name);
517 /* this is mostly stolen from hostap */
518 int ieee80211_wx_set_power(struct ieee80211_device *ieee,
519 struct iw_request_info *info,
520 union iwreq_data *wrqu, char *extra)
525 if (wrqu->power.disabled){
526 ieee->ps = IEEE80211_PS_DISABLED;
529 if (wrqu->power.flags & IW_POWER_TIMEOUT) {
530 //ieee->ps_period = wrqu->power.value / 1000;
531 ieee->ps_timeout = wrqu->power.value / 1000;
534 if (wrqu->power.flags & IW_POWER_PERIOD) {
536 //ieee->ps_timeout = wrqu->power.value / 1000;
537 ieee->ps_period = wrqu->power.value / 1000;
541 switch (wrqu->power.flags & IW_POWER_MODE) {
542 case IW_POWER_UNICAST_R:
543 ieee->ps = IEEE80211_PS_UNICAST;
545 case IW_POWER_MULTICAST_R:
546 ieee->ps = IEEE80211_PS_MBCAST;
549 ieee->ps = IEEE80211_PS_UNICAST | IEEE80211_PS_MBCAST;
553 // ieee->ps = IEEE80211_PS_DISABLED;
566 EXPORT_SYMBOL(ieee80211_wx_set_power);
568 /* this is stolen from hostap */
569 int ieee80211_wx_get_power(struct ieee80211_device *ieee,
570 struct iw_request_info *info,
571 union iwreq_data *wrqu, char *extra)
575 if(ieee->ps == IEEE80211_PS_DISABLED){
576 wrqu->power.disabled = 1;
580 wrqu->power.disabled = 0;
582 if ((wrqu->power.flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
583 wrqu->power.flags = IW_POWER_TIMEOUT;
584 wrqu->power.value = ieee->ps_timeout * 1000;
586 // ret = -EOPNOTSUPP;
588 wrqu->power.flags = IW_POWER_PERIOD;
589 wrqu->power.value = ieee->ps_period * 1000;
590 //ieee->current_network.dtim_period * ieee->current_network.beacon_interval * 1024;
593 if ((ieee->ps & (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST)) == (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST))
594 wrqu->power.flags |= IW_POWER_ALL_R;
595 else if (ieee->ps & IEEE80211_PS_MBCAST)
596 wrqu->power.flags |= IW_POWER_MULTICAST_R;
598 wrqu->power.flags |= IW_POWER_UNICAST_R;
605 EXPORT_SYMBOL(ieee80211_wx_get_power);