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) {
75 ieee80211_stop_send_beacons(ieee);
76 ieee80211_start_send_beacons(ieee);
85 EXPORT_SYMBOL(ieee80211_wx_set_freq);
87 int ieee80211_wx_get_freq(struct ieee80211_device *ieee,
88 struct iw_request_info *a,
89 union iwreq_data *wrqu, char *b)
91 struct iw_freq *fwrq = &wrqu->freq;
93 if (ieee->current_network.channel == 0)
95 /* NM 0.7.0 will not accept channel any more. */
96 fwrq->m = ieee80211_wlan_frequencies[ieee->current_network.channel-1] * 100000;
98 /* fwrq->m = ieee->current_network.channel; */
103 EXPORT_SYMBOL(ieee80211_wx_get_freq);
105 int ieee80211_wx_get_wap(struct ieee80211_device *ieee,
106 struct iw_request_info *info,
107 union iwreq_data *wrqu, char *extra)
111 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
113 if (ieee->iw_mode == IW_MODE_MONITOR)
116 /* We want avoid to give to the user inconsistent infos*/
117 spin_lock_irqsave(&ieee->lock, flags);
119 if (ieee->state != IEEE80211_LINKED &&
120 ieee->state != IEEE80211_LINKED_SCANNING &&
123 eth_zero_addr(wrqu->ap_addr.sa_data);
125 memcpy(wrqu->ap_addr.sa_data,
126 ieee->current_network.bssid, ETH_ALEN);
128 spin_unlock_irqrestore(&ieee->lock, flags);
132 EXPORT_SYMBOL(ieee80211_wx_get_wap);
134 int ieee80211_wx_set_wap(struct ieee80211_device *ieee,
135 struct iw_request_info *info,
136 union iwreq_data *awrq,
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);
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 = !is_zero_ether_addr(temp->sa_data);
171 spin_unlock_irqrestore(&ieee->lock, flags);
174 ieee80211_start_protocol(ieee);
179 EXPORT_SYMBOL(ieee80211_wx_set_wap);
181 int ieee80211_wx_get_essid(struct ieee80211_device *ieee, struct iw_request_info *a, union iwreq_data *wrqu, char *b)
186 if (ieee->iw_mode == IW_MODE_MONITOR)
189 /* We want avoid to give to the user inconsistent infos*/
190 spin_lock_irqsave(&ieee->lock, flags);
192 if (ieee->current_network.ssid[0] == '\0' ||
193 ieee->current_network.ssid_len == 0) {
198 if (ieee->state != IEEE80211_LINKED &&
199 ieee->state != IEEE80211_LINKED_SCANNING &&
200 ieee->ssid_set == 0) {
204 len = ieee->current_network.ssid_len;
205 wrqu->essid.length = len;
206 strncpy(b, ieee->current_network.ssid, len);
207 wrqu->essid.flags = 1;
210 spin_unlock_irqrestore(&ieee->lock, flags);
215 EXPORT_SYMBOL(ieee80211_wx_get_essid);
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. */
228 EXPORT_SYMBOL(ieee80211_wx_set_rate);
230 int ieee80211_wx_get_rate(struct ieee80211_device *ieee,
231 struct iw_request_info *info,
232 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;
251 if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
252 wrqu->rts.value > MAX_RTS_THRESHOLD)
254 ieee->rts = wrqu->rts.value;
258 EXPORT_SYMBOL(ieee80211_wx_set_rts);
260 int ieee80211_wx_get_rts(struct ieee80211_device *ieee,
261 struct iw_request_info *info,
262 union iwreq_data *wrqu, char *extra)
264 wrqu->rts.value = ieee->rts;
265 wrqu->rts.fixed = 0; /* no auto select */
266 wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
269 EXPORT_SYMBOL(ieee80211_wx_get_rts);
271 int ieee80211_wx_set_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
272 union iwreq_data *wrqu, char *b)
275 ieee->sync_scan_hurryup = 1;
279 if (wrqu->mode == ieee->iw_mode)
282 if (wrqu->mode == IW_MODE_MONITOR)
283 ieee->dev->type = ARPHRD_IEEE80211;
285 ieee->dev->type = ARPHRD_ETHER;
287 if (!ieee->proto_started) {
288 ieee->iw_mode = wrqu->mode;
290 ieee80211_stop_protocol(ieee);
291 ieee->iw_mode = wrqu->mode;
292 ieee80211_start_protocol(ieee);
299 EXPORT_SYMBOL(ieee80211_wx_set_mode);
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;
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) {
346 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
347 ieee->LinkDetectInfo.NumRecvDataInPeriod = 1;
349 if (ieee->data_hard_resume)
350 ieee->data_hard_resume(ieee->dev);
352 if (ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
353 ieee80211_start_send_beacons(ieee);
355 netif_carrier_on(ieee->dev);
360 int ieee80211_wx_set_scan(struct ieee80211_device *ieee, struct iw_request_info *a,
361 union iwreq_data *wrqu, char *b)
367 if (ieee->iw_mode == IW_MODE_MONITOR || !(ieee->proto_started)) {
372 if (ieee->state == IEEE80211_LINKED) {
373 queue_work(ieee->wq, &ieee->wx_sync_scan_wq);
374 /* intentionally forget to up sem */
382 EXPORT_SYMBOL(ieee80211_wx_set_scan);
384 int ieee80211_wx_set_essid(struct ieee80211_device *ieee,
385 struct iw_request_info *a,
386 union iwreq_data *wrqu, char *extra)
393 ieee->sync_scan_hurryup = 1;
396 proto_started = ieee->proto_started;
398 if (wrqu->essid.length > IW_ESSID_MAX_SIZE) {
403 if (ieee->iw_mode == IW_MODE_MONITOR) {
409 ieee80211_stop_protocol(ieee);
412 /* this is just to be sure that the GET wx callback
413 * has consisten infos. not needed otherwise
415 spin_lock_irqsave(&ieee->lock, flags);
417 if (wrqu->essid.flags && wrqu->essid.length) {
418 /* first flush current network.ssid */
419 len = ((wrqu->essid.length-1) < IW_ESSID_MAX_SIZE) ? (wrqu->essid.length-1) : IW_ESSID_MAX_SIZE;
420 strncpy(ieee->current_network.ssid, extra, len+1);
421 ieee->current_network.ssid_len = len+1;
425 ieee->current_network.ssid[0] = '\0';
426 ieee->current_network.ssid_len = 0;
428 spin_unlock_irqrestore(&ieee->lock, flags);
431 ieee80211_start_protocol(ieee);
436 EXPORT_SYMBOL(ieee80211_wx_set_essid);
438 int ieee80211_wx_get_mode(struct ieee80211_device *ieee, struct iw_request_info *a,
439 union iwreq_data *wrqu, char *b)
442 wrqu->mode = ieee->iw_mode;
445 EXPORT_SYMBOL(ieee80211_wx_get_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) {
467 if (prev == 0 && ieee->raw_tx) {
468 if (ieee->data_hard_resume)
469 ieee->data_hard_resume(ieee->dev);
471 netif_carrier_on(ieee->dev);
474 if (prev && ieee->raw_tx == 1)
475 netif_carrier_off(ieee->dev);
482 EXPORT_SYMBOL(ieee80211_wx_set_rawtx);
484 int ieee80211_wx_get_name(struct ieee80211_device *ieee,
485 struct iw_request_info *info,
486 union iwreq_data *wrqu, char *extra)
488 strlcpy(wrqu->name, "802.11", IFNAMSIZ);
489 if (ieee->modulation & IEEE80211_CCK_MODULATION) {
490 strlcat(wrqu->name, "b", IFNAMSIZ);
491 if (ieee->modulation & IEEE80211_OFDM_MODULATION)
492 strlcat(wrqu->name, "/g", IFNAMSIZ);
493 } else if (ieee->modulation & IEEE80211_OFDM_MODULATION) {
494 strlcat(wrqu->name, "g", IFNAMSIZ);
497 if (ieee->mode & (IEEE_N_24G | IEEE_N_5G))
498 strlcat(wrqu->name, "/n", IFNAMSIZ);
500 if ((ieee->state == IEEE80211_LINKED) ||
501 (ieee->state == IEEE80211_LINKED_SCANNING))
502 strlcat(wrqu->name, " linked", IFNAMSIZ);
503 else if (ieee->state != IEEE80211_NOLINK)
504 strlcat(wrqu->name, " link..", IFNAMSIZ);
508 EXPORT_SYMBOL(ieee80211_wx_get_name);
510 /* this is mostly stolen from hostap */
511 int ieee80211_wx_set_power(struct ieee80211_device *ieee,
512 struct iw_request_info *info,
513 union iwreq_data *wrqu, char *extra)
519 if (wrqu->power.disabled) {
520 ieee->ps = IEEE80211_PS_DISABLED;
523 if (wrqu->power.flags & IW_POWER_TIMEOUT) {
524 /* ieee->ps_period = wrqu->power.value / 1000; */
525 ieee->ps_timeout = wrqu->power.value / 1000;
528 if (wrqu->power.flags & IW_POWER_PERIOD) {
530 /* ieee->ps_timeout = wrqu->power.value / 1000; */
531 ieee->ps_period = wrqu->power.value / 1000;
532 /* wrq->value / 1024; */
535 switch (wrqu->power.flags & IW_POWER_MODE) {
536 case IW_POWER_UNICAST_R:
537 ieee->ps = IEEE80211_PS_UNICAST;
539 case IW_POWER_MULTICAST_R:
540 ieee->ps = IEEE80211_PS_MBCAST;
543 ieee->ps = IEEE80211_PS_UNICAST | IEEE80211_PS_MBCAST;
547 /* ieee->ps = IEEE80211_PS_DISABLED; */
560 EXPORT_SYMBOL(ieee80211_wx_set_power);
562 /* this is stolen from hostap */
563 int ieee80211_wx_get_power(struct ieee80211_device *ieee,
564 struct iw_request_info *info,
565 union iwreq_data *wrqu, char *extra)
569 if (ieee->ps == IEEE80211_PS_DISABLED) {
570 wrqu->power.disabled = 1;
574 wrqu->power.disabled = 0;
576 if ((wrqu->power.flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
577 wrqu->power.flags = IW_POWER_TIMEOUT;
578 wrqu->power.value = ieee->ps_timeout * 1000;
580 /* ret = -EOPNOTSUPP; */
582 wrqu->power.flags = IW_POWER_PERIOD;
583 wrqu->power.value = ieee->ps_period * 1000;
584 /* ieee->current_network.dtim_period * ieee->current_network.beacon_interval * 1024; */
587 if ((ieee->ps & (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST)) == (IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST))
588 wrqu->power.flags |= IW_POWER_ALL_R;
589 else if (ieee->ps & IEEE80211_PS_MBCAST)
590 wrqu->power.flags |= IW_POWER_MULTICAST_R;
592 wrqu->power.flags |= IW_POWER_UNICAST_R;
599 EXPORT_SYMBOL(ieee80211_wx_get_power);