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>
36 static struct net_device
*dev
;
37 char buf
[WLC_IOCTL_MAXLEN
];
39 /* The frequency of each channel in MHz */
40 const long channel_frequency
[] = {
41 2412, 2417, 2422, 2427, 2432, 2437, 2442,
42 2447, 2452, 2457, 2462, 2467, 2472, 2484
44 #define NUM_CHANNELS ( sizeof(channel_frequency) / sizeof(channel_frequency[0]) )
48 static int wlcompat_private_ioctl(struct net_device
*dev
,
49 struct iw_request_info
*info
,
50 union iwreq_data
*wrqu
,
53 static int wl_ioctl(struct net_device
*dev
, int cmd
, void *buf
, int len
)
55 mm_segment_t old_fs
= get_fs();
62 strncpy(ifr
.ifr_name
, dev
->name
, IFNAMSIZ
);
63 ifr
.ifr_data
= (caddr_t
) &ioc
;
65 ret
= dev
->do_ioctl(dev
,&ifr
,SIOCDEVPRIVATE
);
70 static int wl_set_val(struct net_device
*dev
, char *var
, void *val
, int len
)
75 /* check for overflow */
76 if ((buf_len
= strlen(var
)) + 1 + len
> sizeof(buf
))
82 /* append int value onto the end of the name string */
83 memcpy(&buf
[buf_len
], val
, len
);
86 return wl_ioctl(dev
, WLC_SET_VAR
, buf
, buf_len
);
89 static int wl_get_val(struct net_device
*dev
, char *var
, void *val
, int len
)
94 /* check for overflow */
95 if (strlen(var
) + 1 > sizeof(buf
) || len
> sizeof(buf
))
99 if ((ret
= wl_ioctl(dev
, WLC_GET_VAR
, buf
, sizeof(buf
))))
102 memcpy(val
, buf
, len
);
106 int read_shmem(struct net_device
*dev
, int offset
)
108 if (wl_ioctl(dev
, WLC_GET_SHMEM
, &offset
, sizeof(offset
)) < 0)
114 static int wlcompat_ioctl_getiwrange(struct net_device
*dev
,
118 struct iw_range
*range
;
120 range
= (struct iw_range
*) extra
;
122 range
->we_version_compiled
= WIRELESS_EXT
;
123 range
->we_version_source
= WIRELESS_EXT
;
125 range
->min_nwid
= range
->max_nwid
= 0;
127 range
->num_channels
= NUM_CHANNELS
;
129 for (i
= 0; i
< NUM_CHANNELS
; i
++) {
130 range
->freq
[k
].i
= i
+ 1;
131 range
->freq
[k
].m
= channel_frequency
[i
] * 100000;
132 range
->freq
[k
].e
= 1;
134 if (k
>= IW_MAX_FREQUENCIES
)
137 range
->num_frequency
= k
;
138 range
->sensitivity
= 3;
140 /* nbd: don't know what this means, but other drivers set it this way */
141 range
->pmp_flags
= IW_POWER_PERIOD
;
142 range
->pmt_flags
= IW_POWER_TIMEOUT
;
143 range
->pm_capa
= IW_POWER_PERIOD
| IW_POWER_TIMEOUT
| IW_POWER_UNICAST_R
;
146 range
->max_pmp
= 65535000;
148 range
->max_pmt
= 65535 * 1000;
151 if (wl_ioctl(dev
, WLC_GET_RTS
, &range
->max_rts
, sizeof(int)) < 0)
152 range
->max_rts
= 2347;
154 range
->min_frag
= 256;
156 if (wl_ioctl(dev
, WLC_GET_FRAG
, &range
->max_frag
, sizeof(int)) < 0)
157 range
->max_frag
= 2346;
159 range
->txpower_capa
= IW_TXPOW_MWATT
;
165 static int wlcompat_set_scan(struct net_device
*dev
,
166 struct iw_request_info
*info
,
167 union iwreq_data
*wrqu
,
170 int ap
= 0, oldap
= 0;
171 wl_scan_params_t params
;
173 memset(¶ms
, 0, sizeof(params
));
175 /* use defaults (same parameters as wl scan) */
176 memset(¶ms
.bssid
, 0xff, sizeof(params
.bssid
));
177 params
.bss_type
= DOT11_BSSTYPE_ANY
;
178 params
.scan_type
= -1;
180 params
.active_time
= -1;
181 params
.passive_time
= -1;
182 params
.home_time
= -1;
184 /* can only scan in STA mode */
185 wl_ioctl(dev
, WLC_GET_AP
, &oldap
, sizeof(oldap
));
187 wl_ioctl(dev
, WLC_SET_AP
, &ap
, sizeof(ap
));
189 if (wl_ioctl(dev
, WLC_SCAN
, ¶ms
, 64) < 0)
193 wl_ioctl(dev
, WLC_SET_AP
, &oldap
, sizeof(oldap
));
199 static int wlcompat_get_scan(struct net_device
*dev
,
200 struct iw_request_info
*info
,
201 union iwreq_data
*wrqu
,
204 wl_scan_results_t
*results
= (wl_scan_results_t
*) buf
;
205 wl_bss_info_t
*bss_info
;
207 char *current_ev
= extra
;
209 char *end_buf
= extra
+ IW_SCAN_MAX_DATA
;
213 if (wl_ioctl(dev
, WLC_SCAN_RESULTS
, buf
, WLC_IOCTL_MAXLEN
) < 0)
216 bss_info
= &(results
->bss_info
[0]);
217 info_ptr
= (char *) bss_info
;
218 for (i
= 0; i
< results
->count
; i
++) {
220 /* send the cell address (must be sent first) */
222 iwe
.u
.ap_addr
.sa_family
= ARPHRD_ETHER
;
223 memcpy(&iwe
.u
.ap_addr
.sa_data
, &bss_info
->BSSID
, sizeof(bss_info
->BSSID
));
224 current_ev
= iwe_stream_add_event(current_ev
, end_buf
, &iwe
, IW_EV_ADDR_LEN
);
227 iwe
.cmd
= SIOCGIWESSID
;
228 iwe
.u
.data
.length
= bss_info
->SSID_len
;
229 if (iwe
.u
.data
.length
> IW_ESSID_MAX_SIZE
)
230 iwe
.u
.data
.length
= IW_ESSID_MAX_SIZE
;
231 iwe
.u
.data
.flags
= 1;
232 current_ev
= iwe_stream_add_point(current_ev
, end_buf
, &iwe
, bss_info
->SSID
);
234 /* send frequency/channel info */
235 iwe
.cmd
= SIOCGIWFREQ
;
237 iwe
.u
.freq
.m
= bss_info
->channel
;
238 current_ev
= iwe_stream_add_event(current_ev
, end_buf
, &iwe
, IW_EV_FREQ_LEN
);
240 /* add quality statistics */
242 iwe
.u
.qual
.level
= bss_info
->RSSI
;
243 iwe
.u
.qual
.noise
= bss_info
->phy_noise
;
245 current_ev
= iwe_stream_add_event(current_ev
, end_buf
, &iwe
, IW_EV_QUAL_LEN
);
247 /* send rate information */
248 iwe
.cmd
= SIOCGIWRATE
;
249 current_val
= current_ev
+ IW_EV_LCP_LEN
;
250 iwe
.u
.bitrate
.fixed
= iwe
.u
.bitrate
.disabled
= 0;
252 for(j
= 0 ; j
< bss_info
->rateset
.count
; j
++) {
253 iwe
.u
.bitrate
.value
= ((bss_info
->rateset
.rates
[j
] & 0x7f) * 500000);
254 current_val
= iwe_stream_add_value(current_ev
, current_val
, end_buf
, &iwe
, IW_EV_PARAM_LEN
);
256 if((current_val
- current_ev
) > IW_EV_LCP_LEN
)
257 current_ev
= current_val
;
259 info_ptr
+= sizeof(wl_bss_info_t
);
260 if (bss_info
->ie_length
% 4)
261 info_ptr
+= bss_info
->ie_length
+ 4 - (bss_info
->ie_length
% 4);
263 info_ptr
+= bss_info
->ie_length
;
264 bss_info
= (wl_bss_info_t
*) info_ptr
;
267 wrqu
->data
.length
= (current_ev
- extra
);
268 wrqu
->data
.flags
= 0;
273 static int wlcompat_ioctl(struct net_device
*dev
,
274 struct iw_request_info
*info
,
275 union iwreq_data
*wrqu
,
280 strcpy(wrqu
->name
, "IEEE 802.11-DS");
286 if (wl_ioctl(dev
,WLC_GET_CHANNEL
, &ci
, sizeof(ci
)) < 0)
289 wrqu
->freq
.m
= ci
.target_channel
;
295 if (wrqu
->freq
.e
== 1) {
297 int f
= wrqu
->freq
.m
/ 100000;
298 while ((channel
< NUM_CHANNELS
+ 1) && (f
!= channel_frequency
[channel
]))
301 if (channel
== NUM_CHANNELS
) // channel not found
305 wrqu
->freq
.m
= channel
+ 1;
307 if ((wrqu
->freq
.e
== 0) && (wrqu
->freq
.m
< 1000)) {
308 if (wl_ioctl(dev
, WLC_SET_CHANNEL
, &wrqu
->freq
.m
, sizeof(int)) < 0)
319 if (wrqu
->ap_addr
.sa_family
!= ARPHRD_ETHER
)
322 if (wl_ioctl(dev
, WLC_GET_AP
, &ap
, sizeof(ap
)) < 0)
325 if (wl_ioctl(dev
, (ap
? WLC_SET_BSSID
: WLC_REASSOC
), wrqu
->ap_addr
.sa_data
, 6) < 0)
332 wrqu
->ap_addr
.sa_family
= ARPHRD_ETHER
;
333 if (wl_ioctl(dev
,WLC_GET_BSSID
,wrqu
->ap_addr
.sa_data
,6) < 0)
341 if (wl_ioctl(dev
,WLC_GET_SSID
, &ssid
, sizeof(wlc_ssid_t
)) < 0)
344 wrqu
->essid
.flags
= wrqu
->data
.flags
= 1;
345 wrqu
->essid
.length
= wrqu
->data
.length
= ssid
.SSID_len
+ 1;
346 memcpy(extra
,ssid
.SSID
,ssid
.SSID_len
+ 1);
352 memset(&ssid
, 0, sizeof(ssid
));
353 ssid
.SSID_len
= strlen(extra
);
354 if (ssid
.SSID_len
> WLC_ESSID_MAX_SIZE
)
355 ssid
.SSID_len
= WLC_ESSID_MAX_SIZE
;
356 memcpy(ssid
.SSID
, extra
, ssid
.SSID_len
);
357 if (wl_ioctl(dev
, WLC_SET_SSID
, &ssid
, sizeof(ssid
)) < 0)
363 if (wl_ioctl(dev
,WLC_GET_RTS
,&(wrqu
->rts
.value
),sizeof(int)) < 0)
369 if (wl_ioctl(dev
,WLC_SET_RTS
,&(wrqu
->rts
.value
),sizeof(int)) < 0)
375 if (wl_ioctl(dev
,WLC_GET_FRAG
,&(wrqu
->frag
.value
),sizeof(int)) < 0)
381 if (wl_ioctl(dev
,WLC_SET_FRAG
,&(wrqu
->frag
.value
),sizeof(int)) < 0)
387 if (wl_get_val(dev
, "qtxpower", &(wrqu
->txpower
.value
), sizeof(int)) < 0)
390 wrqu
->txpower
.value
&= ~WL_TXPWR_OVERRIDE
;
392 wrqu
->txpower
.fixed
= 0;
393 wrqu
->txpower
.disabled
= 0;
394 wrqu
->txpower
.flags
= IW_TXPOW_MWATT
;
401 if (wl_get_val(dev
, "qtxpower", &override
, sizeof(int)) < 0)
404 wrqu
->txpower
.value
|= override
& WL_TXPWR_OVERRIDE
;
406 if (wrqu
->txpower
.flags
!= IW_TXPOW_MWATT
)
409 if (wl_set_val(dev
, "qtxpower", &wrqu
->txpower
.value
, sizeof(int)) < 0)
416 if (wl_ioctl(dev
, WLC_GET_WEP
, &val
, sizeof(val
)) < 0)
423 for (key
= val
= 0; (key
< 4) && (val
== 0); key
++) {
425 if (wl_ioctl(dev
, WLC_GET_KEY_PRIMARY
, &val
, sizeof(val
)) < 0)
429 wrqu
->data
.flags
= IW_ENCODE_ENABLED
;
434 magic_offset
= read_shmem(dev
, 0x56) * 2;
436 wrqu
->data
.flags
|= key
+ 1;
437 wrqu
->data
.length
= 16;
439 for (val
= 0; val
< 8; val
++) {
440 buffer
[val
] = read_shmem(dev
, magic_offset
+ (key
* 16) + val
* 2);
443 memset(extra
, 0, 16);
444 memcpy(extra
, buffer
, 16);
446 wrqu
->data
.flags
|= IW_ENCODE_NOKEY
;
449 wrqu
->data
.flags
= IW_ENCODE_DISABLED
;
456 return wlcompat_ioctl_getiwrange(dev
, extra
);
461 int ap
= -1, infra
= -1, passive
= 0, wet
= 0;
463 switch (wrqu
->mode
) {
464 case IW_MODE_MONITOR
:
488 if (wl_ioctl(dev
, WLC_SET_PASSIVE
, &passive
, sizeof(passive
)) < 0)
490 if (wl_ioctl(dev
, WLC_SET_MONITOR
, &passive
, sizeof(passive
)) < 0)
492 if (wl_ioctl(dev
, WLC_SET_WET
, &wet
, sizeof(wet
)) < 0)
495 if (wl_ioctl(dev
, WLC_SET_AP
, &ap
, sizeof(ap
)) < 0)
498 if (wl_ioctl(dev
, WLC_SET_INFRA
, &infra
, sizeof(infra
)) < 0)
506 int ap
, infra
, wet
, passive
;
508 if (wl_ioctl(dev
, WLC_GET_AP
, &ap
, sizeof(ap
)) < 0)
510 if (wl_ioctl(dev
, WLC_GET_INFRA
, &infra
, sizeof(infra
)) < 0)
512 if (wl_ioctl(dev
, WLC_GET_PASSIVE
, &passive
, sizeof(passive
)) < 0)
514 if (wl_ioctl(dev
, WLC_GET_WET
, &wet
, sizeof(wet
)) < 0)
518 wrqu
->mode
= IW_MODE_MONITOR
;
520 wrqu
->mode
= IW_MODE_ADHOC
;
523 wrqu
->mode
= IW_MODE_MASTER
;
526 wrqu
->mode
= IW_MODE_REPEAT
;
528 wrqu
->mode
= IW_MODE_INFRA
;
536 if (info
->cmd
>= SIOCIWFIRSTPRIV
)
537 return wlcompat_private_ioctl(dev
, info
, wrqu
, extra
);
546 static const iw_handler wlcompat_handler
[] = {
547 NULL
, /* SIOCSIWCOMMIT */
548 wlcompat_ioctl
, /* SIOCGIWNAME */
549 NULL
, /* SIOCSIWNWID */
550 NULL
, /* SIOCGIWNWID */
551 wlcompat_ioctl
, /* SIOCSIWFREQ */
552 wlcompat_ioctl
, /* SIOCGIWFREQ */
553 wlcompat_ioctl
, /* SIOCSIWMODE */
554 wlcompat_ioctl
, /* SIOCGIWMODE */
555 NULL
, /* SIOCSIWSENS */
556 NULL
, /* SIOCGIWSENS */
557 NULL
, /* SIOCSIWRANGE, unused */
558 wlcompat_ioctl
, /* SIOCGIWRANGE */
559 NULL
, /* SIOCSIWPRIV */
560 NULL
, /* SIOCGIWPRIV */
561 NULL
, /* SIOCSIWSTATS */
562 NULL
, /* SIOCGIWSTATS */
563 iw_handler_set_spy
, /* SIOCSIWSPY */
564 iw_handler_get_spy
, /* SIOCGIWSPY */
565 iw_handler_set_thrspy
, /* SIOCSIWTHRSPY */
566 iw_handler_get_thrspy
, /* SIOCGIWTHRSPY */
567 wlcompat_ioctl
, /* SIOCSIWAP */
568 wlcompat_ioctl
, /* SIOCGIWAP */
569 NULL
, /* -- hole -- */
570 NULL
, /* SIOCGIWAPLIST */
571 wlcompat_set_scan
, /* SIOCSIWSCAN */
572 wlcompat_get_scan
, /* SIOCGIWSCAN */
573 wlcompat_ioctl
, /* SIOCSIWESSID */
574 wlcompat_ioctl
, /* SIOCGIWESSID */
575 NULL
, /* SIOCSIWNICKN */
576 NULL
, /* SIOCGIWNICKN */
577 NULL
, /* -- hole -- */
578 NULL
, /* -- hole -- */
579 NULL
, /* SIOCSIWRATE */
580 NULL
, /* SIOCGIWRATE */
581 wlcompat_ioctl
, /* SIOCSIWRTS */
582 wlcompat_ioctl
, /* SIOCGIWRTS */
583 wlcompat_ioctl
, /* SIOCSIWFRAG */
584 wlcompat_ioctl
, /* SIOCGIWFRAG */
585 wlcompat_ioctl
, /* SIOCSIWTXPOW */
586 wlcompat_ioctl
, /* SIOCGIWTXPOW */
587 NULL
, /* SIOCSIWRETRY */
588 NULL
, /* SIOCGIWRETRY */
589 NULL
, /* SIOCSIWENCODE */
590 wlcompat_ioctl
, /* SIOCGIWENCODE */
593 static int wlcompat_private_ioctl(struct net_device
*dev
,
594 struct iw_request_info
*info
,
595 union iwreq_data
*wrqu
,
598 int *value
= (int *) wrqu
->name
;
601 case WLCOMPAT_SET_MONITOR
:
603 if (wl_ioctl(dev
, WLC_SET_MONITOR
, value
, sizeof(int)) < 0)
608 case WLCOMPAT_GET_MONITOR
:
610 if (wl_ioctl(dev
, WLC_GET_MONITOR
, extra
, sizeof(int)) < 0)
615 case WLCOMPAT_SET_TXPWR_LIMIT
:
620 if (wl_get_val(dev
, "qtxpower", &val
, sizeof(int)) < 0)
624 val
|= WL_TXPWR_OVERRIDE
;
626 val
&= ~WL_TXPWR_OVERRIDE
;
628 if (wl_set_val(dev
, "qtxpower", &val
, sizeof(int)) < 0)
633 case WLCOMPAT_GET_TXPWR_LIMIT
:
635 if (wl_get_val(dev
, "qtxpower", value
, sizeof(int)) < 0)
638 *value
= ((*value
& WL_TXPWR_OVERRIDE
) == WL_TXPWR_OVERRIDE
? 1 : 0);
642 case WLCOMPAT_SET_ANTDIV
:
644 if (wl_ioctl(dev
, WLC_SET_ANTDIV
, value
, sizeof(int)) < 0)
649 case WLCOMPAT_GET_ANTDIV
:
651 if (wl_ioctl(dev
, WLC_GET_ANTDIV
, extra
, sizeof(int)) < 0)
656 case WLCOMPAT_SET_TXANT
:
658 if (wl_ioctl(dev
, WLC_SET_TXANT
, value
, sizeof(int)) < 0)
663 case WLCOMPAT_GET_TXANT
:
665 if (wl_ioctl(dev
, WLC_GET_TXANT
, extra
, sizeof(int)) < 0)
679 static const struct iw_priv_args wlcompat_private_args
[] =
681 { WLCOMPAT_SET_MONITOR
,
682 IW_PRIV_TYPE_INT
| IW_PRIV_SIZE_FIXED
| 1,
686 { WLCOMPAT_GET_MONITOR
,
688 IW_PRIV_TYPE_INT
| IW_PRIV_SIZE_FIXED
| 1,
691 { WLCOMPAT_SET_TXPWR_LIMIT
,
692 IW_PRIV_TYPE_INT
| IW_PRIV_SIZE_FIXED
| 1,
696 { WLCOMPAT_GET_TXPWR_LIMIT
,
698 IW_PRIV_TYPE_INT
| IW_PRIV_SIZE_FIXED
| 1,
701 { WLCOMPAT_SET_ANTDIV
,
702 IW_PRIV_TYPE_INT
| IW_PRIV_SIZE_FIXED
| 1,
706 { WLCOMPAT_GET_ANTDIV
,
708 IW_PRIV_TYPE_INT
| IW_PRIV_SIZE_FIXED
| 1,
711 { WLCOMPAT_SET_TXANT
,
712 IW_PRIV_TYPE_INT
| IW_PRIV_SIZE_FIXED
| 1,
716 { WLCOMPAT_GET_TXANT
,
718 IW_PRIV_TYPE_INT
| IW_PRIV_SIZE_FIXED
| 1,
723 static const iw_handler wlcompat_private
[] =
725 wlcompat_private_ioctl
,
730 static const struct iw_handler_def wlcompat_handler_def
=
732 .standard
= (iw_handler
*) wlcompat_handler
,
733 .num_standard
= sizeof(wlcompat_handler
)/sizeof(iw_handler
),
734 .private = wlcompat_private
,
736 .private_args
= wlcompat_private_args
,
737 .num_private_args
= sizeof(wlcompat_private_args
) / sizeof(wlcompat_private_args
[0])
742 void print_buffer(int len
, unsigned char *buf
) {
745 for (x
=0;x
<len
&& x
<180 ;x
++) {
748 printk("%02X",buf
[x
]);
757 static int (*old_ioctl
)(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
);
758 static int new_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
) {
760 struct iwreq
*iwr
= (struct iwreq
*) ifr
;
761 struct iw_request_info info
;
764 printk("dev: %s ioctl: 0x%04x\n",dev
->name
,cmd
);
767 if (cmd
>= SIOCIWFIRSTPRIV
) {
770 ret
= wlcompat_private_ioctl(dev
, &info
, &(iwr
->u
), (char *) &(iwr
->u
));
772 } else if (cmd
==SIOCDEVPRIVATE
) {
773 wl_ioctl_t
*ioc
= (wl_ioctl_t
*)ifr
->ifr_data
;
774 unsigned char *buf
= ioc
->buf
;
775 printk(" cmd: %d buf: 0x%08x len: %d\n",ioc
->cmd
,&(ioc
->buf
),ioc
->len
);
777 print_buffer(ioc
->len
, buf
);
778 ret
= old_ioctl(dev
,ifr
,cmd
);
780 print_buffer(ioc
->len
, buf
);
781 printk(" ret: %d\n", ret
);
784 ret
= old_ioctl(dev
,ifr
,cmd
);
789 static int __init
wlcompat_init()
792 char *devname
= "eth0";
794 while (!found
&& (dev
= dev_get_by_name(devname
))) {
795 if ((dev
->wireless_handlers
== NULL
) && ((wl_ioctl(dev
, WLC_GET_MAGIC
, &i
, sizeof(i
)) == 0) && i
== WLC_IOCTL_MAGIC
))
801 printk("No Broadcom devices found.\n");
806 old_ioctl
= dev
->do_ioctl
;
807 dev
->do_ioctl
= new_ioctl
;
808 dev
->wireless_handlers
= (struct iw_handler_def
*)&wlcompat_handler_def
;
812 static void __exit
wlcompat_exit()
814 dev
->wireless_handlers
= NULL
;
815 dev
->do_ioctl
= old_ioctl
;
820 MODULE_AUTHOR("openwrt.org");
821 MODULE_LICENSE("GPL");
823 module_init(wlcompat_init
);
824 module_exit(wlcompat_exit
);