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 wl_ioctl(struct net_device
*dev
, int cmd
, void *buf
, int len
)
47 mm_segment_t old_fs
= get_fs();
54 strncpy(ifr
.ifr_name
, dev
->name
, IFNAMSIZ
);
55 ifr
.ifr_data
= (caddr_t
) &ioc
;
57 ret
= dev
->do_ioctl(dev
,&ifr
,SIOCDEVPRIVATE
);
62 static int wlcompat_ioctl_getiwrange(struct net_device
*dev
,
66 struct iw_range
*range
;
68 range
= (struct iw_range
*) extra
;
70 range
->we_version_compiled
= WIRELESS_EXT
;
71 range
->we_version_source
= WIRELESS_EXT
;
73 range
->min_nwid
= range
->max_nwid
= 0;
75 range
->num_channels
= NUM_CHANNELS
;
77 for (i
= 0; i
< NUM_CHANNELS
; i
++) {
78 range
->freq
[k
].i
= i
+ 1;
79 range
->freq
[k
].m
= channel_frequency
[i
] * 100000;
82 if (k
>= IW_MAX_FREQUENCIES
)
85 range
->num_frequency
= k
;
86 range
->sensitivity
= 3;
88 /* nbd: don't know what this means, but other drivers set it this way */
89 range
->pmp_flags
= IW_POWER_PERIOD
;
90 range
->pmt_flags
= IW_POWER_TIMEOUT
;
91 range
->pm_capa
= IW_POWER_PERIOD
| IW_POWER_TIMEOUT
| IW_POWER_UNICAST_R
;
94 range
->max_pmp
= 65535000;
96 range
->max_pmt
= 65535 * 1000;
99 if (wl_ioctl(dev
, WLC_GET_RTS
, &range
->max_rts
, sizeof(int)) < 0)
100 range
->max_rts
= 2347;
102 range
->min_frag
= 256;
104 if (wl_ioctl(dev
, WLC_GET_FRAG
, &range
->max_frag
, sizeof(int)) < 0)
105 range
->max_frag
= 2346;
107 range
->txpower_capa
= IW_TXPOW_MWATT
;
113 static int wlcompat_set_scan(struct net_device
*dev
,
114 struct iw_request_info
*info
,
115 union iwreq_data
*wrqu
,
118 int ap
= 0, oldap
= 0;
119 wl_scan_params_t params
;
121 memset(¶ms
, 0, sizeof(params
));
123 /* use defaults (same parameters as wl scan) */
124 memset(¶ms
.bssid
, 0xff, sizeof(params
.bssid
));
125 params
.bss_type
= DOT11_BSSTYPE_ANY
;
126 params
.scan_type
= -1;
128 params
.active_time
= -1;
129 params
.passive_time
= -1;
130 params
.home_time
= -1;
132 /* can only scan in STA mode */
133 wl_ioctl(dev
, WLC_GET_AP
, &oldap
, sizeof(oldap
));
135 wl_ioctl(dev
, WLC_SET_AP
, &ap
, sizeof(ap
));
137 if (wl_ioctl(dev
, WLC_SCAN
, ¶ms
, 64) < 0)
141 wl_ioctl(dev
, WLC_SET_AP
, &oldap
, sizeof(oldap
));
147 static int wlcompat_get_scan(struct net_device
*dev
,
148 struct iw_request_info
*info
,
149 union iwreq_data
*wrqu
,
152 wl_scan_results_t
*results
= (wl_scan_results_t
*) buf
;
153 wl_bss_info_t
*bss_info
;
155 char *current_ev
= extra
;
157 char *end_buf
= extra
+ IW_SCAN_MAX_DATA
;
161 if (wl_ioctl(dev
, WLC_SCAN_RESULTS
, buf
, WLC_IOCTL_MAXLEN
) < 0)
164 bss_info
= &(results
->bss_info
[0]);
165 info_ptr
= (char *) bss_info
;
166 for (i
= 0; i
< results
->count
; i
++) {
168 /* send the cell address (must be sent first) */
170 iwe
.u
.ap_addr
.sa_family
= ARPHRD_ETHER
;
171 memcpy(&iwe
.u
.ap_addr
.sa_data
, &bss_info
->BSSID
, sizeof(bss_info
->BSSID
));
172 current_ev
= iwe_stream_add_event(current_ev
, end_buf
, &iwe
, IW_EV_ADDR_LEN
);
175 iwe
.cmd
= SIOCGIWESSID
;
176 iwe
.u
.data
.length
= bss_info
->SSID_len
;
177 if (iwe
.u
.data
.length
> IW_ESSID_MAX_SIZE
)
178 iwe
.u
.data
.length
= IW_ESSID_MAX_SIZE
;
179 iwe
.u
.data
.flags
= 1;
180 current_ev
= iwe_stream_add_point(current_ev
, end_buf
, &iwe
, bss_info
->SSID
);
182 /* send frequency/channel info */
183 iwe
.cmd
= SIOCGIWFREQ
;
185 iwe
.u
.freq
.m
= bss_info
->channel
;
186 current_ev
= iwe_stream_add_event(current_ev
, end_buf
, &iwe
, IW_EV_FREQ_LEN
);
188 /* add quality statistics */
190 iwe
.u
.qual
.level
= bss_info
->RSSI
;
191 iwe
.u
.qual
.noise
= bss_info
->phy_noise
;
193 current_ev
= iwe_stream_add_event(current_ev
, end_buf
, &iwe
, IW_EV_QUAL_LEN
);
195 /* send rate information */
196 iwe
.cmd
= SIOCGIWRATE
;
197 current_val
= current_ev
+ IW_EV_LCP_LEN
;
198 iwe
.u
.bitrate
.fixed
= iwe
.u
.bitrate
.disabled
= 0;
200 for(j
= 0 ; j
< bss_info
->rateset
.count
; j
++) {
201 iwe
.u
.bitrate
.value
= ((bss_info
->rateset
.rates
[j
] & 0x7f) * 500000);
202 current_val
= iwe_stream_add_value(current_ev
, current_val
, end_buf
, &iwe
, IW_EV_PARAM_LEN
);
204 if((current_val
- current_ev
) > IW_EV_LCP_LEN
)
205 current_ev
= current_val
;
207 info_ptr
+= sizeof(wl_bss_info_t
);
208 if (bss_info
->ie_length
% 4)
209 info_ptr
+= bss_info
->ie_length
+ 4 - (bss_info
->ie_length
% 4);
211 info_ptr
+= bss_info
->ie_length
;
212 bss_info
= (wl_bss_info_t
*) info_ptr
;
215 wrqu
->data
.length
= (current_ev
- extra
);
216 wrqu
->data
.flags
= 0;
221 static int wlcompat_ioctl(struct net_device
*dev
,
222 struct iw_request_info
*info
,
223 union iwreq_data
*wrqu
,
228 strcpy(wrqu
->name
, "IEEE 802.11-DS");
234 if (wl_ioctl(dev
,WLC_GET_CHANNEL
, &ci
, sizeof(ci
)) < 0)
237 wrqu
->freq
.m
= ci
.target_channel
;
243 if (wrqu
->freq
.e
== 1) {
245 int f
= wrqu
->freq
.m
/ 100000;
246 while ((channel
< NUM_CHANNELS
+ 1) && (f
!= channel_frequency
[channel
]))
249 if (channel
== NUM_CHANNELS
) // channel not found
253 wrqu
->freq
.m
= channel
+ 1;
255 if ((wrqu
->freq
.e
== 0) && (wrqu
->freq
.m
< 1000)) {
256 if (wl_ioctl(dev
, WLC_SET_CHANNEL
, &wrqu
->freq
.m
, sizeof(int)) < 0)
265 wrqu
->ap_addr
.sa_family
= ARPHRD_ETHER
;
266 if (wl_ioctl(dev
,WLC_GET_BSSID
,wrqu
->ap_addr
.sa_data
,6) < 0)
274 if (wl_ioctl(dev
,WLC_GET_SSID
, &ssid
, sizeof(wlc_ssid_t
)) < 0)
277 wrqu
->essid
.flags
= wrqu
->data
.flags
= 1;
278 wrqu
->essid
.length
= wrqu
->data
.length
= ssid
.SSID_len
+ 1;
279 memcpy(extra
,ssid
.SSID
,ssid
.SSID_len
+ 1);
285 memset(&ssid
, 0, sizeof(ssid
));
286 ssid
.SSID_len
= strlen(extra
);
287 if (ssid
.SSID_len
> WLC_ESSID_MAX_SIZE
)
288 ssid
.SSID_len
= WLC_ESSID_MAX_SIZE
;
289 memcpy(ssid
.SSID
, extra
, ssid
.SSID_len
);
290 if (wl_ioctl(dev
, WLC_SET_SSID
, &ssid
, sizeof(ssid
)) < 0)
296 if (wl_ioctl(dev
,WLC_GET_RTS
,&(wrqu
->rts
.value
),sizeof(int)) < 0)
302 if (wl_ioctl(dev
,WLC_SET_RTS
,&(wrqu
->rts
.value
),sizeof(int)) < 0)
308 if (wl_ioctl(dev
,WLC_GET_FRAG
,&(wrqu
->frag
.value
),sizeof(int)) < 0)
314 if (wl_ioctl(dev
,WLC_SET_FRAG
,&(wrqu
->frag
.value
),sizeof(int)) < 0)
320 wrqu
->txpower
.value
= 0;
321 if (wl_ioctl(dev
,WLC_GET_TXPWR
, &(wrqu
->txpower
.value
), sizeof(int)) < 0)
323 wrqu
->txpower
.fixed
= 0;
324 wrqu
->txpower
.disabled
= 0;
325 wrqu
->txpower
.flags
= IW_TXPOW_MWATT
;
330 if (wrqu
->txpower
.flags
!= IW_TXPOW_MWATT
)
333 if (wl_ioctl(dev
, WLC_SET_TXPWR
, &wrqu
->txpower
.value
, sizeof(int)) < 0)
339 if (wl_ioctl(dev
, WLC_GET_WEP
, &val
, sizeof(val
)) < 0)
343 wrqu
->data
.flags
= IW_ENCODE_ENABLED
| IW_ENCODE_NOKEY
;
345 wrqu
->data
.flags
= IW_ENCODE_DISABLED
;
354 return wlcompat_ioctl_getiwrange(dev
, extra
);
359 int ap
= -1, infra
= -1, passive
= 0, wet
= 0;
361 switch (wrqu
->mode
) {
362 case IW_MODE_MONITOR
:
386 if (wl_ioctl(dev
, WLC_SET_PASSIVE
, &passive
, sizeof(passive
)) < 0)
388 if (wl_ioctl(dev
, WLC_SET_MONITOR
, &passive
, sizeof(passive
)) < 0)
390 if (wl_ioctl(dev
, WLC_SET_WET
, &wet
, sizeof(wet
)) < 0)
393 if (wl_ioctl(dev
, WLC_SET_AP
, &ap
, sizeof(ap
)) < 0)
396 if (wl_ioctl(dev
, WLC_SET_INFRA
, &infra
, sizeof(infra
)) < 0)
404 int ap
, infra
, wet
, passive
;
406 if (wl_ioctl(dev
, WLC_GET_AP
, &ap
, sizeof(ap
)) < 0)
408 if (wl_ioctl(dev
, WLC_GET_INFRA
, &infra
, sizeof(infra
)) < 0)
410 if (wl_ioctl(dev
, WLC_GET_PASSIVE
, &passive
, sizeof(passive
)) < 0)
412 if (wl_ioctl(dev
, WLC_GET_WET
, &wet
, sizeof(wet
)) < 0)
416 wrqu
->mode
= IW_MODE_MONITOR
;
418 wrqu
->mode
= IW_MODE_ADHOC
;
421 wrqu
->mode
= IW_MODE_MASTER
;
424 wrqu
->mode
= IW_MODE_REPEAT
;
426 wrqu
->mode
= IW_MODE_INFRA
;
441 static const iw_handler wlcompat_handler
[] = {
442 NULL
, /* SIOCSIWCOMMIT */
443 wlcompat_ioctl
, /* SIOCGIWNAME */
444 NULL
, /* SIOCSIWNWID */
445 NULL
, /* SIOCGIWNWID */
446 wlcompat_ioctl
, /* SIOCSIWFREQ */
447 wlcompat_ioctl
, /* SIOCGIWFREQ */
448 wlcompat_ioctl
, /* SIOCSIWMODE */
449 wlcompat_ioctl
, /* SIOCGIWMODE */
450 NULL
, /* SIOCSIWSENS */
451 NULL
, /* SIOCGIWSENS */
452 NULL
, /* SIOCSIWRANGE, unused */
453 wlcompat_ioctl
, /* SIOCGIWRANGE */
454 NULL
, /* SIOCSIWPRIV */
455 NULL
, /* SIOCGIWPRIV */
456 NULL
, /* SIOCSIWSTATS */
457 NULL
, /* SIOCGIWSTATS */
458 iw_handler_set_spy
, /* SIOCSIWSPY */
459 iw_handler_get_spy
, /* SIOCGIWSPY */
460 iw_handler_set_thrspy
, /* SIOCSIWTHRSPY */
461 iw_handler_get_thrspy
, /* SIOCGIWTHRSPY */
462 NULL
, /* SIOCSIWAP */
463 wlcompat_ioctl
, /* SIOCGIWAP */
464 NULL
, /* -- hole -- */
465 NULL
, /* SIOCGIWAPLIST */
466 wlcompat_set_scan
, /* SIOCSIWSCAN */
467 wlcompat_get_scan
, /* SIOCGIWSCAN */
468 wlcompat_ioctl
, /* SIOCSIWESSID */
469 wlcompat_ioctl
, /* SIOCGIWESSID */
470 NULL
, /* SIOCSIWNICKN */
471 NULL
, /* SIOCGIWNICKN */
472 NULL
, /* -- hole -- */
473 NULL
, /* -- hole -- */
474 NULL
, /* SIOCSIWRATE */
475 NULL
, /* SIOCGIWRATE */
476 wlcompat_ioctl
, /* SIOCSIWRTS */
477 wlcompat_ioctl
, /* SIOCGIWRTS */
478 wlcompat_ioctl
, /* SIOCSIWFRAG */
479 wlcompat_ioctl
, /* SIOCGIWFRAG */
480 wlcompat_ioctl
, /* SIOCSIWTXPOW */
481 wlcompat_ioctl
, /* SIOCGIWTXPOW */
482 NULL
, /* SIOCSIWRETRY */
483 NULL
, /* SIOCGIWRETRY */
484 NULL
, /* SIOCSIWENCODE */
485 wlcompat_ioctl
, /* SIOCGIWENCODE */
488 static const struct iw_handler_def wlcompat_handler_def
=
490 .standard
= (iw_handler
*) wlcompat_handler
,
491 .num_standard
= sizeof(wlcompat_handler
)/sizeof(iw_handler
),
494 .private_args
= NULL
,
495 .num_private_args
= 0,
499 static int (*old_ioctl
)(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
);
500 void print_buffer(int len
, unsigned char *buf
) {
503 for (x
=0;x
<len
&& x
<180 ;x
++) {
506 printk("%02X",buf
[x
]);
514 static int new_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
) {
516 printk("dev: %s ioctl: 0x%04x\n",dev
->name
,cmd
);
517 if (cmd
==SIOCDEVPRIVATE
) {
518 wl_ioctl_t
*ioc
= (wl_ioctl_t
*)ifr
->ifr_data
;
519 unsigned char *buf
= ioc
->buf
;
520 printk(" cmd: %d buf: 0x%08x len: %d\n",ioc
->cmd
,&(ioc
->buf
),ioc
->len
);
522 print_buffer(ioc
->len
, buf
);
523 ret
= old_ioctl(dev
,ifr
,cmd
);
525 print_buffer(ioc
->len
, buf
);
526 printk(" ret: %d\n", ret
);
528 ret
= old_ioctl(dev
,ifr
,cmd
);
534 static int __init
wlcompat_init()
536 dev
= dev_get_by_name("eth1");
538 old_ioctl
= dev
->do_ioctl
;
539 dev
->do_ioctl
= new_ioctl
;
541 dev
->wireless_handlers
= (struct iw_handler_def
*)&wlcompat_handler_def
;
545 static void __exit
wlcompat_exit()
547 dev
->wireless_handlers
= NULL
;
549 dev
->do_ioctl
= old_ioctl
;
555 MODULE_AUTHOR("openwrt.org");
556 MODULE_LICENSE("GPL");
558 module_init(wlcompat_init
);
559 module_exit(wlcompat_exit
);