blob: 7cda2908baee61c79c838f2d89fd88738311ce5e [file] [log] [blame]
Larry Finger94a79942011-08-23 19:00:42 -05001/* IEEE 802.11 SoftMAC layer
2 * Copyright (c) 2005 Andrea Merello <andreamrl@tiscali.it>
3 *
4 * Mostly extracted from the rtl8180-sa2400 driver for the
5 * in-kernel generic ieee802.11 stack.
6 *
7 * Some pieces of code might be stolen from ipw2100 driver
8 * copyright of who own it's copyright ;-)
9 *
10 * PS wx handler mostly stolen from hostap, copyright who
11 * own it's copyright ;-)
12 *
13 * released under the GPL
14 */
15
16
17#include "rtllib.h"
18#include "rtl_core.h"
19#ifdef ENABLE_DOT11D
20#include "dot11d.h"
21#endif
22/* FIXME: add A freqs */
23
24const long rtllib_wlan_frequencies[] = {
25 2412, 2417, 2422, 2427,
26 2432, 2437, 2442, 2447,
27 2452, 2457, 2462, 2467,
28 2472, 2484
29};
30
31
32int rtllib_wx_set_freq(struct rtllib_device *ieee, struct iw_request_info *a,
33 union iwreq_data *wrqu, char *b)
34{
35 int ret;
36 struct iw_freq *fwrq = & wrqu->freq;
37
38 down(&ieee->wx_sem);
39
40 if (ieee->iw_mode == IW_MODE_INFRA){
41 ret = 0;
42 goto out;
43 }
44
45 /* if setting by freq convert to channel */
46 if (fwrq->e == 1) {
47 if ((fwrq->m >= (int) 2.412e8 &&
48 fwrq->m <= (int) 2.487e8)) {
49 int f = fwrq->m / 100000;
50 int c = 0;
51
52 while ((c < 14) && (f != rtllib_wlan_frequencies[c]))
53 c++;
54
55 /* hack to fall through */
56 fwrq->e = 0;
57 fwrq->m = c + 1;
58 }
59 }
60
61 if (fwrq->e > 0 || fwrq->m > 14 || fwrq->m < 1 ){
62 ret = -EOPNOTSUPP;
63 goto out;
64
65 }else { /* Set the channel */
66
67#ifdef ENABLE_DOT11D
68 if (ieee->active_channel_map[fwrq->m] != 1) {
69 ret = -EINVAL;
70 goto out;
71 }
72#endif
73 ieee->current_network.channel = fwrq->m;
74 ieee->set_chan(ieee->dev, ieee->current_network.channel);
75
76 if (ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
77 if (ieee->state == RTLLIB_LINKED){
78
79 rtllib_stop_send_beacons(ieee);
80 rtllib_start_send_beacons(ieee);
81 }
82 }
83
84 ret = 0;
85out:
86 up(&ieee->wx_sem);
87 return ret;
88}
89
90
91int rtllib_wx_get_freq(struct rtllib_device *ieee,
92 struct iw_request_info *a,
93 union iwreq_data *wrqu, char *b)
94{
95 struct iw_freq *fwrq = & wrqu->freq;
96
97 if (ieee->current_network.channel == 0)
98 return -1;
99 fwrq->m = rtllib_wlan_frequencies[ieee->current_network.channel-1] * 100000;
100 fwrq->e = 1;
101 return 0;
102}
103
104int rtllib_wx_get_wap(struct rtllib_device *ieee,
105 struct iw_request_info *info,
106 union iwreq_data *wrqu, char *extra)
107{
108 unsigned long flags;
109
110 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
111
112 if (ieee->iw_mode == IW_MODE_MONITOR)
113 return -1;
114
115 /* We want avoid to give to the user inconsistent infos*/
116 spin_lock_irqsave(&ieee->lock, flags);
117
118 if (ieee->state != RTLLIB_LINKED &&
119 ieee->state != RTLLIB_LINKED_SCANNING &&
120 ieee->wap_set == 0)
121
122 memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);
123 else
124 memcpy(wrqu->ap_addr.sa_data,
125 ieee->current_network.bssid, ETH_ALEN);
126
127 spin_unlock_irqrestore(&ieee->lock, flags);
128
129 return 0;
130}
131
132
133int rtllib_wx_set_wap(struct rtllib_device *ieee,
134 struct iw_request_info *info,
135 union iwreq_data *awrq,
136 char *extra)
137{
138
139 int ret = 0;
140 u8 zero[] = {0,0,0,0,0,0};
141 unsigned long flags;
142
143 short ifup = ieee->proto_started;
144 struct sockaddr *temp = (struct sockaddr *)awrq;
145
146 rtllib_stop_scan_syncro(ieee);
147
148 down(&ieee->wx_sem);
149 /* use ifconfig hw ether */
150 if (ieee->iw_mode == IW_MODE_MASTER){
151 ret = -1;
152 goto out;
153 }
154
155 if (temp->sa_family != ARPHRD_ETHER){
156 ret = -EINVAL;
157 goto out;
158 }
159
160 if (memcmp(temp->sa_data, zero,ETH_ALEN) == 0){
161 spin_lock_irqsave(&ieee->lock, flags);
162 memcpy(ieee->current_network.bssid, temp->sa_data, ETH_ALEN);
163 ieee->wap_set = 0;
164 spin_unlock_irqrestore(&ieee->lock, flags);
165 ret = -1;
166 goto out;
167 }
168
169
170 if (ifup)
171 rtllib_stop_protocol(ieee,true);
172
173 /* just to avoid to give inconsistent infos in the
174 * get wx method. not really needed otherwise
175 */
176 spin_lock_irqsave(&ieee->lock, flags);
177
178 ieee->cannot_notify = false;
179 memcpy(ieee->current_network.bssid, temp->sa_data, ETH_ALEN);
180 ieee->wap_set = (memcmp(temp->sa_data, zero,ETH_ALEN)!=0);
181
182 spin_unlock_irqrestore(&ieee->lock, flags);
183
184 if (ifup)
185 rtllib_start_protocol(ieee);
186out:
187 up(&ieee->wx_sem);
188 return ret;
189}
190
191 int rtllib_wx_get_essid(struct rtllib_device *ieee, struct iw_request_info *a,union iwreq_data *wrqu,char *b)
192{
193 int len,ret = 0;
194 unsigned long flags;
195
196 if (ieee->iw_mode == IW_MODE_MONITOR)
197 return -1;
198
199 /* We want avoid to give to the user inconsistent infos*/
200 spin_lock_irqsave(&ieee->lock, flags);
201
202 if (ieee->current_network.ssid[0] == '\0' ||
203 ieee->current_network.ssid_len == 0){
204 ret = -1;
205 goto out;
206 }
207
208 if (ieee->state != RTLLIB_LINKED &&
209 ieee->state != RTLLIB_LINKED_SCANNING &&
210 ieee->ssid_set == 0){
211 ret = -1;
212 goto out;
213 }
214 len = ieee->current_network.ssid_len;
215 wrqu->essid.length = len;
216 strncpy(b,ieee->current_network.ssid,len);
217 wrqu->essid.flags = 1;
218
219out:
220 spin_unlock_irqrestore(&ieee->lock, flags);
221
222 return ret;
223
224}
225
226int rtllib_wx_set_rate(struct rtllib_device *ieee,
227 struct iw_request_info *info,
228 union iwreq_data *wrqu, char *extra)
229{
230
231 u32 target_rate = wrqu->bitrate.value;
232
233 ieee->rate = target_rate/100000;
234 return 0;
235}
236
237int rtllib_wx_get_rate(struct rtllib_device *ieee,
238 struct iw_request_info *info,
239 union iwreq_data *wrqu, char *extra)
240{
241 u32 tmp_rate = 0;
242#if defined RTL8192SU
243 if (ieee->mode & (IEEE_A | IEEE_B | IEEE_G))
244 tmp_rate = ieee->rate;
245 else if (ieee->mode & IEEE_N_5G)
246 tmp_rate = 580;
247 else if (ieee->mode & IEEE_N_24G) {
248 if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
249 tmp_rate = HTHalfMcsToDataRate(ieee, 15);
250 else
251 tmp_rate = HTMcsToDataRate(ieee, 15);
252 }
253#elif defined RTL8192SE || defined RTL8192CE
254 tmp_rate = ieee->rtl_11n_user_show_rates(ieee->dev);
255#else
256 tmp_rate = TxCountToDataRate(ieee, ieee->softmac_stats.CurrentShowTxate);
257#endif
258 wrqu->bitrate.value = tmp_rate * 500000;
259
260 return 0;
261}
262
263
264int rtllib_wx_set_rts(struct rtllib_device *ieee,
265 struct iw_request_info *info,
266 union iwreq_data *wrqu, char *extra)
267{
268 if (wrqu->rts.disabled || !wrqu->rts.fixed)
269 ieee->rts = DEFAULT_RTS_THRESHOLD;
270 else
271 {
272 if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
273 wrqu->rts.value > MAX_RTS_THRESHOLD)
274 return -EINVAL;
275 ieee->rts = wrqu->rts.value;
276 }
277 return 0;
278}
279
280int rtllib_wx_get_rts(struct rtllib_device *ieee,
281 struct iw_request_info *info,
282 union iwreq_data *wrqu, char *extra)
283{
284 wrqu->rts.value = ieee->rts;
285 wrqu->rts.fixed = 0; /* no auto select */
286 wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
287 return 0;
288}
289
290int rtllib_wx_set_mode(struct rtllib_device *ieee, struct iw_request_info *a,
291 union iwreq_data *wrqu, char *b)
292{
293 int set_mode_status = 0;
294
295 rtllib_stop_scan_syncro(ieee);
296 down(&ieee->wx_sem);
297 switch (wrqu->mode) {
298 case IW_MODE_MONITOR:
299 case IW_MODE_ADHOC:
300 case IW_MODE_INFRA:
301 break;
302 case IW_MODE_AUTO:
303 wrqu->mode = IW_MODE_INFRA;
304 break;
305 default:
306 set_mode_status = -EINVAL;
307 goto out;
308 }
309
310 if (wrqu->mode == ieee->iw_mode)
311 goto out;
312
313 if (wrqu->mode == IW_MODE_MONITOR) {
314#if defined(RTLLIB_RADIOTAP) && (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,10))
315 ieee->dev->type = ARPHRD_IEEE80211_RADIOTAP;
316#else
317 ieee->dev->type = ARPHRD_IEEE80211;
318#endif
319 rtllib_EnableNetMonitorMode(ieee->dev,false);
320
321 } else {
322 ieee->dev->type = ARPHRD_ETHER;
323 if (ieee->iw_mode == IW_MODE_MONITOR)
324 rtllib_DisableNetMonitorMode(ieee->dev,false);
325 }
326
327 if (!ieee->proto_started) {
328 ieee->iw_mode = wrqu->mode;
329 } else {
330 rtllib_stop_protocol(ieee,true);
331 ieee->iw_mode = wrqu->mode;
332 rtllib_start_protocol(ieee);
333 }
334
335out:
336 up(&ieee->wx_sem);
337 return set_mode_status;
338}
339
340void rtllib_wx_sync_scan_wq(void *data)
341{
342 struct rtllib_device *ieee = container_of_work_rsl(data, struct rtllib_device, wx_sync_scan_wq);
343 short chan;
344 HT_EXTCHNL_OFFSET chan_offset=0;
345 HT_CHANNEL_WIDTH bandwidth=0;
346 int b40M = 0;
347 static int count = 0;
348
349 if (!(ieee->softmac_features & IEEE_SOFTMAC_SCAN)){
350 rtllib_start_scan_syncro(ieee, 0);
351 goto out;
352 }
353
354 chan = ieee->current_network.channel;
355
356 if (ieee->LeisurePSLeave)
357 ieee->LeisurePSLeave(ieee->dev);
358 /* notify AP to be in PS mode */
359 rtllib_sta_ps_send_null_frame(ieee, 1);
360 rtllib_sta_ps_send_null_frame(ieee, 1);
361
362 rtllib_stop_all_queues(ieee);
363
364 if (ieee->data_hard_stop)
365 ieee->data_hard_stop(ieee->dev);
366 rtllib_stop_send_beacons(ieee);
367 ieee->state = RTLLIB_LINKED_SCANNING;
368 ieee->link_change(ieee->dev);
369 /* wait for ps packet to be kicked out successfully */
370 msleep(50);
371
372 if (ieee->ScanOperationBackupHandler)
373 ieee->ScanOperationBackupHandler(ieee->dev,SCAN_OPT_BACKUP);
374
375 if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT && ieee->pHTInfo->bCurBW40MHz) {
376 b40M = 1;
377 chan_offset = ieee->pHTInfo->CurSTAExtChnlOffset;
378 bandwidth = (HT_CHANNEL_WIDTH)ieee->pHTInfo->bCurBW40MHz;
379 RT_TRACE(COMP_DBG, "Scan in 40M, force to 20M first:%d, %d\n", chan_offset, bandwidth);
380 ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
381 }
382
383 rtllib_start_scan_syncro(ieee, 0);
384
385 if (b40M) {
386 RT_TRACE(COMP_DBG, "Scan in 20M, back to 40M\n");
387 if (chan_offset == HT_EXTCHNL_OFFSET_UPPER)
388 ieee->set_chan(ieee->dev, chan + 2);
389 else if (chan_offset == HT_EXTCHNL_OFFSET_LOWER)
390 ieee->set_chan(ieee->dev, chan - 2);
391 else
392 ieee->set_chan(ieee->dev, chan);
393 ieee->SetBWModeHandler(ieee->dev, bandwidth, chan_offset);
394 } else {
395 ieee->set_chan(ieee->dev, chan);
396 }
397
398 if (ieee->ScanOperationBackupHandler)
399 ieee->ScanOperationBackupHandler(ieee->dev,SCAN_OPT_RESTORE);
400
401 ieee->state = RTLLIB_LINKED;
402 ieee->link_change(ieee->dev);
403
404 /* Notify AP that I wake up again */
405 rtllib_sta_ps_send_null_frame(ieee, 0);
406
407 if (ieee->LinkDetectInfo.NumRecvBcnInPeriod == 0 ||
408 ieee->LinkDetectInfo.NumRecvDataInPeriod == 0 ) {
409 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
410 ieee->LinkDetectInfo.NumRecvDataInPeriod= 1;
411 }
412
413 if (ieee->data_hard_resume)
414 ieee->data_hard_resume(ieee->dev);
415
416 if (ieee->iw_mode == IW_MODE_ADHOC || ieee->iw_mode == IW_MODE_MASTER)
417 rtllib_start_send_beacons(ieee);
418
419 rtllib_wake_all_queues(ieee);
420
421 count = 0;
422out:
423 up(&ieee->wx_sem);
424
425}
426
427int rtllib_wx_set_scan(struct rtllib_device *ieee, struct iw_request_info *a,
428 union iwreq_data *wrqu, char *b)
429{
430 int ret = 0;
431
432 down(&ieee->wx_sem);
433
434 if (ieee->iw_mode == IW_MODE_MONITOR || !(ieee->proto_started)){
435 ret = -1;
436 goto out;
437 }
438
439 if ( ieee->state == RTLLIB_LINKED){
440 queue_work_rsl(ieee->wq, &ieee->wx_sync_scan_wq);
441 /* intentionally forget to up sem */
442 return 0;
443 }
444
445out:
446 up(&ieee->wx_sem);
447 return ret;
448}
449
450int rtllib_wx_set_essid(struct rtllib_device *ieee,
451 struct iw_request_info *a,
452 union iwreq_data *wrqu, char *extra)
453{
454
455 int ret=0,len,i;
456 short proto_started;
457 unsigned long flags;
458
459 rtllib_stop_scan_syncro(ieee);
460 down(&ieee->wx_sem);
461
462 proto_started = ieee->proto_started;
463
464#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
465 len = ((wrqu->essid.length-1) < IW_ESSID_MAX_SIZE) ? (wrqu->essid.length-1) : IW_ESSID_MAX_SIZE;
466#else
467 len = (wrqu->essid.length < IW_ESSID_MAX_SIZE) ? wrqu->essid.length : IW_ESSID_MAX_SIZE;
468#endif
469
470 if (len > IW_ESSID_MAX_SIZE){
471 ret= -E2BIG;
472 goto out;
473 }
474
475 if (ieee->iw_mode == IW_MODE_MONITOR){
476 ret= -1;
477 goto out;
478 }
479
480 for (i=0; i<len; i++){
481 if (extra[i] < 0){
482 ret= -1;
483 goto out;
484 }
485 }
486
487 if (proto_started)
488 rtllib_stop_protocol(ieee,true);
489
490
491 /* this is just to be sure that the GET wx callback
492 * has consisten infos. not needed otherwise
493 */
494 spin_lock_irqsave(&ieee->lock, flags);
495
496 if (wrqu->essid.flags && wrqu->essid.length) {
497 strncpy(ieee->current_network.ssid, extra, len);
498 ieee->current_network.ssid_len = len;
499 ieee->cannot_notify = false;
500 ieee->ssid_set = 1;
501 }
502 else{
503 ieee->ssid_set = 0;
504 ieee->current_network.ssid[0] = '\0';
505 ieee->current_network.ssid_len = 0;
506 }
507 spin_unlock_irqrestore(&ieee->lock, flags);
508
509 if (proto_started)
510 rtllib_start_protocol(ieee);
511out:
512 up(&ieee->wx_sem);
513 return ret;
514}
515
516 int rtllib_wx_get_mode(struct rtllib_device *ieee, struct iw_request_info *a,
517 union iwreq_data *wrqu, char *b)
518{
519 wrqu->mode = ieee->iw_mode;
520 return 0;
521}
522
523 int rtllib_wx_set_rawtx(struct rtllib_device *ieee,
524 struct iw_request_info *info,
525 union iwreq_data *wrqu, char *extra)
526{
527
528 int *parms = (int *)extra;
529 int enable = (parms[0] > 0);
530 short prev = ieee->raw_tx;
531
532 down(&ieee->wx_sem);
533
534 if (enable)
535 ieee->raw_tx = 1;
536 else
537 ieee->raw_tx = 0;
538
539 printk(KERN_INFO"raw TX is %s\n",
540 ieee->raw_tx ? "enabled" : "disabled");
541
542 if (ieee->iw_mode == IW_MODE_MONITOR)
543 {
544 if (prev == 0 && ieee->raw_tx){
545 if (ieee->data_hard_resume)
546 ieee->data_hard_resume(ieee->dev);
547
548 netif_carrier_on(ieee->dev);
549 }
550
551 if (prev && ieee->raw_tx == 1)
552 netif_carrier_off(ieee->dev);
553 }
554
555 up(&ieee->wx_sem);
556
557 return 0;
558}
559
560int rtllib_wx_get_name(struct rtllib_device *ieee,
561 struct iw_request_info *info,
562 union iwreq_data *wrqu, char *extra)
563{
564 strcpy(wrqu->name, "802.11");
565
566 if (ieee->modulation & RTLLIB_CCK_MODULATION)
567 strcat(wrqu->name, "b");
568 if (ieee->modulation & RTLLIB_OFDM_MODULATION)
569 strcat(wrqu->name, "g");
570 if (ieee->mode & (IEEE_N_24G | IEEE_N_5G))
571 strcat(wrqu->name, "n");
572 return 0;
573}
574
575
576/* this is mostly stolen from hostap */
577int rtllib_wx_set_power(struct rtllib_device *ieee,
578 struct iw_request_info *info,
579 union iwreq_data *wrqu, char *extra)
580{
581 int ret = 0;
582#if 1
583 if (
584 (!ieee->sta_wake_up) ||
585 (!ieee->enter_sleep_state) ||
586 (!ieee->ps_is_queue_empty)){
587
588 RTLLIB_DEBUG(RTLLIB_DL_ERR,"%s(): PS mode is tryied to be use but driver missed a callback\n\n",__func__);
589
590 return -1;
591 }
592#endif
593 down(&ieee->wx_sem);
594
595 if (wrqu->power.disabled){
596 RT_TRACE(COMP_DBG, "===>%s(): power disable\n",__func__);
597 ieee->ps = RTLLIB_PS_DISABLED;
598 goto exit;
599 }
600 if (wrqu->power.flags & IW_POWER_TIMEOUT) {
601 ieee->ps_timeout = wrqu->power.value / 1000;
602 RT_TRACE(COMP_DBG, "===>%s():ps_timeout is %d\n",__func__,ieee->ps_timeout);
603 }
604
605 if (wrqu->power.flags & IW_POWER_PERIOD) {
606
607 ieee->ps_period = wrqu->power.value / 1000;
608
609 }
610 switch (wrqu->power.flags & IW_POWER_MODE) {
611 case IW_POWER_UNICAST_R:
612 ieee->ps = RTLLIB_PS_UNICAST;
613 break;
614 case IW_POWER_MULTICAST_R:
615 ieee->ps = RTLLIB_PS_MBCAST;
616 break;
617 case IW_POWER_ALL_R:
618 ieee->ps = RTLLIB_PS_UNICAST | RTLLIB_PS_MBCAST;
619 break;
620
621 case IW_POWER_ON:
622 break;
623
624 default:
625 ret = -EINVAL;
626 goto exit;
627
628 }
629exit:
630 up(&ieee->wx_sem);
631 return ret;
632
633}
634
635/* this is stolen from hostap */
636int rtllib_wx_get_power(struct rtllib_device *ieee,
637 struct iw_request_info *info,
638 union iwreq_data *wrqu, char *extra)
639{
640 int ret =0;
641
642 down(&ieee->wx_sem);
643
644 if (ieee->ps == RTLLIB_PS_DISABLED) {
645 wrqu->power.disabled = 1;
646 goto exit;
647 }
648
649 wrqu->power.disabled = 0;
650
651 if ((wrqu->power.flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
652 wrqu->power.flags = IW_POWER_TIMEOUT;
653 wrqu->power.value = ieee->ps_timeout * 1000;
654 } else {
655 wrqu->power.flags = IW_POWER_PERIOD;
656 wrqu->power.value = ieee->ps_period * 1000;
657 }
658
659 if ((ieee->ps & (RTLLIB_PS_MBCAST | RTLLIB_PS_UNICAST)) == (RTLLIB_PS_MBCAST | RTLLIB_PS_UNICAST))
660 wrqu->power.flags |= IW_POWER_ALL_R;
661 else if (ieee->ps & RTLLIB_PS_MBCAST)
662 wrqu->power.flags |= IW_POWER_MULTICAST_R;
663 else
664 wrqu->power.flags |= IW_POWER_UNICAST_R;
665
666exit:
667 up(&ieee->wx_sem);
668 return ret;
669
670}