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>
37 static struct net_device
*dev
;
38 char buf
[WLC_IOCTL_MAXLEN
];
40 /* The frequency of each channel in MHz */
41 const long channel_frequency
[] = {
42 2412, 2417, 2422, 2427, 2432, 2437, 2442,
43 2447, 2452, 2457, 2462, 2467, 2472, 2484
45 #define NUM_CHANNELS ( sizeof(channel_frequency) / sizeof(channel_frequency[0]) )
47 static int wl_ioctl(struct net_device
*dev
, int cmd
, void *buf
, int len
)
49 mm_segment_t old_fs
= get_fs();
56 strncpy(ifr
.ifr_name
, dev
->name
, IFNAMSIZ
);
57 ifr
.ifr_data
= (caddr_t
) &ioc
;
59 ret
= dev
->do_ioctl(dev
,&ifr
,SIOCDEVPRIVATE
);
64 static int wlcompat_ioctl_getiwrange(struct net_device
*dev
,
68 struct iw_range
*range
;
70 range
= (struct iw_range
*) extra
;
72 range
->we_version_compiled
= WIRELESS_EXT
;
73 range
->we_version_source
= WIRELESS_EXT
;
75 range
->min_nwid
= range
->max_nwid
= 0;
77 range
->num_channels
= NUM_CHANNELS
;
79 for (i
= 0; i
< NUM_CHANNELS
; i
++) {
80 range
->freq
[k
].i
= i
+ 1;
81 range
->freq
[k
].m
= channel_frequency
[i
] * 100000;
84 if (k
>= IW_MAX_FREQUENCIES
)
87 range
->num_frequency
= k
;
88 range
->sensitivity
= 3;
90 /* nbd: don't know what this means, but other drivers set it this way */
91 range
->pmp_flags
= IW_POWER_PERIOD
;
92 range
->pmt_flags
= IW_POWER_TIMEOUT
;
93 range
->pm_capa
= IW_POWER_PERIOD
| IW_POWER_TIMEOUT
| IW_POWER_UNICAST_R
;
96 range
->max_pmp
= 65535000;
98 range
->max_pmt
= 65535 * 1000;
101 if (wl_ioctl(dev
, WLC_GET_RTS
, &range
->max_rts
, sizeof(int)) < 0)
102 range
->max_rts
= 2347;
104 range
->min_frag
= 256;
106 if (wl_ioctl(dev
, WLC_GET_FRAG
, &range
->max_frag
, sizeof(int)) < 0)
107 range
->max_frag
= 2346;
109 range
->txpower_capa
= IW_TXPOW_MWATT
;
115 static int wlcompat_set_scan(struct net_device
*dev
,
116 struct iw_request_info
*info
,
117 union iwreq_data
*wrqu
,
120 int ap
= 0, oldap
= 0;
121 wl_scan_params_t params
;
123 memset(¶ms
, 0, sizeof(params
));
125 /* use defaults (same parameters as wl scan) */
126 memset(¶ms
.bssid
, 0xff, sizeof(params
.bssid
));
127 params
.bss_type
= DOT11_BSSTYPE_ANY
;
128 params
.scan_type
= -1;
130 params
.active_time
= -1;
131 params
.passive_time
= -1;
132 params
.home_time
= -1;
134 /* can only scan in STA mode */
135 wl_ioctl(dev
, WLC_GET_AP
, &oldap
, sizeof(oldap
));
137 wl_ioctl(dev
, WLC_SET_AP
, &ap
, sizeof(ap
));
139 if (wl_ioctl(dev
, WLC_SCAN
, ¶ms
, 64) < 0)
143 wl_ioctl(dev
, WLC_SET_AP
, &oldap
, sizeof(oldap
));
149 static int wlcompat_get_scan(struct net_device
*dev
,
150 struct iw_request_info
*info
,
151 union iwreq_data
*wrqu
,
154 wl_scan_results_t
*results
= (wl_scan_results_t
*) buf
;
155 wl_bss_info_t
*bss_info
;
157 char *current_ev
= extra
;
159 char *end_buf
= extra
+ IW_SCAN_MAX_DATA
;
163 if (wl_ioctl(dev
, WLC_SCAN_RESULTS
, buf
, WLC_IOCTL_MAXLEN
) < 0)
166 bss_info
= &(results
->bss_info
[0]);
167 info_ptr
= (char *) bss_info
;
168 for (i
= 0; i
< results
->count
; i
++) {
170 /* send the cell address (must be sent first) */
172 iwe
.u
.ap_addr
.sa_family
= ARPHRD_ETHER
;
173 memcpy(&iwe
.u
.ap_addr
.sa_data
, &bss_info
->BSSID
, sizeof(bss_info
->BSSID
));
174 current_ev
= iwe_stream_add_event(current_ev
, end_buf
, &iwe
, IW_EV_ADDR_LEN
);
177 iwe
.cmd
= SIOCGIWESSID
;
178 iwe
.u
.data
.length
= bss_info
->SSID_len
;
179 if (iwe
.u
.data
.length
> IW_ESSID_MAX_SIZE
)
180 iwe
.u
.data
.length
= IW_ESSID_MAX_SIZE
;
181 iwe
.u
.data
.flags
= 1;
182 current_ev
= iwe_stream_add_point(current_ev
, end_buf
, &iwe
, bss_info
->SSID
);
184 /* send frequency/channel info */
185 iwe
.cmd
= SIOCGIWFREQ
;
187 iwe
.u
.freq
.m
= bss_info
->channel
;
188 current_ev
= iwe_stream_add_event(current_ev
, end_buf
, &iwe
, IW_EV_FREQ_LEN
);
190 /* add quality statistics */
192 iwe
.u
.qual
.level
= bss_info
->RSSI
;
193 iwe
.u
.qual
.noise
= bss_info
->phy_noise
;
195 current_ev
= iwe_stream_add_event(current_ev
, end_buf
, &iwe
, IW_EV_QUAL_LEN
);
197 /* send rate information */
198 iwe
.cmd
= SIOCGIWRATE
;
199 current_val
= current_ev
+ IW_EV_LCP_LEN
;
200 iwe
.u
.bitrate
.fixed
= iwe
.u
.bitrate
.disabled
= 0;
202 for(j
= 0 ; j
< bss_info
->rateset
.count
; j
++) {
203 iwe
.u
.bitrate
.value
= ((bss_info
->rateset
.rates
[j
] & 0x7f) * 500000);
204 current_val
= iwe_stream_add_value(current_ev
, current_val
, end_buf
, &iwe
, IW_EV_PARAM_LEN
);
206 if((current_val
- current_ev
) > IW_EV_LCP_LEN
)
207 current_ev
= current_val
;
209 info_ptr
+= sizeof(wl_bss_info_t
);
210 if (bss_info
->ie_length
% 4)
211 info_ptr
+= bss_info
->ie_length
+ 4 - (bss_info
->ie_length
% 4);
213 info_ptr
+= bss_info
->ie_length
;
214 bss_info
= (wl_bss_info_t
*) info_ptr
;
217 wrqu
->data
.length
= (current_ev
- extra
);
218 wrqu
->data
.flags
= 0;
223 static int wlcompat_ioctl(struct net_device
*dev
,
224 struct iw_request_info
*info
,
225 union iwreq_data
*wrqu
,
230 strcpy(wrqu
->name
, "IEEE 802.11-DS");
236 if (wl_ioctl(dev
,WLC_GET_CHANNEL
, &ci
, sizeof(ci
)) < 0)
239 wrqu
->freq
.m
= ci
.target_channel
;
245 if (wrqu
->freq
.e
== 1) {
247 int f
= wrqu
->freq
.m
/ 100000;
248 while ((channel
< NUM_CHANNELS
+ 1) && (f
!= channel_frequency
[channel
]))
251 if (channel
== NUM_CHANNELS
) // channel not found
255 wrqu
->freq
.m
= channel
+ 1;
257 if ((wrqu
->freq
.e
== 0) && (wrqu
->freq
.m
< 1000)) {
258 if (wl_ioctl(dev
, WLC_SET_CHANNEL
, &wrqu
->freq
.m
, sizeof(int)) < 0)
267 wrqu
->ap_addr
.sa_family
= ARPHRD_ETHER
;
268 if (wl_ioctl(dev
,WLC_GET_BSSID
,wrqu
->ap_addr
.sa_data
,6) < 0)
276 if (wl_ioctl(dev
,WLC_GET_SSID
, &ssid
, sizeof(wlc_ssid_t
)) < 0)
279 wrqu
->essid
.flags
= wrqu
->data
.flags
= 1;
280 wrqu
->essid
.length
= wrqu
->data
.length
= ssid
.SSID_len
+ 1;
281 memcpy(extra
,ssid
.SSID
,ssid
.SSID_len
+ 1);
287 memset(&ssid
, 0, sizeof(ssid
));
288 ssid
.SSID_len
= strlen(extra
);
289 if (ssid
.SSID_len
> WLC_ESSID_MAX_SIZE
)
290 ssid
.SSID_len
= WLC_ESSID_MAX_SIZE
;
291 memcpy(ssid
.SSID
, extra
, ssid
.SSID_len
);
292 if (wl_ioctl(dev
, WLC_SET_SSID
, &ssid
, sizeof(ssid
)) < 0)
298 if (wl_ioctl(dev
,WLC_GET_RTS
,&(wrqu
->rts
.value
),sizeof(int)) < 0)
304 if (wl_ioctl(dev
,WLC_SET_RTS
,&(wrqu
->rts
.value
),sizeof(int)) < 0)
310 if (wl_ioctl(dev
,WLC_GET_FRAG
,&(wrqu
->frag
.value
),sizeof(int)) < 0)
316 if (wl_ioctl(dev
,WLC_SET_FRAG
,&(wrqu
->frag
.value
),sizeof(int)) < 0)
322 wrqu
->txpower
.value
= 0;
323 if (wl_ioctl(dev
,WLC_GET_TXPWR
, &(wrqu
->txpower
.value
), sizeof(int)) < 0)
325 wrqu
->txpower
.fixed
= 0;
326 wrqu
->txpower
.disabled
= 0;
327 wrqu
->txpower
.flags
= IW_TXPOW_MWATT
;
332 if (wrqu
->txpower
.flags
!= IW_TXPOW_MWATT
)
335 if (wl_ioctl(dev
, WLC_SET_TXPWR
, &wrqu
->txpower
.value
, sizeof(int)) < 0)
340 wrqu
->data
.flags
= IW_ENCODE_DISABLED
;
345 return wlcompat_ioctl_getiwrange(dev
, extra
);
350 int ap
= -1, infra
= -1, passive
= 0, wet
= 0;
352 switch (wrqu
->mode
) {
353 case IW_MODE_MONITOR
:
377 if (wl_ioctl(dev
, WLC_SET_PASSIVE
, &passive
, sizeof(passive
)) < 0)
379 if (wl_ioctl(dev
, WLC_SET_MONITOR
, &passive
, sizeof(passive
)) < 0)
381 if (wl_ioctl(dev
, WLC_SET_WET
, &wet
, sizeof(wet
)) < 0)
384 if (wl_ioctl(dev
, WLC_SET_AP
, &ap
, sizeof(ap
)) < 0)
387 if (wl_ioctl(dev
, WLC_SET_INFRA
, &infra
, sizeof(infra
)) < 0)
395 int ap
, infra
, wet
, passive
;
397 if (wl_ioctl(dev
, WLC_GET_AP
, &ap
, sizeof(ap
)) < 0)
399 if (wl_ioctl(dev
, WLC_GET_INFRA
, &infra
, sizeof(infra
)) < 0)
401 if (wl_ioctl(dev
, WLC_GET_PASSIVE
, &passive
, sizeof(passive
)) < 0)
403 if (wl_ioctl(dev
, WLC_GET_WET
, &wet
, sizeof(wet
)) < 0)
407 wrqu
->mode
= IW_MODE_MONITOR
;
409 wrqu
->mode
= IW_MODE_ADHOC
;
412 wrqu
->mode
= IW_MODE_MASTER
;
415 wrqu
->mode
= IW_MODE_REPEAT
;
417 wrqu
->mode
= IW_MODE_INFRA
;
432 static const iw_handler wlcompat_handler
[] = {
433 NULL
, /* SIOCSIWCOMMIT */
434 wlcompat_ioctl
, /* SIOCGIWNAME */
435 NULL
, /* SIOCSIWNWID */
436 NULL
, /* SIOCGIWNWID */
437 wlcompat_ioctl
, /* SIOCSIWFREQ */
438 wlcompat_ioctl
, /* SIOCGIWFREQ */
439 wlcompat_ioctl
, /* SIOCSIWMODE */
440 wlcompat_ioctl
, /* SIOCGIWMODE */
441 NULL
, /* SIOCSIWSENS */
442 NULL
, /* SIOCGIWSENS */
443 NULL
, /* SIOCSIWRANGE, unused */
444 wlcompat_ioctl
, /* SIOCGIWRANGE */
445 NULL
, /* SIOCSIWPRIV */
446 NULL
, /* SIOCGIWPRIV */
447 NULL
, /* SIOCSIWSTATS */
448 NULL
, /* SIOCGIWSTATS */
449 iw_handler_set_spy
, /* SIOCSIWSPY */
450 iw_handler_get_spy
, /* SIOCGIWSPY */
451 iw_handler_set_thrspy
, /* SIOCSIWTHRSPY */
452 iw_handler_get_thrspy
, /* SIOCGIWTHRSPY */
453 NULL
, /* SIOCSIWAP */
454 wlcompat_ioctl
, /* SIOCGIWAP */
455 NULL
, /* -- hole -- */
456 NULL
, /* SIOCGIWAPLIST */
457 wlcompat_set_scan
, /* SIOCSIWSCAN */
458 wlcompat_get_scan
, /* SIOCGIWSCAN */
459 wlcompat_ioctl
, /* SIOCSIWESSID */
460 wlcompat_ioctl
, /* SIOCGIWESSID */
461 NULL
, /* SIOCSIWNICKN */
462 NULL
, /* SIOCGIWNICKN */
463 NULL
, /* -- hole -- */
464 NULL
, /* -- hole -- */
465 NULL
, /* SIOCSIWRATE */
466 NULL
, /* SIOCGIWRATE */
467 wlcompat_ioctl
, /* SIOCSIWRTS */
468 wlcompat_ioctl
, /* SIOCGIWRTS */
469 wlcompat_ioctl
, /* SIOCSIWFRAG */
470 wlcompat_ioctl
, /* SIOCGIWFRAG */
471 wlcompat_ioctl
, /* SIOCSIWTXPOW */
472 wlcompat_ioctl
, /* SIOCGIWTXPOW */
473 NULL
, /* SIOCSIWRETRY */
474 NULL
, /* SIOCGIWRETRY */
475 NULL
, /* SIOCSIWENCODE */
476 wlcompat_ioctl
, /* SIOCGIWENCODE */
479 static const struct iw_handler_def wlcompat_handler_def
=
481 .standard
= (iw_handler
*) wlcompat_handler
,
482 .num_standard
= sizeof(wlcompat_handler
)/sizeof(iw_handler
),
485 .private_args
= NULL
,
486 .num_private_args
= 0,
490 static int (*old_ioctl
)(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
);
491 static int new_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
) {
492 int ret
= old_ioctl(dev
,ifr
,cmd
);
493 printk("dev: %s ioctl: 0x%04x\n",dev
->name
,cmd
);
494 if (cmd
==SIOCDEVPRIVATE
) {
496 wl_ioctl_t
*ioc
= (wl_ioctl_t
*)ifr
->ifr_data
;
497 unsigned char *buf
= ioc
->buf
;
498 printk(" cmd: %d buf: 0x%08x len: %d\n",ioc
->cmd
,&(ioc
->buf
),ioc
->len
);
500 for (x
=0;x
<ioc
->len
&& x
<128 ;x
++) {
501 printk("%02X",buf
[x
]);
509 static int __init
wlcompat_init()
511 dev
= dev_get_by_name("eth1");
513 old_ioctl
= dev
->do_ioctl
;
514 dev
->do_ioctl
= new_ioctl
;
516 dev
->wireless_handlers
= (struct iw_handler_def
*)&wlcompat_handler_def
;
520 static void __exit
wlcompat_exit()
522 dev
->wireless_handlers
= NULL
;
524 dev
->do_ioctl
= old_ioctl
;
530 MODULE_AUTHOR("openwrt.org");
531 MODULE_LICENSE("GPL");
533 module_init(wlcompat_init
);
534 module_exit(wlcompat_exit
);