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"
19 /* FIXME: add A freqs */
21 const long ieee80211_wlan_frequencies[] = {
22 2412, 2417, 2422, 2427,
23 2432, 2437, 2442, 2447,
24 2452, 2457, 2462, 2467,
29 int ieee80211_wx_set_freq(struct ieee80211_device *ieee, struct iw_request_info *a,
30 union iwreq_data *wrqu, char *b)
33 struct iw_freq *fwrq = & wrqu->freq;
37 if(ieee->iw_mode == IW_MODE_INFRA){
42 /* if setting by freq convert to channel */
44 if ((fwrq->m >= (int) 2.412e8 &&
45 fwrq->m <= (int) 2.487e8)) {
46 int f = fwrq->m / 100000;
49 while ((c < 14) && (f != ieee80211_wlan_frequencies[c]))
52 /* hack to fall through */
58 if (fwrq->e > 0 || fwrq->m > 14 || fwrq->m < 1 ){
62 }else { /* Set the channel */
64 if (!(GET_DOT11D_INFO(ieee)->channel_map)[fwrq->m]) {
68 ieee->current_network.channel = fwrq->m;
69 ieee->set_chan(ieee->dev, ieee->current_network.channel);
71 if(ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
72 if(ieee->state == IEEE80211_LINKED){
74 ieee80211_stop_send_beacons(ieee);
75 ieee80211_start_send_beacons(ieee);
86 int ieee80211_wx_get_freq(struct ieee80211_device *ieee,
87 struct iw_request_info *a,
88 union iwreq_data *wrqu, char *b)
90 struct iw_freq *fwrq = & wrqu->freq;
92 if (ieee->current_network.channel == 0)
94 //NM 0.7.0 will not accept channel any more.
95 fwrq->m = ieee80211_wlan_frequencies[ieee->current_network.channel-1] * 100000;
97 // fwrq->m = ieee->current_network.channel;
103 int ieee80211_wx_get_wap(struct ieee80211_device *ieee,
104 struct iw_request_info *info,
105 union iwreq_data *wrqu, char *extra)
109 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
111 if (ieee->iw_mode == IW_MODE_MONITOR)
114 /* We want avoid to give to the user inconsistent infos*/
115 spin_lock_irqsave(&ieee->lock, flags);
117 if (ieee->state != IEEE80211_LINKED &&
118 ieee->state != IEEE80211_LINKED_SCANNING &&
121 memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);
123 memcpy(wrqu->ap_addr.sa_data,
124 ieee->current_network.bssid, ETH_ALEN);
126 spin_unlock_irqrestore(&ieee->lock, flags);
132 int ieee80211_wx_set_wap(struct ieee80211_device *ieee,
133 struct iw_request_info *info,
134 union iwreq_data *awrq,
139 u8 zero[] = {0,0,0,0,0,0};
142 short ifup = ieee->proto_started;//dev->flags & IFF_UP;
143 struct sockaddr *temp = (struct sockaddr *)awrq;
145 ieee->sync_scan_hurryup = 1;
148 /* use ifconfig hw ether */
149 if (ieee->iw_mode == IW_MODE_MASTER){
154 if (temp->sa_family != ARPHRD_ETHER){
160 ieee80211_stop_protocol(ieee);
162 /* just to avoid to give inconsistent infos in the
163 * get wx method. not really needed otherwise
165 spin_lock_irqsave(&ieee->lock, flags);
167 memcpy(ieee->current_network.bssid, temp->sa_data, ETH_ALEN);
168 ieee->wap_set = memcmp(temp->sa_data, zero,ETH_ALEN)!=0;
170 spin_unlock_irqrestore(&ieee->lock, flags);
173 ieee80211_start_protocol(ieee);
179 int ieee80211_wx_get_essid(struct ieee80211_device *ieee, struct iw_request_info *a,union iwreq_data *wrqu,char *b)
184 if (ieee->iw_mode == IW_MODE_MONITOR)
187 /* We want avoid to give to the user inconsistent infos*/
188 spin_lock_irqsave(&ieee->lock, flags);
190 if (ieee->current_network.ssid[0] == '\0' ||
191 ieee->current_network.ssid_len == 0){
196 if (ieee->state != IEEE80211_LINKED &&
197 ieee->state != IEEE80211_LINKED_SCANNING &&
198 ieee->ssid_set == 0){
202 len = ieee->current_network.ssid_len;
203 wrqu->essid.length = len;
204 strncpy(b,ieee->current_network.ssid,len);
205 wrqu->essid.flags = 1;
208 spin_unlock_irqrestore(&ieee->lock, flags);
214 int ieee80211_wx_set_rate(struct ieee80211_device *ieee,
215 struct iw_request_info *info,
216 union iwreq_data *wrqu, char *extra)
219 u32 target_rate = wrqu->bitrate.value;
221 ieee->rate = target_rate/100000;
222 //FIXME: we might want to limit rate also in management protocols.
228 int ieee80211_wx_get_rate(struct ieee80211_device *ieee,
229 struct iw_request_info *info,
230 union iwreq_data *wrqu, char *extra)
234 //printk("===>mode:%d, halfNmode:%d\n", ieee->mode, ieee->bHalfWirelessN24GMode);
235 if (ieee->mode & (IEEE_A | IEEE_B | IEEE_G))
236 tmp_rate = ieee->rate;
237 else if (ieee->mode & IEEE_N_5G)
239 else if (ieee->mode & IEEE_N_24G)
241 if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
242 tmp_rate = HTHalfMcsToDataRate(ieee, 15);
244 tmp_rate = HTMcsToDataRate(ieee, 15);
247 tmp_rate = TxCountToDataRate(ieee, ieee->softmac_stats.CurrentShowTxate);
250 wrqu->bitrate.value = tmp_rate * 500000;
256 int ieee80211_wx_set_rts(struct ieee80211_device *ieee,
257 struct iw_request_info *info,
258 union iwreq_data *wrqu, char *extra)
260 if (wrqu->rts.disabled || !wrqu->rts.fixed)
261 ieee->rts = DEFAULT_RTS_THRESHOLD;
264 if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
265 wrqu->rts.value > MAX_RTS_THRESHOLD)
267 ieee->rts = wrqu->rts.value;
272 int ieee80211_wx_get_rts(struct ieee80211_device *ieee,
273 struct iw_request_info *info,
274 union iwreq_data *wrqu, char *extra)
276 wrqu->rts.value = ieee->rts;
277 wrqu->rts.fixed = 0; /* no auto select */
278 wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
281 int ieee80211_wx_set_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
282 union iwreq_data *wrqu, char *b)
285 ieee->sync_scan_hurryup = 1;
289 if (wrqu->mode == ieee->iw_mode)
292 if (wrqu->mode == IW_MODE_MONITOR){
294 ieee->dev->type = ARPHRD_IEEE80211;
296 ieee->dev->type = ARPHRD_ETHER;
299 if (!ieee->proto_started){
300 ieee->iw_mode = wrqu->mode;
302 ieee80211_stop_protocol(ieee);
303 ieee->iw_mode = wrqu->mode;
304 ieee80211_start_protocol(ieee);
312 void ieee80211_wx_sync_scan_wq(struct work_struct *work)
314 struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, wx_sync_scan_wq);
316 HT_EXTCHNL_OFFSET chan_offset=0;
317 HT_CHANNEL_WIDTH bandwidth=0;
319 static int count = 0;
320 chan = ieee->current_network.channel;
321 netif_carrier_off(ieee->dev);
323 if (ieee->data_hard_stop)
324 ieee->data_hard_stop(ieee->dev);
326 ieee80211_stop_send_beacons(ieee);
328 ieee->state = IEEE80211_LINKED_SCANNING;
329 ieee->link_change(ieee->dev);
331 ieee->InitialGainHandler(ieee->dev,IG_Backup);
333 #if(RTL8192S_DISABLE_FW_DM == 0)
334 if (ieee->SetFwCmdHandler)
336 ieee->SetFwCmdHandler(ieee->dev, FW_CMD_DIG_HALT);
337 ieee->SetFwCmdHandler(ieee->dev, FW_CMD_HIGH_PWR_DISABLE);
340 if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT && ieee->pHTInfo->bCurBW40MHz) {
342 chan_offset = ieee->pHTInfo->CurSTAExtChnlOffset;
343 bandwidth = (HT_CHANNEL_WIDTH)ieee->pHTInfo->bCurBW40MHz;
344 printk("Scan in 40M, force to 20M first:%d, %d\n", chan_offset, bandwidth);
345 ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
347 ieee80211_start_scan_syncro(ieee);
349 printk("Scan in 20M, back to 40M\n");
350 if (chan_offset == HT_EXTCHNL_OFFSET_UPPER)
351 ieee->set_chan(ieee->dev, chan + 2);
352 else if (chan_offset == HT_EXTCHNL_OFFSET_LOWER)
353 ieee->set_chan(ieee->dev, chan - 2);
355 ieee->set_chan(ieee->dev, chan);
356 ieee->SetBWModeHandler(ieee->dev, bandwidth, chan_offset);
358 ieee->set_chan(ieee->dev, chan);
362 ieee->InitialGainHandler(ieee->dev,IG_Restore);
364 #if(RTL8192S_DISABLE_FW_DM == 0)
365 if (ieee->SetFwCmdHandler)
367 ieee->SetFwCmdHandler(ieee->dev, FW_CMD_DIG_RESUME);
368 ieee->SetFwCmdHandler(ieee->dev, FW_CMD_HIGH_PWR_ENABLE);
371 ieee->state = IEEE80211_LINKED;
372 ieee->link_change(ieee->dev);
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);
385 netif_carrier_on(ieee->dev);
391 int ieee80211_wx_set_scan(struct ieee80211_device *ieee, struct iw_request_info *a,
392 union iwreq_data *wrqu, char *b)
398 if (ieee->iw_mode == IW_MODE_MONITOR || !(ieee->proto_started)){
403 if ( ieee->state == IEEE80211_LINKED){
404 queue_work(ieee->wq, &ieee->wx_sync_scan_wq);
405 /* intentionally forget to up sem */
414 int ieee80211_wx_set_essid(struct ieee80211_device *ieee,
415 struct iw_request_info *a,
416 union iwreq_data *wrqu, char *extra)
423 ieee->sync_scan_hurryup = 1;
426 proto_started = ieee->proto_started;
428 if (wrqu->essid.length > IW_ESSID_MAX_SIZE){
433 if (ieee->iw_mode == IW_MODE_MONITOR){
439 ieee80211_stop_protocol(ieee);
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;
455 for (i=0; i<len + 1; i++)
456 printk("%c ", extra[i]);
464 ieee->current_network.ssid[0] = '\0';
465 ieee->current_network.ssid_len = 0;
467 spin_unlock_irqrestore(&ieee->lock, flags);
470 ieee80211_start_protocol(ieee);
476 int ieee80211_wx_get_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
477 union iwreq_data *wrqu, char *b)
480 wrqu->mode = ieee->iw_mode;
484 int ieee80211_wx_set_rawtx(struct ieee80211_device *ieee,
485 struct iw_request_info *info,
486 union iwreq_data *wrqu, char *extra)
489 int *parms = (int *)extra;
490 int enable = (parms[0] > 0);
491 short prev = ieee->raw_tx;
500 printk(KERN_INFO"raw TX is %s\n",
501 ieee->raw_tx ? "enabled" : "disabled");
503 if(ieee->iw_mode == IW_MODE_MONITOR)
505 if(prev == 0 && ieee->raw_tx){
506 if (ieee->data_hard_resume)
507 ieee->data_hard_resume(ieee->dev);
509 netif_carrier_on(ieee->dev);
512 if(prev && ieee->raw_tx == 1)
513 netif_carrier_off(ieee->dev);
521 int ieee80211_wx_get_name(struct ieee80211_device *ieee,
522 struct iw_request_info *info,
523 union iwreq_data *wrqu, char *extra)
525 strlcpy(wrqu->name, "802.11", IFNAMSIZ);
526 if(ieee->modulation & IEEE80211_CCK_MODULATION){
527 strlcat(wrqu->name, "b", IFNAMSIZ);
528 if(ieee->modulation & IEEE80211_OFDM_MODULATION)
529 strlcat(wrqu->name, "/g", IFNAMSIZ);
530 }else if(ieee->modulation & IEEE80211_OFDM_MODULATION)
531 strlcat(wrqu->name, "g", IFNAMSIZ);
532 if (ieee->mode & (IEEE_N_24G | IEEE_N_5G))
533 strlcat(wrqu->name, "/n", IFNAMSIZ);
535 if((ieee->state == IEEE80211_LINKED) ||
536 (ieee->state == IEEE80211_LINKED_SCANNING))
537 strlcat(wrqu->name, " link", IFNAMSIZ);
538 else if(ieee->state != IEEE80211_NOLINK)
539 strlcat(wrqu->name, " .....", IFNAMSIZ);
546 /* this is mostly stolen from hostap */
547 int ieee80211_wx_set_power(struct ieee80211_device *ieee,
548 struct iw_request_info *info,
549 union iwreq_data *wrqu, char *extra)
554 (!ieee->sta_wake_up) ||
555 // (!ieee->ps_request_tx_ack) ||
556 (!ieee->enter_sleep_state) ||
557 (!ieee->ps_is_queue_empty)){
559 // printk("ERROR. PS mode is tryied to be use but driver missed a callback\n\n");
566 if (wrqu->power.disabled){
567 ieee->ps = IEEE80211_PS_DISABLED;
570 if (wrqu->power.flags & IW_POWER_TIMEOUT) {
571 //ieee->ps_period = wrqu->power.value / 1000;
572 ieee->ps_timeout = wrqu->power.value / 1000;
575 if (wrqu->power.flags & IW_POWER_PERIOD) {
577 //ieee->ps_timeout = wrqu->power.value / 1000;
578 ieee->ps_period = wrqu->power.value / 1000;
582 switch (wrqu->power.flags & IW_POWER_MODE) {
583 case IW_POWER_UNICAST_R:
584 ieee->ps = IEEE80211_PS_UNICAST;
586 case IW_POWER_MULTICAST_R:
587 ieee->ps = IEEE80211_PS_MBCAST;
590 ieee->ps = IEEE80211_PS_UNICAST | IEEE80211_PS_MBCAST;
594 // ieee->ps = IEEE80211_PS_DISABLED;
608 /* this is stolen from hostap */
609 int ieee80211_wx_get_power(struct ieee80211_device *ieee,
610 struct iw_request_info *info,
611 union iwreq_data *wrqu, char *extra)
617 if(ieee->ps == IEEE80211_PS_DISABLED){
618 wrqu->power.disabled = 1;
622 wrqu->power.disabled = 0;
624 if ((wrqu->power.flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
625 wrqu->power.flags = IW_POWER_TIMEOUT;
626 wrqu->power.value = ieee->ps_timeout * 1000;
628 // ret = -EOPNOTSUPP;
630 wrqu->power.flags = IW_POWER_PERIOD;
631 wrqu->power.value = ieee->ps_period * 1000;
632 //ieee->current_network.dtim_period * ieee->current_network.beacon_interval * 1024;
635 if ((ieee->ps & (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST)) == (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST))
636 wrqu->power.flags |= IW_POWER_ALL_R;
637 else if (ieee->ps & IEEE80211_PS_MBCAST)
638 wrqu->power.flags |= IW_POWER_MULTICAST_R;
640 wrqu->power.flags |= IW_POWER_UNICAST_R;
648 EXPORT_SYMBOL(ieee80211_wx_get_essid);
649 EXPORT_SYMBOL(ieee80211_wx_set_essid);
650 EXPORT_SYMBOL(ieee80211_wx_set_rate);
651 EXPORT_SYMBOL(ieee80211_wx_get_rate);
652 EXPORT_SYMBOL(ieee80211_wx_set_wap);
653 EXPORT_SYMBOL(ieee80211_wx_get_wap);
654 EXPORT_SYMBOL(ieee80211_wx_set_mode);
655 EXPORT_SYMBOL(ieee80211_wx_get_mode);
656 EXPORT_SYMBOL(ieee80211_wx_set_scan);
657 EXPORT_SYMBOL(ieee80211_wx_get_freq);
658 EXPORT_SYMBOL(ieee80211_wx_set_freq);
659 EXPORT_SYMBOL(ieee80211_wx_set_rawtx);
660 EXPORT_SYMBOL(ieee80211_wx_get_name);
661 EXPORT_SYMBOL(ieee80211_wx_set_power);
662 EXPORT_SYMBOL(ieee80211_wx_get_power);
663 EXPORT_SYMBOL(ieee80211_wlan_frequencies);
664 EXPORT_SYMBOL(ieee80211_wx_set_rts);
665 EXPORT_SYMBOL(ieee80211_wx_get_rts);