4 * Copyright (C) 2005 Mike Baker,
5 * Felix Fietkau <nbd@vd-s.ath.cx>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/if_arp.h>
29 #include <asm/uaccess.h>
30 #include <linux/wireless.h>
32 #include <net/iw_handler.h>
35 static struct net_device
*dev
;
36 char buf
[WLC_IOCTL_MAXLEN
];
38 /* The frequency of each channel in MHz */
39 const long channel_frequency
[] = {
40 2412, 2417, 2422, 2427, 2432, 2437, 2442,
41 2447, 2452, 2457, 2462, 2467, 2472, 2484
43 #define NUM_CHANNELS ( sizeof(channel_frequency) / sizeof(channel_frequency[0]) )
45 static int wlcompat_private_ioctl(struct net_device
*dev
,
46 struct iw_request_info
*info
,
47 union iwreq_data
*wrqu
,
50 static int wl_ioctl(struct net_device
*dev
, int cmd
, void *buf
, int len
)
52 mm_segment_t old_fs
= get_fs();
59 strncpy(ifr
.ifr_name
, dev
->name
, IFNAMSIZ
);
60 ifr
.ifr_data
= (caddr_t
) &ioc
;
62 ret
= dev
->do_ioctl(dev
,&ifr
,SIOCDEVPRIVATE
);
67 static int wl_set_val(struct net_device
*dev
, char *var
, void *val
, int len
)
72 /* check for overflow */
73 if ((buf_len
= strlen(var
)) + 1 + len
> sizeof(buf
))
79 /* append int value onto the end of the name string */
80 memcpy(&buf
[buf_len
], val
, len
);
83 return wl_ioctl(dev
, WLC_SET_VAR
, buf
, buf_len
);
86 static int wl_get_val(struct net_device
*dev
, char *var
, void *val
, int len
)
91 /* check for overflow */
92 if (strlen(var
) + 1 > sizeof(buf
) || len
> sizeof(buf
))
96 if ((ret
= wl_ioctl(dev
, WLC_GET_VAR
, buf
, sizeof(buf
))))
99 memcpy(val
, buf
, len
);
104 static int wlcompat_ioctl_getiwrange(struct net_device
*dev
,
108 struct iw_range
*range
;
110 range
= (struct iw_range
*) extra
;
112 range
->we_version_compiled
= WIRELESS_EXT
;
113 range
->we_version_source
= WIRELESS_EXT
;
115 range
->min_nwid
= range
->max_nwid
= 0;
117 range
->num_channels
= NUM_CHANNELS
;
119 for (i
= 0; i
< NUM_CHANNELS
; i
++) {
120 range
->freq
[k
].i
= i
+ 1;
121 range
->freq
[k
].m
= channel_frequency
[i
] * 100000;
122 range
->freq
[k
].e
= 1;
124 if (k
>= IW_MAX_FREQUENCIES
)
127 range
->num_frequency
= k
;
128 range
->sensitivity
= 3;
130 /* nbd: don't know what this means, but other drivers set it this way */
131 range
->pmp_flags
= IW_POWER_PERIOD
;
132 range
->pmt_flags
= IW_POWER_TIMEOUT
;
133 range
->pm_capa
= IW_POWER_PERIOD
| IW_POWER_TIMEOUT
| IW_POWER_UNICAST_R
;
136 range
->max_pmp
= 65535000;
138 range
->max_pmt
= 65535 * 1000;
141 if (wl_ioctl(dev
, WLC_GET_RTS
, &range
->max_rts
, sizeof(int)) < 0)
142 range
->max_rts
= 2347;
144 range
->min_frag
= 256;
146 if (wl_ioctl(dev
, WLC_GET_FRAG
, &range
->max_frag
, sizeof(int)) < 0)
147 range
->max_frag
= 2346;
149 range
->txpower_capa
= IW_TXPOW_MWATT
;
155 static int wlcompat_set_scan(struct net_device
*dev
,
156 struct iw_request_info
*info
,
157 union iwreq_data
*wrqu
,
160 int ap
= 0, oldap
= 0;
161 wl_scan_params_t params
;
163 memset(¶ms
, 0, sizeof(params
));
165 /* use defaults (same parameters as wl scan) */
166 memset(¶ms
.bssid
, 0xff, sizeof(params
.bssid
));
167 params
.bss_type
= DOT11_BSSTYPE_ANY
;
168 params
.scan_type
= -1;
170 params
.active_time
= -1;
171 params
.passive_time
= -1;
172 params
.home_time
= -1;
174 /* can only scan in STA mode */
175 wl_ioctl(dev
, WLC_GET_AP
, &oldap
, sizeof(oldap
));
177 wl_ioctl(dev
, WLC_SET_AP
, &ap
, sizeof(ap
));
179 if (wl_ioctl(dev
, WLC_SCAN
, ¶ms
, 64) < 0)
183 wl_ioctl(dev
, WLC_SET_AP
, &oldap
, sizeof(oldap
));
189 static int wlcompat_get_scan(struct net_device
*dev
,
190 struct iw_request_info
*info
,
191 union iwreq_data
*wrqu
,
194 wl_scan_results_t
*results
= (wl_scan_results_t
*) buf
;
195 wl_bss_info_t
*bss_info
;
197 char *current_ev
= extra
;
199 char *end_buf
= extra
+ IW_SCAN_MAX_DATA
;
203 if (wl_ioctl(dev
, WLC_SCAN_RESULTS
, buf
, WLC_IOCTL_MAXLEN
) < 0)
206 bss_info
= &(results
->bss_info
[0]);
207 info_ptr
= (char *) bss_info
;
208 for (i
= 0; i
< results
->count
; i
++) {
210 /* send the cell address (must be sent first) */
212 iwe
.u
.ap_addr
.sa_family
= ARPHRD_ETHER
;
213 memcpy(&iwe
.u
.ap_addr
.sa_data
, &bss_info
->BSSID
, sizeof(bss_info
->BSSID
));
214 current_ev
= iwe_stream_add_event(current_ev
, end_buf
, &iwe
, IW_EV_ADDR_LEN
);
217 iwe
.cmd
= SIOCGIWESSID
;
218 iwe
.u
.data
.length
= bss_info
->SSID_len
;
219 if (iwe
.u
.data
.length
> IW_ESSID_MAX_SIZE
)
220 iwe
.u
.data
.length
= IW_ESSID_MAX_SIZE
;
221 iwe
.u
.data
.flags
= 1;
222 current_ev
= iwe_stream_add_point(current_ev
, end_buf
, &iwe
, bss_info
->SSID
);
224 /* send frequency/channel info */
225 iwe
.cmd
= SIOCGIWFREQ
;
227 iwe
.u
.freq
.m
= bss_info
->channel
;
228 current_ev
= iwe_stream_add_event(current_ev
, end_buf
, &iwe
, IW_EV_FREQ_LEN
);
230 /* add quality statistics */
232 iwe
.u
.qual
.level
= bss_info
->RSSI
;
233 iwe
.u
.qual
.noise
= bss_info
->phy_noise
;
235 current_ev
= iwe_stream_add_event(current_ev
, end_buf
, &iwe
, IW_EV_QUAL_LEN
);
237 /* send rate information */
238 iwe
.cmd
= SIOCGIWRATE
;
239 current_val
= current_ev
+ IW_EV_LCP_LEN
;
240 iwe
.u
.bitrate
.fixed
= iwe
.u
.bitrate
.disabled
= 0;
242 for(j
= 0 ; j
< bss_info
->rateset
.count
; j
++) {
243 iwe
.u
.bitrate
.value
= ((bss_info
->rateset
.rates
[j
] & 0x7f) * 500000);
244 current_val
= iwe_stream_add_value(current_ev
, current_val
, end_buf
, &iwe
, IW_EV_PARAM_LEN
);
246 if((current_val
- current_ev
) > IW_EV_LCP_LEN
)
247 current_ev
= current_val
;
249 info_ptr
+= sizeof(wl_bss_info_t
);
250 if (bss_info
->ie_length
% 4)
251 info_ptr
+= bss_info
->ie_length
+ 4 - (bss_info
->ie_length
% 4);
253 info_ptr
+= bss_info
->ie_length
;
254 bss_info
= (wl_bss_info_t
*) info_ptr
;
257 wrqu
->data
.length
= (current_ev
- extra
);
258 wrqu
->data
.flags
= 0;
263 static int wlcompat_ioctl(struct net_device
*dev
,
264 struct iw_request_info
*info
,
265 union iwreq_data
*wrqu
,
270 strcpy(wrqu
->name
, "IEEE 802.11-DS");
276 if (wl_ioctl(dev
,WLC_GET_CHANNEL
, &ci
, sizeof(ci
)) < 0)
279 wrqu
->freq
.m
= ci
.target_channel
;
285 if (wrqu
->freq
.e
== 1) {
287 int f
= wrqu
->freq
.m
/ 100000;
288 while ((channel
< NUM_CHANNELS
+ 1) && (f
!= channel_frequency
[channel
]))
291 if (channel
== NUM_CHANNELS
) // channel not found
295 wrqu
->freq
.m
= channel
+ 1;
297 if ((wrqu
->freq
.e
== 0) && (wrqu
->freq
.m
< 1000)) {
298 if (wl_ioctl(dev
, WLC_SET_CHANNEL
, &wrqu
->freq
.m
, sizeof(int)) < 0)
307 wrqu
->ap_addr
.sa_family
= ARPHRD_ETHER
;
308 if (wl_ioctl(dev
,WLC_GET_BSSID
,wrqu
->ap_addr
.sa_data
,6) < 0)
316 if (wl_ioctl(dev
,WLC_GET_SSID
, &ssid
, sizeof(wlc_ssid_t
)) < 0)
319 wrqu
->essid
.flags
= wrqu
->data
.flags
= 1;
320 wrqu
->essid
.length
= wrqu
->data
.length
= ssid
.SSID_len
+ 1;
321 memcpy(extra
,ssid
.SSID
,ssid
.SSID_len
+ 1);
327 memset(&ssid
, 0, sizeof(ssid
));
328 ssid
.SSID_len
= strlen(extra
);
329 if (ssid
.SSID_len
> WLC_ESSID_MAX_SIZE
)
330 ssid
.SSID_len
= WLC_ESSID_MAX_SIZE
;
331 memcpy(ssid
.SSID
, extra
, ssid
.SSID_len
);
332 if (wl_ioctl(dev
, WLC_SET_SSID
, &ssid
, sizeof(ssid
)) < 0)
338 if (wl_ioctl(dev
,WLC_GET_RTS
,&(wrqu
->rts
.value
),sizeof(int)) < 0)
344 if (wl_ioctl(dev
,WLC_SET_RTS
,&(wrqu
->rts
.value
),sizeof(int)) < 0)
350 if (wl_ioctl(dev
,WLC_GET_FRAG
,&(wrqu
->frag
.value
),sizeof(int)) < 0)
356 if (wl_ioctl(dev
,WLC_SET_FRAG
,&(wrqu
->frag
.value
),sizeof(int)) < 0)
362 if (wl_get_val(dev
, "qtxpower", &(wrqu
->txpower
.value
), sizeof(int)) < 0)
365 wrqu
->txpower
.value
&= ~WL_TXPWR_OVERRIDE
;
367 wrqu
->txpower
.fixed
= 0;
368 wrqu
->txpower
.disabled
= 0;
369 wrqu
->txpower
.flags
= IW_TXPOW_MWATT
;
376 if (wl_get_val(dev
, "qtxpower", &override
, sizeof(int)) < 0)
379 wrqu
->txpower
.value
|= override
& WL_TXPWR_OVERRIDE
;
381 if (wrqu
->txpower
.flags
!= IW_TXPOW_MWATT
)
384 if (wl_set_val(dev
, "qtxpower", &wrqu
->txpower
.value
, sizeof(int)) < 0)
390 if (wl_ioctl(dev
, WLC_GET_WEP
, &val
, sizeof(val
)) < 0)
394 wrqu
->data
.flags
= IW_ENCODE_ENABLED
| IW_ENCODE_NOKEY
;
396 wrqu
->data
.flags
= IW_ENCODE_DISABLED
;
405 return wlcompat_ioctl_getiwrange(dev
, extra
);
410 int ap
= -1, infra
= -1, passive
= 0, wet
= 0;
412 switch (wrqu
->mode
) {
413 case IW_MODE_MONITOR
:
437 if (wl_ioctl(dev
, WLC_SET_PASSIVE
, &passive
, sizeof(passive
)) < 0)
439 if (wl_ioctl(dev
, WLC_SET_MONITOR
, &passive
, sizeof(passive
)) < 0)
441 if (wl_ioctl(dev
, WLC_SET_WET
, &wet
, sizeof(wet
)) < 0)
444 if (wl_ioctl(dev
, WLC_SET_AP
, &ap
, sizeof(ap
)) < 0)
447 if (wl_ioctl(dev
, WLC_SET_INFRA
, &infra
, sizeof(infra
)) < 0)
455 int ap
, infra
, wet
, passive
;
457 if (wl_ioctl(dev
, WLC_GET_AP
, &ap
, sizeof(ap
)) < 0)
459 if (wl_ioctl(dev
, WLC_GET_INFRA
, &infra
, sizeof(infra
)) < 0)
461 if (wl_ioctl(dev
, WLC_GET_PASSIVE
, &passive
, sizeof(passive
)) < 0)
463 if (wl_ioctl(dev
, WLC_GET_WET
, &wet
, sizeof(wet
)) < 0)
467 wrqu
->mode
= IW_MODE_MONITOR
;
469 wrqu
->mode
= IW_MODE_ADHOC
;
472 wrqu
->mode
= IW_MODE_MASTER
;
475 wrqu
->mode
= IW_MODE_REPEAT
;
477 wrqu
->mode
= IW_MODE_INFRA
;
485 if (info
->cmd
>= SIOCIWFIRSTPRIV
)
486 return wlcompat_private_ioctl(dev
, info
, wrqu
, extra
);
495 static const iw_handler wlcompat_handler
[] = {
496 NULL
, /* SIOCSIWCOMMIT */
497 wlcompat_ioctl
, /* SIOCGIWNAME */
498 NULL
, /* SIOCSIWNWID */
499 NULL
, /* SIOCGIWNWID */
500 wlcompat_ioctl
, /* SIOCSIWFREQ */
501 wlcompat_ioctl
, /* SIOCGIWFREQ */
502 wlcompat_ioctl
, /* SIOCSIWMODE */
503 wlcompat_ioctl
, /* SIOCGIWMODE */
504 NULL
, /* SIOCSIWSENS */
505 NULL
, /* SIOCGIWSENS */
506 NULL
, /* SIOCSIWRANGE, unused */
507 wlcompat_ioctl
, /* SIOCGIWRANGE */
508 NULL
, /* SIOCSIWPRIV */
509 NULL
, /* SIOCGIWPRIV */
510 NULL
, /* SIOCSIWSTATS */
511 NULL
, /* SIOCGIWSTATS */
512 iw_handler_set_spy
, /* SIOCSIWSPY */
513 iw_handler_get_spy
, /* SIOCGIWSPY */
514 iw_handler_set_thrspy
, /* SIOCSIWTHRSPY */
515 iw_handler_get_thrspy
, /* SIOCGIWTHRSPY */
516 NULL
, /* SIOCSIWAP */
517 wlcompat_ioctl
, /* SIOCGIWAP */
518 NULL
, /* -- hole -- */
519 NULL
, /* SIOCGIWAPLIST */
520 wlcompat_set_scan
, /* SIOCSIWSCAN */
521 wlcompat_get_scan
, /* SIOCGIWSCAN */
522 wlcompat_ioctl
, /* SIOCSIWESSID */
523 wlcompat_ioctl
, /* SIOCGIWESSID */
524 NULL
, /* SIOCSIWNICKN */
525 NULL
, /* SIOCGIWNICKN */
526 NULL
, /* -- hole -- */
527 NULL
, /* -- hole -- */
528 NULL
, /* SIOCSIWRATE */
529 NULL
, /* SIOCGIWRATE */
530 wlcompat_ioctl
, /* SIOCSIWRTS */
531 wlcompat_ioctl
, /* SIOCGIWRTS */
532 wlcompat_ioctl
, /* SIOCSIWFRAG */
533 wlcompat_ioctl
, /* SIOCGIWFRAG */
534 wlcompat_ioctl
, /* SIOCSIWTXPOW */
535 wlcompat_ioctl
, /* SIOCGIWTXPOW */
536 NULL
, /* SIOCSIWRETRY */
537 NULL
, /* SIOCGIWRETRY */
538 NULL
, /* SIOCSIWENCODE */
539 wlcompat_ioctl
, /* SIOCGIWENCODE */
542 #define PRIV_SET_MONITOR SIOCIWFIRSTPRIV + 0
543 #define PRIV_GET_MONITOR SIOCIWFIRSTPRIV + 1
544 #define PRIV_SET_TXPWR_LIMIT SIOCIWFIRSTPRIV + 2
545 #define PRIV_GET_TXPWR_LIMIT SIOCIWFIRSTPRIV + 3
547 static const struct iw_priv_args wlcompat_private_args
[] =
550 IW_PRIV_TYPE_INT
| IW_PRIV_SIZE_FIXED
| 1,
556 IW_PRIV_TYPE_INT
| IW_PRIV_SIZE_FIXED
| 1,
559 { PRIV_SET_TXPWR_LIMIT
,
560 IW_PRIV_TYPE_INT
| IW_PRIV_SIZE_FIXED
| 1,
564 { PRIV_GET_TXPWR_LIMIT
,
566 IW_PRIV_TYPE_INT
| IW_PRIV_SIZE_FIXED
| 1,
571 static int wlcompat_private_ioctl(struct net_device
*dev
,
572 struct iw_request_info
*info
,
573 union iwreq_data
*wrqu
,
576 int *value
= (int *) wrqu
->name
;
579 case PRIV_SET_MONITOR
:
581 if (wl_ioctl(dev
, WLC_SET_MONITOR
, value
, sizeof(int)) < 0)
586 case PRIV_GET_MONITOR
:
588 if (wl_ioctl(dev
, WLC_GET_MONITOR
, extra
, sizeof(int)) < 0)
593 case PRIV_SET_TXPWR_LIMIT
:
598 if (wl_get_val(dev
, "qtxpower", &val
, sizeof(int)) < 0)
602 val
|= WL_TXPWR_OVERRIDE
;
604 val
&= ~WL_TXPWR_OVERRIDE
;
606 if (wl_set_val(dev
, "qtxpower", &val
, sizeof(int)) < 0)
611 case PRIV_GET_TXPWR_LIMIT
:
613 if (wl_get_val(dev
, "qtxpower", value
, sizeof(int)) < 0)
616 *value
= ((*value
& WL_TXPWR_OVERRIDE
) == WL_TXPWR_OVERRIDE
? 1 : 0);
630 static const iw_handler wlcompat_private
[] =
632 wlcompat_private_ioctl
,
633 wlcompat_private_ioctl
,
634 wlcompat_private_ioctl
,
635 wlcompat_private_ioctl
639 static const struct iw_handler_def wlcompat_handler_def
=
641 .standard
= (iw_handler
*) wlcompat_handler
,
642 .num_standard
= sizeof(wlcompat_handler
)/sizeof(iw_handler
),
643 .private = wlcompat_private
,
644 .num_private
= sizeof(wlcompat_private
)/sizeof(iw_handler
),
645 .private_args
= wlcompat_private_args
,
646 .num_private_args
= sizeof(wlcompat_private_args
)/sizeof(struct iw_priv_args
),
651 static int (*old_ioctl
)(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
);
652 void print_buffer(int len
, unsigned char *buf
) {
655 for (x
=0;x
<len
&& x
<180 ;x
++) {
658 printk("%02X",buf
[x
]);
666 static int new_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
) {
668 printk("dev: %s ioctl: 0x%04x\n",dev
->name
,cmd
);
669 if (cmd
==SIOCDEVPRIVATE
) {
670 wl_ioctl_t
*ioc
= (wl_ioctl_t
*)ifr
->ifr_data
;
671 unsigned char *buf
= ioc
->buf
;
672 printk(" cmd: %d buf: 0x%08x len: %d\n",ioc
->cmd
,&(ioc
->buf
),ioc
->len
);
674 print_buffer(ioc
->len
, buf
);
675 ret
= old_ioctl(dev
,ifr
,cmd
);
677 print_buffer(ioc
->len
, buf
);
678 printk(" ret: %d\n", ret
);
680 ret
= old_ioctl(dev
,ifr
,cmd
);
686 static int __init
wlcompat_init()
688 dev
= dev_get_by_name("eth1");
690 old_ioctl
= dev
->do_ioctl
;
691 dev
->do_ioctl
= new_ioctl
;
693 dev
->wireless_handlers
= (struct iw_handler_def
*)&wlcompat_handler_def
;
697 static void __exit
wlcompat_exit()
699 dev
->wireless_handlers
= NULL
;
701 dev
->do_ioctl
= old_ioctl
;
707 MODULE_AUTHOR("openwrt.org");
708 MODULE_LICENSE("GPL");
710 module_init(wlcompat_init
);
711 module_exit(wlcompat_exit
);