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]) )
46 typedef struct internal_wsec_key {
48 uint8 unknown_1; // 0x01
50 uint8 unknown_2[7]; // 0x03
53 char data[32]; // 0x0e
57 static int wlcompat_private_ioctl(struct net_device *dev,
58 struct iw_request_info *info,
59 union iwreq_data *wrqu,
62 void print_buffer(int len, unsigned char *buf);
65 static int wl_ioctl(struct net_device *dev, int cmd, void *buf, int len)
67 mm_segment_t old_fs = get_fs();
74 strncpy(ifr.ifr_name, dev->name, IFNAMSIZ);
75 ifr.ifr_data = (caddr_t) &ioc;
77 ret = dev->do_ioctl(dev,&ifr,SIOCDEVPRIVATE);
82 static int wl_set_val(struct net_device *dev, char *var, void *val, int len)
87 /* check for overflow */
88 if ((buf_len = strlen(var)) + 1 + len > sizeof(buf))
94 /* append int value onto the end of the name string */
95 memcpy(&buf[buf_len], val, len);
98 return wl_ioctl(dev, WLC_SET_VAR, buf, buf_len);
101 static int wl_get_val(struct net_device *dev, char *var, void *val, int len)
106 /* check for overflow */
107 if (strlen(var) + 1 > sizeof(buf) || len > sizeof(buf))
111 if ((ret = wl_ioctl(dev, WLC_GET_VAR, buf, sizeof(buf))))
114 memcpy(val, buf, len);
118 int get_primary_key(struct net_device *dev)
122 for (key = val = 0; (key < 4) && (val == 0); key++) {
124 if (wl_ioctl(dev, WLC_GET_KEY_PRIMARY, &val, sizeof(val)) < 0)
131 static int wlcompat_ioctl_getiwrange(struct net_device *dev,
135 struct iw_range *range;
137 range = (struct iw_range *) extra;
139 range->we_version_compiled = WIRELESS_EXT;
140 range->we_version_source = WIRELESS_EXT;
142 range->min_nwid = range->max_nwid = 0;
144 range->num_channels = NUM_CHANNELS;
146 for (i = 0; i < NUM_CHANNELS; i++) {
147 range->freq[k].i = i + 1;
148 range->freq[k].m = channel_frequency[i] * 100000;
149 range->freq[k].e = 1;
151 if (k >= IW_MAX_FREQUENCIES)
154 range->num_frequency = k;
155 range->sensitivity = 3;
157 /* nbd: don't know what this means, but other drivers set it this way */
158 range->pmp_flags = IW_POWER_PERIOD;
159 range->pmt_flags = IW_POWER_TIMEOUT;
160 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
163 range->max_pmp = 65535000;
165 range->max_pmt = 65535 * 1000;
168 if (wl_ioctl(dev, WLC_GET_RTS, &range->max_rts, sizeof(int)) < 0)
169 range->max_rts = 2347;
171 range->min_frag = 256;
173 if (wl_ioctl(dev, WLC_GET_FRAG, &range->max_frag, sizeof(int)) < 0)
174 range->max_frag = 2346;
176 range->txpower_capa = IW_TXPOW_MWATT;
182 static int wlcompat_set_scan(struct net_device *dev,
183 struct iw_request_info *info,
184 union iwreq_data *wrqu,
187 int ap = 0, oldap = 0;
188 wl_scan_params_t params;
190 memset(¶ms, 0, sizeof(params));
192 /* use defaults (same parameters as wl scan) */
193 memset(¶ms.bssid, 0xff, sizeof(params.bssid));
194 params.bss_type = DOT11_BSSTYPE_ANY;
195 params.scan_type = -1;
197 params.active_time = -1;
198 params.passive_time = -1;
199 params.home_time = -1;
201 /* can only scan in STA mode */
202 wl_ioctl(dev, WLC_GET_AP, &oldap, sizeof(oldap));
204 wl_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap));
206 if (wl_ioctl(dev, WLC_SCAN, ¶ms, 64) < 0)
210 wl_ioctl(dev, WLC_SET_AP, &oldap, sizeof(oldap));
216 static int wlcompat_get_scan(struct net_device *dev,
217 struct iw_request_info *info,
218 union iwreq_data *wrqu,
221 wl_scan_results_t *results = (wl_scan_results_t *) buf;
222 wl_bss_info_t *bss_info;
224 char *current_ev = extra;
226 char *end_buf = extra + IW_SCAN_MAX_DATA;
230 if (wl_ioctl(dev, WLC_SCAN_RESULTS, buf, WLC_IOCTL_MAXLEN) < 0)
233 bss_info = &(results->bss_info[0]);
234 info_ptr = (char *) bss_info;
235 for (i = 0; i < results->count; i++) {
237 /* send the cell address (must be sent first) */
239 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
240 memcpy(&iwe.u.ap_addr.sa_data, &bss_info->BSSID, sizeof(bss_info->BSSID));
241 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_ADDR_LEN);
244 iwe.cmd = SIOCGIWESSID;
245 iwe.u.data.length = bss_info->SSID_len;
246 if (iwe.u.data.length > IW_ESSID_MAX_SIZE)
247 iwe.u.data.length = IW_ESSID_MAX_SIZE;
248 iwe.u.data.flags = 1;
249 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, bss_info->SSID);
251 /* send frequency/channel info */
252 iwe.cmd = SIOCGIWFREQ;
254 iwe.u.freq.m = bss_info->channel;
255 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_FREQ_LEN);
257 /* add quality statistics */
259 iwe.u.qual.level = bss_info->RSSI;
260 iwe.u.qual.noise = bss_info->phy_noise;
262 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);
264 /* send rate information */
265 iwe.cmd = SIOCGIWRATE;
266 current_val = current_ev + IW_EV_LCP_LEN;
267 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
269 for(j = 0 ; j < bss_info->rateset.count ; j++) {
270 iwe.u.bitrate.value = ((bss_info->rateset.rates[j] & 0x7f) * 500000);
271 current_val = iwe_stream_add_value(current_ev, current_val, end_buf, &iwe, IW_EV_PARAM_LEN);
273 if((current_val - current_ev) > IW_EV_LCP_LEN)
274 current_ev = current_val;
276 info_ptr += sizeof(wl_bss_info_t);
277 if (bss_info->ie_length % 4)
278 info_ptr += bss_info->ie_length + 4 - (bss_info->ie_length % 4);
280 info_ptr += bss_info->ie_length;
281 bss_info = (wl_bss_info_t *) info_ptr;
284 wrqu->data.length = (current_ev - extra);
285 wrqu->data.flags = 0;
290 static int wlcompat_ioctl(struct net_device *dev,
291 struct iw_request_info *info,
292 union iwreq_data *wrqu,
297 strcpy(wrqu->name, "IEEE 802.11-DS");
303 if (wl_ioctl(dev,WLC_GET_CHANNEL, &ci, sizeof(ci)) < 0)
306 wrqu->freq.m = ci.target_channel;
312 if (wrqu->freq.e == 1) {
314 int f = wrqu->freq.m / 100000;
315 while ((channel < NUM_CHANNELS + 1) && (f != channel_frequency[channel]))
318 if (channel == NUM_CHANNELS) // channel not found
322 wrqu->freq.m = channel + 1;
324 if ((wrqu->freq.e == 0) && (wrqu->freq.m < 1000)) {
325 if (wl_ioctl(dev, WLC_SET_CHANNEL, &wrqu->freq.m, sizeof(int)) < 0)
336 if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
339 if (wl_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap)) < 0)
342 if (wl_ioctl(dev, (ap ? WLC_SET_BSSID : WLC_REASSOC), wrqu->ap_addr.sa_data, 6) < 0)
349 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
350 if (wl_ioctl(dev,WLC_GET_BSSID,wrqu->ap_addr.sa_data,6) < 0)
358 if (wl_ioctl(dev,WLC_GET_SSID, &ssid, sizeof(wlc_ssid_t)) < 0)
361 wrqu->essid.flags = wrqu->data.flags = 1;
362 wrqu->essid.length = wrqu->data.length = ssid.SSID_len + 1;
363 memcpy(extra,ssid.SSID,ssid.SSID_len + 1);
369 memset(&ssid, 0, sizeof(ssid));
370 ssid.SSID_len = strlen(extra);
371 if (ssid.SSID_len > WLC_ESSID_MAX_SIZE)
372 ssid.SSID_len = WLC_ESSID_MAX_SIZE;
373 memcpy(ssid.SSID, extra, ssid.SSID_len);
374 if (wl_ioctl(dev, WLC_SET_SSID, &ssid, sizeof(ssid)) < 0)
380 if (wl_ioctl(dev,WLC_GET_RTS,&(wrqu->rts.value),sizeof(int)) < 0)
386 if (wl_ioctl(dev,WLC_SET_RTS,&(wrqu->rts.value),sizeof(int)) < 0)
392 if (wl_ioctl(dev,WLC_GET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
398 if (wl_ioctl(dev,WLC_SET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
406 if (wl_ioctl(dev, WLC_GET_RADIO, &radio, sizeof(int)) < 0)
409 if (wl_get_val(dev, "qtxpower", &(wrqu->txpower.value), sizeof(int)) < 0)
412 wrqu->txpower.value &= ~WL_TXPWR_OVERRIDE;
414 wrqu->txpower.fixed = 0;
415 wrqu->txpower.disabled = radio;
416 wrqu->txpower.flags = IW_TXPOW_MWATT;
421 /* This is weird: WLC_SET_RADIO with 1 as argument disables the radio */
422 int radio = wrqu->txpower.disabled;
424 if (wl_ioctl(dev, WLC_SET_RADIO, &radio, sizeof(int)) < 0)
427 if (!wrqu->txpower.disabled) {
430 if (wl_get_val(dev, "qtxpower", &override, sizeof(int)) < 0)
433 wrqu->txpower.value |= override & WL_TXPWR_OVERRIDE;
435 if (wrqu->txpower.flags != IW_TXPOW_MWATT)
438 if (wl_set_val(dev, "qtxpower", &wrqu->txpower.value, sizeof(int)) < 0)
445 int wep = 1, wrestrict = 1;
446 int index = (wrqu->data.flags & IW_ENCODE_INDEX) - 1;
449 index = get_primary_key(dev);
451 if (wrqu->data.flags & IW_ENCODE_DISABLED)
454 if (wrqu->data.flags & IW_ENCODE_OPEN)
457 if (wrqu->data.pointer && (wrqu->data.length > 0) && (wrqu->data.length <= 16)) {
459 memset(&key, 0, sizeof(key));
461 key.flags = WL_PRIMARY_KEY;
462 key.len = wrqu->data.length;
464 memcpy(key.data, wrqu->data.pointer, wrqu->data.length);
466 if (wl_ioctl(dev, WLC_SET_KEY, &key, sizeof(key)) < 0)
471 if (wl_ioctl(dev, WLC_SET_KEY_PRIMARY, &index, sizeof(index)) < 0)
474 if (wl_ioctl(dev, WLC_SET_WSEC, &wep, sizeof(wep)) < 0)
478 if (wl_ioctl(dev, WLC_SET_WEP_RESTRICT, &wrestrict, sizeof(wrestrict)) < 0)
485 if (wl_ioctl(dev, WLC_GET_WEP, &val, sizeof(val)) < 0)
490 int key = get_primary_key(dev);
492 wrqu->data.flags = IW_ENCODE_ENABLED;
497 info_addr = (int *) dev->priv;
498 wep_key = (wkey *) ((*info_addr) + 0x2752 + (key * 0x110));
500 wrqu->data.flags |= key + 1;
501 wrqu->data.length = wep_key->len;
503 memset(extra, 0, 16);
504 memcpy(extra, wep_key->data, 16);
506 wrqu->data.flags |= IW_ENCODE_NOKEY;
509 wrqu->data.flags = IW_ENCODE_DISABLED;
516 return wlcompat_ioctl_getiwrange(dev, extra);
521 int ap = -1, infra = -1, passive = 0, wet = 0;
523 switch (wrqu->mode) {
524 case IW_MODE_MONITOR:
549 wl_ioctl(dev, WLC_SET_PASSIVE, &passive, sizeof(passive));
550 wl_ioctl(dev, WLC_SET_MONITOR, &passive, sizeof(passive));
551 wl_ioctl(dev, WLC_SET_WET, &wet, sizeof(wet));
553 wl_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap));
555 wl_ioctl(dev, WLC_SET_INFRA, &infra, sizeof(infra));
562 int ap, infra, wet, passive;
564 if (wl_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap)) < 0)
566 if (wl_ioctl(dev, WLC_GET_INFRA, &infra, sizeof(infra)) < 0)
568 if (wl_ioctl(dev, WLC_GET_PASSIVE, &passive, sizeof(passive)) < 0)
570 if (wl_ioctl(dev, WLC_GET_WET, &wet, sizeof(wet)) < 0)
574 wrqu->mode = IW_MODE_MONITOR;
576 wrqu->mode = IW_MODE_ADHOC;
579 wrqu->mode = IW_MODE_MASTER;
582 wrqu->mode = IW_MODE_REPEAT;
584 wrqu->mode = IW_MODE_INFRA;
592 if (info->cmd >= SIOCIWFIRSTPRIV)
593 return wlcompat_private_ioctl(dev, info, wrqu, extra);
602 static const iw_handler wlcompat_handler[] = {
603 NULL, /* SIOCSIWCOMMIT */
604 wlcompat_ioctl, /* SIOCGIWNAME */
605 NULL, /* SIOCSIWNWID */
606 NULL, /* SIOCGIWNWID */
607 wlcompat_ioctl, /* SIOCSIWFREQ */
608 wlcompat_ioctl, /* SIOCGIWFREQ */
609 wlcompat_ioctl, /* SIOCSIWMODE */
610 wlcompat_ioctl, /* SIOCGIWMODE */
611 NULL, /* SIOCSIWSENS */
612 NULL, /* SIOCGIWSENS */
613 NULL, /* SIOCSIWRANGE, unused */
614 wlcompat_ioctl, /* SIOCGIWRANGE */
615 NULL, /* SIOCSIWPRIV */
616 NULL, /* SIOCGIWPRIV */
617 NULL, /* SIOCSIWSTATS */
618 NULL, /* SIOCGIWSTATS */
619 iw_handler_set_spy, /* SIOCSIWSPY */
620 iw_handler_get_spy, /* SIOCGIWSPY */
621 iw_handler_set_thrspy, /* SIOCSIWTHRSPY */
622 iw_handler_get_thrspy, /* SIOCGIWTHRSPY */
623 wlcompat_ioctl, /* SIOCSIWAP */
624 wlcompat_ioctl, /* SIOCGIWAP */
625 NULL, /* -- hole -- */
626 NULL, /* SIOCGIWAPLIST */
627 wlcompat_set_scan, /* SIOCSIWSCAN */
628 wlcompat_get_scan, /* SIOCGIWSCAN */
629 wlcompat_ioctl, /* SIOCSIWESSID */
630 wlcompat_ioctl, /* SIOCGIWESSID */
631 NULL, /* SIOCSIWNICKN */
632 NULL, /* SIOCGIWNICKN */
633 NULL, /* -- hole -- */
634 NULL, /* -- hole -- */
635 NULL, /* SIOCSIWRATE */
636 NULL, /* SIOCGIWRATE */
637 wlcompat_ioctl, /* SIOCSIWRTS */
638 wlcompat_ioctl, /* SIOCGIWRTS */
639 wlcompat_ioctl, /* SIOCSIWFRAG */
640 wlcompat_ioctl, /* SIOCGIWFRAG */
641 wlcompat_ioctl, /* SIOCSIWTXPOW */
642 wlcompat_ioctl, /* SIOCGIWTXPOW */
643 NULL, /* SIOCSIWRETRY */
644 NULL, /* SIOCGIWRETRY */
645 wlcompat_ioctl, /* SIOCSIWENCODE */
646 wlcompat_ioctl, /* SIOCGIWENCODE */
649 static int wlcompat_private_ioctl(struct net_device *dev,
650 struct iw_request_info *info,
651 union iwreq_data *wrqu,
654 int *value = (int *) wrqu->name;
657 case WLCOMPAT_SET_MONITOR:
659 if (wl_ioctl(dev, WLC_SET_MONITOR, value, sizeof(int)) < 0)
664 case WLCOMPAT_GET_MONITOR:
666 if (wl_ioctl(dev, WLC_GET_MONITOR, extra, sizeof(int)) < 0)
671 case WLCOMPAT_SET_TXPWR_LIMIT:
676 if (wl_get_val(dev, "qtxpower", &val, sizeof(int)) < 0)
680 val |= WL_TXPWR_OVERRIDE;
682 val &= ~WL_TXPWR_OVERRIDE;
684 if (wl_set_val(dev, "qtxpower", &val, sizeof(int)) < 0)
689 case WLCOMPAT_GET_TXPWR_LIMIT:
691 if (wl_get_val(dev, "qtxpower", value, sizeof(int)) < 0)
694 *value = ((*value & WL_TXPWR_OVERRIDE) == WL_TXPWR_OVERRIDE ? 1 : 0);
698 case WLCOMPAT_SET_ANTDIV:
700 if (wl_ioctl(dev, WLC_SET_ANTDIV, value, sizeof(int)) < 0)
705 case WLCOMPAT_GET_ANTDIV:
707 if (wl_ioctl(dev, WLC_GET_ANTDIV, extra, sizeof(int)) < 0)
712 case WLCOMPAT_SET_TXANT:
714 if (wl_ioctl(dev, WLC_SET_TXANT, value, sizeof(int)) < 0)
719 case WLCOMPAT_GET_TXANT:
721 if (wl_ioctl(dev, WLC_GET_TXANT, extra, sizeof(int)) < 0)
735 static const struct iw_priv_args wlcompat_private_args[] =
737 { WLCOMPAT_SET_MONITOR,
738 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
742 { WLCOMPAT_GET_MONITOR,
744 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
747 { WLCOMPAT_SET_TXPWR_LIMIT,
748 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
752 { WLCOMPAT_GET_TXPWR_LIMIT,
754 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
757 { WLCOMPAT_SET_ANTDIV,
758 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
762 { WLCOMPAT_GET_ANTDIV,
764 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
767 { WLCOMPAT_SET_TXANT,
768 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
772 { WLCOMPAT_GET_TXANT,
774 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
779 static const iw_handler wlcompat_private[] =
781 wlcompat_private_ioctl,
786 static const struct iw_handler_def wlcompat_handler_def =
788 .standard = (iw_handler *) wlcompat_handler,
789 .num_standard = sizeof(wlcompat_handler)/sizeof(iw_handler),
790 .private = wlcompat_private,
792 .private_args = wlcompat_private_args,
793 .num_private_args = sizeof(wlcompat_private_args) / sizeof(wlcompat_private_args[0])
798 void print_buffer(int len, unsigned char *buf) {
801 for (x=0;x<len && x<180 ;x++) {
804 printk("%02X",buf[x]);
813 static int (*old_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
814 static int new_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) {
816 struct iwreq *iwr = (struct iwreq *) ifr;
817 struct iw_request_info info;
820 printk("dev: %s ioctl: 0x%04x\n",dev->name,cmd);
823 if (cmd >= SIOCIWFIRSTPRIV) {
826 ret = wlcompat_private_ioctl(dev, &info, &(iwr->u), (char *) &(iwr->u));
828 } else if (cmd==SIOCDEVPRIVATE) {
829 wl_ioctl_t *ioc = (wl_ioctl_t *)ifr->ifr_data;
830 unsigned char *buf = ioc->buf;
831 printk(" cmd: %d buf: 0x%08x len: %d\n",ioc->cmd,&(ioc->buf),ioc->len);
833 print_buffer(ioc->len, buf);
834 ret = old_ioctl(dev,ifr,cmd);
836 print_buffer(ioc->len, buf);
837 printk(" ret: %d\n", ret);
840 ret = old_ioctl(dev,ifr,cmd);
845 static int __init wlcompat_init()
848 char *devname = "eth0";
850 while (!found && (dev = dev_get_by_name(devname))) {
851 if ((dev->wireless_handlers == NULL) && ((wl_ioctl(dev, WLC_GET_MAGIC, &i, sizeof(i)) == 0) && i == WLC_IOCTL_MAGIC))
857 printk("No Broadcom devices found.\n");
862 old_ioctl = dev->do_ioctl;
863 dev->do_ioctl = new_ioctl;
864 dev->wireless_handlers = (struct iw_handler_def *)&wlcompat_handler_def;
866 printk("broadcom driver private data: 0x%08x\n", dev->priv);
871 static void __exit wlcompat_exit()
873 dev->wireless_handlers = NULL;
874 dev->do_ioctl = old_ioctl;
879 MODULE_AUTHOR("openwrt.org");
880 MODULE_LICENSE("GPL");
882 module_init(wlcompat_init);
883 module_exit(wlcompat_exit);