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>
21 /* FIXME: add A freqs */
23 const long rtllib_wlan_frequencies[] = {
24 2412, 2417, 2422, 2427,
25 2432, 2437, 2442, 2447,
26 2452, 2457, 2462, 2467,
29 EXPORT_SYMBOL(rtllib_wlan_frequencies);
32 int rtllib_wx_set_freq(struct rtllib_device *ieee, struct iw_request_info *a,
33 union iwreq_data *wrqu, char *b)
36 struct iw_freq *fwrq = &wrqu->freq;
38 mutex_lock(&ieee->wx_mutex);
40 if (ieee->iw_mode == IW_MODE_INFRA) {
45 /* if setting by freq convert to channel */
47 if ((fwrq->m >= (int) 2.412e8 &&
48 fwrq->m <= (int) 2.487e8)) {
49 int f = fwrq->m / 100000;
52 while ((c < 14) && (f != rtllib_wlan_frequencies[c]))
55 /* hack to fall through */
61 if (fwrq->e > 0 || fwrq->m > 14 || fwrq->m < 1) {
65 } else { /* Set the channel */
67 if (ieee->active_channel_map[fwrq->m] != 1) {
71 ieee->current_network.channel = fwrq->m;
72 ieee->set_chan(ieee->dev, ieee->current_network.channel);
74 if (ieee->iw_mode == IW_MODE_ADHOC ||
75 ieee->iw_mode == IW_MODE_MASTER)
76 if (ieee->state == RTLLIB_LINKED) {
77 rtllib_stop_send_beacons(ieee);
78 rtllib_start_send_beacons(ieee);
84 mutex_unlock(&ieee->wx_mutex);
87 EXPORT_SYMBOL(rtllib_wx_set_freq);
90 int rtllib_wx_get_freq(struct rtllib_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 fwrq->m = rtllib_wlan_frequencies[ieee->current_network.channel-1] *
103 EXPORT_SYMBOL(rtllib_wx_get_freq);
105 int rtllib_wx_get_wap(struct rtllib_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 != RTLLIB_LINKED &&
120 ieee->state != RTLLIB_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(rtllib_wx_get_wap);
135 int rtllib_wx_set_wap(struct rtllib_device *ieee,
136 struct iw_request_info *info,
137 union iwreq_data *awrq,
144 short ifup = ieee->proto_started;
145 struct sockaddr *temp = (struct sockaddr *)awrq;
147 rtllib_stop_scan_syncro(ieee);
149 mutex_lock(&ieee->wx_mutex);
150 /* use ifconfig hw ether */
151 if (ieee->iw_mode == IW_MODE_MASTER) {
156 if (temp->sa_family != ARPHRD_ETHER) {
161 if (is_zero_ether_addr(temp->sa_data)) {
162 spin_lock_irqsave(&ieee->lock, flags);
163 ether_addr_copy(ieee->current_network.bssid, temp->sa_data);
165 spin_unlock_irqrestore(&ieee->lock, flags);
172 rtllib_stop_protocol(ieee, true);
174 /* just to avoid to give inconsistent infos in the
175 * get wx method. not really needed otherwise
177 spin_lock_irqsave(&ieee->lock, flags);
179 ieee->cannot_notify = false;
180 ether_addr_copy(ieee->current_network.bssid, temp->sa_data);
181 ieee->wap_set = !is_zero_ether_addr(temp->sa_data);
183 spin_unlock_irqrestore(&ieee->lock, flags);
186 rtllib_start_protocol(ieee);
188 mutex_unlock(&ieee->wx_mutex);
191 EXPORT_SYMBOL(rtllib_wx_set_wap);
193 int rtllib_wx_get_essid(struct rtllib_device *ieee, struct iw_request_info *a,
194 union iwreq_data *wrqu, char *b)
199 if (ieee->iw_mode == IW_MODE_MONITOR)
202 /* We want avoid to give to the user inconsistent infos*/
203 spin_lock_irqsave(&ieee->lock, flags);
205 if (ieee->current_network.ssid[0] == '\0' ||
206 ieee->current_network.ssid_len == 0) {
211 if (ieee->state != RTLLIB_LINKED &&
212 ieee->state != RTLLIB_LINKED_SCANNING &&
213 ieee->ssid_set == 0) {
217 len = ieee->current_network.ssid_len;
218 wrqu->essid.length = len;
219 strncpy(b, ieee->current_network.ssid, len);
220 wrqu->essid.flags = 1;
223 spin_unlock_irqrestore(&ieee->lock, flags);
228 EXPORT_SYMBOL(rtllib_wx_get_essid);
230 int rtllib_wx_set_rate(struct rtllib_device *ieee,
231 struct iw_request_info *info,
232 union iwreq_data *wrqu, char *extra)
235 u32 target_rate = wrqu->bitrate.value;
237 ieee->rate = target_rate/100000;
240 EXPORT_SYMBOL(rtllib_wx_set_rate);
242 int rtllib_wx_get_rate(struct rtllib_device *ieee,
243 struct iw_request_info *info,
244 union iwreq_data *wrqu, char *extra)
248 tmp_rate = TxCountToDataRate(ieee,
249 ieee->softmac_stats.CurrentShowTxate);
250 wrqu->bitrate.value = tmp_rate * 500000;
254 EXPORT_SYMBOL(rtllib_wx_get_rate);
257 int rtllib_wx_set_rts(struct rtllib_device *ieee,
258 struct iw_request_info *info,
259 union iwreq_data *wrqu, char *extra)
261 if (wrqu->rts.disabled || !wrqu->rts.fixed)
262 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;
271 EXPORT_SYMBOL(rtllib_wx_set_rts);
273 int rtllib_wx_get_rts(struct rtllib_device *ieee,
274 struct iw_request_info *info,
275 union iwreq_data *wrqu, char *extra)
277 wrqu->rts.value = ieee->rts;
278 wrqu->rts.fixed = 0; /* no auto select */
279 wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
282 EXPORT_SYMBOL(rtllib_wx_get_rts);
284 int rtllib_wx_set_mode(struct rtllib_device *ieee, struct iw_request_info *a,
285 union iwreq_data *wrqu, char *b)
287 int set_mode_status = 0;
289 rtllib_stop_scan_syncro(ieee);
290 mutex_lock(&ieee->wx_mutex);
291 switch (wrqu->mode) {
292 case IW_MODE_MONITOR:
297 wrqu->mode = IW_MODE_INFRA;
300 set_mode_status = -EINVAL;
304 if (wrqu->mode == ieee->iw_mode)
307 if (wrqu->mode == IW_MODE_MONITOR) {
308 ieee->dev->type = ARPHRD_IEEE80211;
309 rtllib_EnableNetMonitorMode(ieee->dev, false);
311 ieee->dev->type = ARPHRD_ETHER;
312 if (ieee->iw_mode == IW_MODE_MONITOR)
313 rtllib_DisableNetMonitorMode(ieee->dev, false);
316 if (!ieee->proto_started) {
317 ieee->iw_mode = wrqu->mode;
319 rtllib_stop_protocol(ieee, true);
320 ieee->iw_mode = wrqu->mode;
321 rtllib_start_protocol(ieee);
325 mutex_unlock(&ieee->wx_mutex);
326 return set_mode_status;
328 EXPORT_SYMBOL(rtllib_wx_set_mode);
330 void rtllib_wx_sync_scan_wq(void *data)
332 struct rtllib_device *ieee = container_of_work_rsl(data,
333 struct rtllib_device, wx_sync_scan_wq);
335 enum ht_extchnl_offset chan_offset = 0;
336 enum ht_channel_width bandwidth = 0;
339 if (!(ieee->softmac_features & IEEE_SOFTMAC_SCAN)) {
340 rtllib_start_scan_syncro(ieee, 0);
344 chan = ieee->current_network.channel;
346 if (ieee->LeisurePSLeave)
347 ieee->LeisurePSLeave(ieee->dev);
348 /* notify AP to be in PS mode */
349 rtllib_sta_ps_send_null_frame(ieee, 1);
350 rtllib_sta_ps_send_null_frame(ieee, 1);
352 rtllib_stop_all_queues(ieee);
354 if (ieee->data_hard_stop)
355 ieee->data_hard_stop(ieee->dev);
356 rtllib_stop_send_beacons(ieee);
357 ieee->state = RTLLIB_LINKED_SCANNING;
358 ieee->link_change(ieee->dev);
359 /* wait for ps packet to be kicked out successfully */
362 if (ieee->ScanOperationBackupHandler)
363 ieee->ScanOperationBackupHandler(ieee->dev, SCAN_OPT_BACKUP);
365 if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT &&
366 ieee->pHTInfo->bCurBW40MHz) {
368 chan_offset = ieee->pHTInfo->CurSTAExtChnlOffset;
369 bandwidth = (enum ht_channel_width)ieee->pHTInfo->bCurBW40MHz;
370 RT_TRACE(COMP_DBG, "Scan in 40M, force to 20M first:%d, %d\n",
371 chan_offset, bandwidth);
372 ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20,
373 HT_EXTCHNL_OFFSET_NO_EXT);
376 rtllib_start_scan_syncro(ieee, 0);
379 RT_TRACE(COMP_DBG, "Scan in 20M, back to 40M\n");
380 if (chan_offset == HT_EXTCHNL_OFFSET_UPPER)
381 ieee->set_chan(ieee->dev, chan + 2);
382 else if (chan_offset == HT_EXTCHNL_OFFSET_LOWER)
383 ieee->set_chan(ieee->dev, chan - 2);
385 ieee->set_chan(ieee->dev, chan);
386 ieee->SetBWModeHandler(ieee->dev, bandwidth, chan_offset);
388 ieee->set_chan(ieee->dev, chan);
391 if (ieee->ScanOperationBackupHandler)
392 ieee->ScanOperationBackupHandler(ieee->dev, SCAN_OPT_RESTORE);
394 ieee->state = RTLLIB_LINKED;
395 ieee->link_change(ieee->dev);
397 /* Notify AP that I wake up again */
398 rtllib_sta_ps_send_null_frame(ieee, 0);
400 if (ieee->LinkDetectInfo.NumRecvBcnInPeriod == 0 ||
401 ieee->LinkDetectInfo.NumRecvDataInPeriod == 0) {
402 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
403 ieee->LinkDetectInfo.NumRecvDataInPeriod = 1;
406 if (ieee->data_hard_resume)
407 ieee->data_hard_resume(ieee->dev);
409 if (ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
410 rtllib_start_send_beacons(ieee);
412 rtllib_wake_all_queues(ieee);
415 mutex_unlock(&ieee->wx_mutex);
419 int rtllib_wx_set_scan(struct rtllib_device *ieee, struct iw_request_info *a,
420 union iwreq_data *wrqu, char *b)
424 mutex_lock(&ieee->wx_mutex);
426 if (ieee->iw_mode == IW_MODE_MONITOR || !(ieee->proto_started)) {
431 if (ieee->state == RTLLIB_LINKED) {
432 schedule_work(&ieee->wx_sync_scan_wq);
433 /* intentionally forget to up sem */
438 mutex_unlock(&ieee->wx_mutex);
441 EXPORT_SYMBOL(rtllib_wx_set_scan);
443 int rtllib_wx_set_essid(struct rtllib_device *ieee,
444 struct iw_request_info *a,
445 union iwreq_data *wrqu, char *extra)
452 rtllib_stop_scan_syncro(ieee);
453 mutex_lock(&ieee->wx_mutex);
455 proto_started = ieee->proto_started;
457 len = min_t(__u16, wrqu->essid.length, IW_ESSID_MAX_SIZE);
459 if (ieee->iw_mode == IW_MODE_MONITOR) {
464 for (i = 0; i < len; i++) {
472 rtllib_stop_protocol(ieee, true);
475 /* this is just to be sure that the GET wx callback
476 * has consistent infos. not needed otherwise
478 spin_lock_irqsave(&ieee->lock, flags);
480 if (wrqu->essid.flags && wrqu->essid.length) {
481 strncpy(ieee->current_network.ssid, extra, len);
482 ieee->current_network.ssid_len = len;
483 ieee->cannot_notify = false;
487 ieee->current_network.ssid[0] = '\0';
488 ieee->current_network.ssid_len = 0;
490 spin_unlock_irqrestore(&ieee->lock, flags);
493 rtllib_start_protocol(ieee);
495 mutex_unlock(&ieee->wx_mutex);
498 EXPORT_SYMBOL(rtllib_wx_set_essid);
500 int rtllib_wx_get_mode(struct rtllib_device *ieee, struct iw_request_info *a,
501 union iwreq_data *wrqu, char *b)
503 wrqu->mode = ieee->iw_mode;
506 EXPORT_SYMBOL(rtllib_wx_get_mode);
508 int rtllib_wx_set_rawtx(struct rtllib_device *ieee,
509 struct iw_request_info *info,
510 union iwreq_data *wrqu, char *extra)
513 int *parms = (int *)extra;
514 int enable = (parms[0] > 0);
515 short prev = ieee->raw_tx;
517 mutex_lock(&ieee->wx_mutex);
524 netdev_info(ieee->dev, "raw TX is %s\n",
525 ieee->raw_tx ? "enabled" : "disabled");
527 if (ieee->iw_mode == IW_MODE_MONITOR) {
528 if (prev == 0 && ieee->raw_tx) {
529 if (ieee->data_hard_resume)
530 ieee->data_hard_resume(ieee->dev);
532 netif_carrier_on(ieee->dev);
535 if (prev && ieee->raw_tx == 1)
536 netif_carrier_off(ieee->dev);
539 mutex_unlock(&ieee->wx_mutex);
543 EXPORT_SYMBOL(rtllib_wx_set_rawtx);
545 int rtllib_wx_get_name(struct rtllib_device *ieee,
546 struct iw_request_info *info,
547 union iwreq_data *wrqu, char *extra)
549 strcpy(wrqu->name, "802.11");
551 if (ieee->modulation & RTLLIB_CCK_MODULATION)
552 strcat(wrqu->name, "b");
553 if (ieee->modulation & RTLLIB_OFDM_MODULATION)
554 strcat(wrqu->name, "g");
555 if (ieee->mode & (IEEE_N_24G | IEEE_N_5G))
556 strcat(wrqu->name, "n");
559 EXPORT_SYMBOL(rtllib_wx_get_name);
562 /* this is mostly stolen from hostap */
563 int rtllib_wx_set_power(struct rtllib_device *ieee,
564 struct iw_request_info *info,
565 union iwreq_data *wrqu, char *extra)
569 if ((!ieee->sta_wake_up) ||
570 (!ieee->enter_sleep_state) ||
571 (!ieee->ps_is_queue_empty)) {
572 netdev_warn(ieee->dev,
573 "%s(): PS mode is tried to be use but driver missed a callback\n",
578 mutex_lock(&ieee->wx_mutex);
580 if (wrqu->power.disabled) {
581 RT_TRACE(COMP_DBG, "===>%s(): power disable\n", __func__);
582 ieee->ps = RTLLIB_PS_DISABLED;
585 if (wrqu->power.flags & IW_POWER_TIMEOUT) {
586 ieee->ps_timeout = wrqu->power.value / 1000;
587 RT_TRACE(COMP_DBG, "===>%s():ps_timeout is %d\n", __func__,
591 if (wrqu->power.flags & IW_POWER_PERIOD)
592 ieee->ps_period = wrqu->power.value / 1000;
594 switch (wrqu->power.flags & IW_POWER_MODE) {
595 case IW_POWER_UNICAST_R:
596 ieee->ps = RTLLIB_PS_UNICAST;
598 case IW_POWER_MULTICAST_R:
599 ieee->ps = RTLLIB_PS_MBCAST;
602 ieee->ps = RTLLIB_PS_UNICAST | RTLLIB_PS_MBCAST;
614 mutex_unlock(&ieee->wx_mutex);
618 EXPORT_SYMBOL(rtllib_wx_set_power);
620 /* this is stolen from hostap */
621 int rtllib_wx_get_power(struct rtllib_device *ieee,
622 struct iw_request_info *info,
623 union iwreq_data *wrqu, char *extra)
625 mutex_lock(&ieee->wx_mutex);
627 if (ieee->ps == RTLLIB_PS_DISABLED) {
628 wrqu->power.disabled = 1;
632 wrqu->power.disabled = 0;
634 if ((wrqu->power.flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
635 wrqu->power.flags = IW_POWER_TIMEOUT;
636 wrqu->power.value = ieee->ps_timeout * 1000;
638 wrqu->power.flags = IW_POWER_PERIOD;
639 wrqu->power.value = ieee->ps_period * 1000;
642 if ((ieee->ps & (RTLLIB_PS_MBCAST | RTLLIB_PS_UNICAST)) ==
643 (RTLLIB_PS_MBCAST | RTLLIB_PS_UNICAST))
644 wrqu->power.flags |= IW_POWER_ALL_R;
645 else if (ieee->ps & RTLLIB_PS_MBCAST)
646 wrqu->power.flags |= IW_POWER_MULTICAST_R;
648 wrqu->power.flags |= IW_POWER_UNICAST_R;
651 mutex_unlock(&ieee->wx_mutex);
655 EXPORT_SYMBOL(rtllib_wx_get_power);