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)
88 /* check for overflow */
89 if ((buf_len = strlen(var)) + 1 + len > sizeof(buf))
95 /* append int value onto the end of the name string */
96 memcpy(&(buf[buf_len]), val, len);
99 ret = wl_ioctl(dev, WLC_SET_VAR, buf, buf_len);
103 static int wl_get_val(struct net_device *dev, char *var, void *val, int len)
109 /* check for overflow */
110 if ((buf_len = strlen(var)) + 1 > sizeof(buf) || len > sizeof(buf))
114 if (ret = wl_ioctl(dev, WLC_GET_VAR, buf, buf_len + len))
117 memcpy(val, buf, len);
121 int get_primary_key(struct net_device *dev)
125 for (key = val = 0; (key < 4) && (val == 0); key++) {
127 if (wl_ioctl(dev, WLC_GET_KEY_PRIMARY, &val, sizeof(val)) < 0)
134 static int wlcompat_ioctl_getiwrange(struct net_device *dev,
138 struct iw_range *range;
140 range = (struct iw_range *) extra;
142 range->we_version_compiled = WIRELESS_EXT;
143 range->we_version_source = WIRELESS_EXT;
145 range->min_nwid = range->max_nwid = 0;
147 range->num_channels = NUM_CHANNELS;
149 for (i = 0; i < NUM_CHANNELS; i++) {
150 range->freq[k].i = i + 1;
151 range->freq[k].m = channel_frequency[i] * 100000;
152 range->freq[k].e = 1;
154 if (k >= IW_MAX_FREQUENCIES)
157 range->num_frequency = k;
158 range->sensitivity = 3;
160 /* nbd: don't know what this means, but other drivers set it this way */
161 range->pmp_flags = IW_POWER_PERIOD;
162 range->pmt_flags = IW_POWER_TIMEOUT;
163 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
166 range->max_pmp = 65535000;
168 range->max_pmt = 65535 * 1000;
171 if (wl_ioctl(dev, WLC_GET_RTS, &range->max_rts, sizeof(int)) < 0)
172 range->max_rts = 2347;
174 range->min_frag = 256;
176 if (wl_ioctl(dev, WLC_GET_FRAG, &range->max_frag, sizeof(int)) < 0)
177 range->max_frag = 2346;
179 range->txpower_capa = IW_TXPOW_DBM;
185 static int wlcompat_set_scan(struct net_device *dev,
186 struct iw_request_info *info,
187 union iwreq_data *wrqu,
190 int ap = 0, oldap = 0;
191 wl_scan_params_t params;
193 memset(¶ms, 0, sizeof(params));
195 /* use defaults (same parameters as wl scan) */
196 memset(¶ms.bssid, 0xff, sizeof(params.bssid));
197 params.bss_type = DOT11_BSSTYPE_ANY;
198 params.scan_type = -1;
200 params.active_time = -1;
201 params.passive_time = -1;
202 params.home_time = -1;
204 /* can only scan in STA mode */
205 wl_ioctl(dev, WLC_GET_AP, &oldap, sizeof(oldap));
207 wl_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap));
209 if (wl_ioctl(dev, WLC_SCAN, ¶ms, 64) < 0)
213 wl_ioctl(dev, WLC_SET_AP, &oldap, sizeof(oldap));
219 static int wlcompat_get_scan(struct net_device *dev,
220 struct iw_request_info *info,
221 union iwreq_data *wrqu,
224 wl_scan_results_t *results = (wl_scan_results_t *) buf;
225 wl_bss_info_t *bss_info;
227 char *current_ev = extra;
229 char *end_buf = extra + IW_SCAN_MAX_DATA;
233 if (wl_ioctl(dev, WLC_SCAN_RESULTS, buf, WLC_IOCTL_MAXLEN) < 0)
236 bss_info = &(results->bss_info[0]);
237 info_ptr = (char *) bss_info;
238 for (i = 0; i < results->count; i++) {
240 /* send the cell address (must be sent first) */
242 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
243 memcpy(&iwe.u.ap_addr.sa_data, &bss_info->BSSID, sizeof(bss_info->BSSID));
244 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_ADDR_LEN);
247 iwe.cmd = SIOCGIWESSID;
248 iwe.u.data.length = bss_info->SSID_len;
249 if (iwe.u.data.length > IW_ESSID_MAX_SIZE)
250 iwe.u.data.length = IW_ESSID_MAX_SIZE;
251 iwe.u.data.flags = 1;
252 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, bss_info->SSID);
254 /* send frequency/channel info */
255 iwe.cmd = SIOCGIWFREQ;
257 iwe.u.freq.m = bss_info->channel;
258 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_FREQ_LEN);
260 /* add quality statistics */
262 iwe.u.qual.level = bss_info->RSSI;
263 iwe.u.qual.noise = bss_info->phy_noise;
265 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);
267 /* send rate information */
268 iwe.cmd = SIOCGIWRATE;
269 current_val = current_ev + IW_EV_LCP_LEN;
270 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
272 for(j = 0 ; j < bss_info->rateset.count ; j++) {
273 iwe.u.bitrate.value = ((bss_info->rateset.rates[j] & 0x7f) * 500000);
274 current_val = iwe_stream_add_value(current_ev, current_val, end_buf, &iwe, IW_EV_PARAM_LEN);
276 if((current_val - current_ev) > IW_EV_LCP_LEN)
277 current_ev = current_val;
279 info_ptr += sizeof(wl_bss_info_t);
280 if (bss_info->ie_length % 4)
281 info_ptr += bss_info->ie_length + 4 - (bss_info->ie_length % 4);
283 info_ptr += bss_info->ie_length;
284 bss_info = (wl_bss_info_t *) info_ptr;
287 wrqu->data.length = (current_ev - extra);
288 wrqu->data.flags = 0;
293 static int wlcompat_ioctl(struct net_device *dev,
294 struct iw_request_info *info,
295 union iwreq_data *wrqu,
300 strcpy(wrqu->name, "IEEE 802.11-DS");
306 if (wl_ioctl(dev,WLC_GET_CHANNEL, &ci, sizeof(ci)) < 0)
309 wrqu->freq.m = ci.target_channel;
315 if (wrqu->freq.m == -1) {
317 if (wl_ioctl(dev, WLC_SET_CHANNEL, &wrqu->freq.m, sizeof(int)) < 0)
320 if (wrqu->freq.e == 1) {
322 int f = wrqu->freq.m / 100000;
323 while ((channel < NUM_CHANNELS + 1) && (f != channel_frequency[channel]))
326 if (channel == NUM_CHANNELS) // channel not found
330 wrqu->freq.m = channel + 1;
332 if ((wrqu->freq.e == 0) && (wrqu->freq.m < 1000)) {
333 if (wl_ioctl(dev, WLC_SET_CHANNEL, &wrqu->freq.m, sizeof(int)) < 0)
345 if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
348 if (wl_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap)) < 0)
351 if (wl_ioctl(dev, (ap ? WLC_SET_BSSID : WLC_REASSOC), wrqu->ap_addr.sa_data, 6) < 0)
358 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
359 if (wl_ioctl(dev,WLC_GET_BSSID,wrqu->ap_addr.sa_data,6) < 0)
367 if (wl_ioctl(dev,WLC_GET_SSID, &ssid, sizeof(wlc_ssid_t)) < 0)
370 wrqu->essid.flags = wrqu->data.flags = 1;
371 wrqu->essid.length = wrqu->data.length = ssid.SSID_len + 1;
372 memcpy(extra,ssid.SSID,ssid.SSID_len + 1);
378 memset(&ssid, 0, sizeof(ssid));
379 ssid.SSID_len = strlen(extra);
380 if (ssid.SSID_len > WLC_ESSID_MAX_SIZE)
381 ssid.SSID_len = WLC_ESSID_MAX_SIZE;
382 memcpy(ssid.SSID, extra, ssid.SSID_len);
383 if (wl_ioctl(dev, WLC_SET_SSID, &ssid, sizeof(ssid)) < 0)
389 if (wl_ioctl(dev,WLC_GET_RTS,&(wrqu->rts.value),sizeof(int)) < 0)
395 if (wl_ioctl(dev,WLC_SET_RTS,&(wrqu->rts.value),sizeof(int)) < 0)
401 if (wl_ioctl(dev,WLC_GET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
407 if (wl_ioctl(dev,WLC_SET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
415 wl_ioctl(dev, WLC_GET_RADIO, &radio, sizeof(int));
417 if (wl_get_val(dev, "qtxpower", &(wrqu->txpower.value), sizeof(int)) < 0)
420 wrqu->txpower.value &= ~WL_TXPWR_OVERRIDE;
421 wrqu->txpower.value /= 4;
423 wrqu->txpower.fixed = 0;
424 wrqu->txpower.disabled = radio;
425 wrqu->txpower.flags = IW_TXPOW_DBM;
430 /* This is weird: WLC_SET_RADIO with 1 as argument disables the radio */
431 int radio = wrqu->txpower.disabled;
433 wl_ioctl(dev, WLC_SET_RADIO, &radio, sizeof(int));
435 if (!wrqu->txpower.disabled && (wrqu->txpower.value > 0)) {
438 if (wl_get_val(dev, "qtxpower", &value, sizeof(int)) < 0)
441 value &= WL_TXPWR_OVERRIDE;
442 wrqu->txpower.value *= 4;
443 wrqu->txpower.value |= value;
445 if (wrqu->txpower.flags != IW_TXPOW_DBM)
448 if (wrqu->txpower.value > 0)
449 if (wl_set_val(dev, "qtxpower", &(wrqu->txpower.value), sizeof(int)) < 0)
456 int val = 0, wep = 1, wrestrict = 1;
457 int index = (wrqu->data.flags & IW_ENCODE_INDEX) - 1;
460 index = get_primary_key(dev);
462 if (wrqu->data.flags & IW_ENCODE_DISABLED) {
464 if (wl_ioctl(dev, WLC_SET_WSEC, &wep, sizeof(val)) < 0)
469 if (wl_ioctl(dev, WLC_SET_WSEC, &wep, sizeof(val)) < 0)
472 if (wrqu->data.flags & IW_ENCODE_OPEN)
475 if (wrqu->data.pointer && (wrqu->data.length > 0) && (wrqu->data.length <= 16)) {
477 memset(&key, 0, sizeof(key));
479 key.flags = WL_PRIMARY_KEY;
480 key.len = wrqu->data.length;
482 memcpy(key.data, wrqu->data.pointer, wrqu->data.length);
484 if (wl_ioctl(dev, WLC_SET_KEY, &key, sizeof(key)) < 0)
489 wl_ioctl(dev, WLC_SET_KEY_PRIMARY, &index, sizeof(index));
492 wl_ioctl(dev, WLC_SET_WEP_RESTRICT, &wrestrict, sizeof(wrestrict));
500 if (wl_ioctl(dev, WLC_GET_WEP, &val, sizeof(val)) < 0)
505 int key = get_primary_key(dev);
507 wrqu->data.flags = IW_ENCODE_ENABLED;
512 info_addr = (int *) dev->priv;
513 wep_key = (wkey *) ((*info_addr) + 0x2752 + (key * 0x110));
515 wrqu->data.flags |= key + 1;
516 wrqu->data.length = wep_key->len;
518 memset(extra, 0, 16);
519 memcpy(extra, wep_key->data, 16);
521 wrqu->data.flags |= IW_ENCODE_NOKEY;
524 wrqu->data.flags = IW_ENCODE_DISABLED;
531 return wlcompat_ioctl_getiwrange(dev, extra);
536 int ap = -1, infra = -1, passive = 0, wet = 0;
538 switch (wrqu->mode) {
539 case IW_MODE_MONITOR:
564 wl_ioctl(dev, WLC_SET_PASSIVE, &passive, sizeof(passive));
565 wl_ioctl(dev, WLC_SET_MONITOR, &passive, sizeof(passive));
566 wl_ioctl(dev, WLC_SET_WET, &wet, sizeof(wet));
568 wl_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap));
570 wl_ioctl(dev, WLC_SET_INFRA, &infra, sizeof(infra));
577 int ap, infra, wet, passive;
579 if (wl_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap)) < 0)
581 if (wl_ioctl(dev, WLC_GET_INFRA, &infra, sizeof(infra)) < 0)
583 if (wl_ioctl(dev, WLC_GET_PASSIVE, &passive, sizeof(passive)) < 0)
585 if (wl_ioctl(dev, WLC_GET_WET, &wet, sizeof(wet)) < 0)
589 wrqu->mode = IW_MODE_MONITOR;
591 wrqu->mode = IW_MODE_ADHOC;
594 wrqu->mode = IW_MODE_MASTER;
597 wrqu->mode = IW_MODE_REPEAT;
599 wrqu->mode = IW_MODE_INFRA;
607 if (info->cmd >= SIOCIWFIRSTPRIV)
608 return wlcompat_private_ioctl(dev, info, wrqu, extra);
617 static const iw_handler wlcompat_handler[] = {
618 NULL, /* SIOCSIWCOMMIT */
619 wlcompat_ioctl, /* SIOCGIWNAME */
620 NULL, /* SIOCSIWNWID */
621 NULL, /* SIOCGIWNWID */
622 wlcompat_ioctl, /* SIOCSIWFREQ */
623 wlcompat_ioctl, /* SIOCGIWFREQ */
624 wlcompat_ioctl, /* SIOCSIWMODE */
625 wlcompat_ioctl, /* SIOCGIWMODE */
626 NULL, /* SIOCSIWSENS */
627 NULL, /* SIOCGIWSENS */
628 NULL, /* SIOCSIWRANGE, unused */
629 wlcompat_ioctl, /* SIOCGIWRANGE */
630 NULL, /* SIOCSIWPRIV */
631 NULL, /* SIOCGIWPRIV */
632 NULL, /* SIOCSIWSTATS */
633 NULL, /* SIOCGIWSTATS */
634 iw_handler_set_spy, /* SIOCSIWSPY */
635 iw_handler_get_spy, /* SIOCGIWSPY */
636 iw_handler_set_thrspy, /* SIOCSIWTHRSPY */
637 iw_handler_get_thrspy, /* SIOCGIWTHRSPY */
638 wlcompat_ioctl, /* SIOCSIWAP */
639 wlcompat_ioctl, /* SIOCGIWAP */
640 NULL, /* -- hole -- */
641 NULL, /* SIOCGIWAPLIST */
642 wlcompat_set_scan, /* SIOCSIWSCAN */
643 wlcompat_get_scan, /* SIOCGIWSCAN */
644 wlcompat_ioctl, /* SIOCSIWESSID */
645 wlcompat_ioctl, /* SIOCGIWESSID */
646 NULL, /* SIOCSIWNICKN */
647 NULL, /* SIOCGIWNICKN */
648 NULL, /* -- hole -- */
649 NULL, /* -- hole -- */
650 NULL, /* SIOCSIWRATE */
651 NULL, /* SIOCGIWRATE */
652 wlcompat_ioctl, /* SIOCSIWRTS */
653 wlcompat_ioctl, /* SIOCGIWRTS */
654 wlcompat_ioctl, /* SIOCSIWFRAG */
655 wlcompat_ioctl, /* SIOCGIWFRAG */
656 wlcompat_ioctl, /* SIOCSIWTXPOW */
657 wlcompat_ioctl, /* SIOCGIWTXPOW */
658 NULL, /* SIOCSIWRETRY */
659 NULL, /* SIOCGIWRETRY */
660 wlcompat_ioctl, /* SIOCSIWENCODE */
661 wlcompat_ioctl, /* SIOCGIWENCODE */
664 static int wlcompat_private_ioctl(struct net_device *dev,
665 struct iw_request_info *info,
666 union iwreq_data *wrqu,
669 int *value = (int *) wrqu->name;
672 case WLCOMPAT_SET_MONITOR:
674 if (wl_ioctl(dev, WLC_SET_MONITOR, value, sizeof(int)) < 0)
679 case WLCOMPAT_GET_MONITOR:
681 if (wl_ioctl(dev, WLC_GET_MONITOR, extra, sizeof(int)) < 0)
686 case WLCOMPAT_SET_TXPWR_LIMIT:
691 if (wl_get_val(dev, "qtxpower", &val, sizeof(int)) < 0)
695 val |= WL_TXPWR_OVERRIDE;
697 val &= ~WL_TXPWR_OVERRIDE;
699 if (wl_set_val(dev, "qtxpower", &val, sizeof(int)) < 0)
704 case WLCOMPAT_GET_TXPWR_LIMIT:
706 if (wl_get_val(dev, "qtxpower", value, sizeof(int)) < 0)
709 *value = ((*value & WL_TXPWR_OVERRIDE) == WL_TXPWR_OVERRIDE ? 1 : 0);
713 case WLCOMPAT_SET_ANTDIV:
715 if (wl_ioctl(dev, WLC_SET_ANTDIV, value, sizeof(int)) < 0)
720 case WLCOMPAT_GET_ANTDIV:
722 if (wl_ioctl(dev, WLC_GET_ANTDIV, extra, sizeof(int)) < 0)
727 case WLCOMPAT_SET_TXANT:
729 if (wl_ioctl(dev, WLC_SET_TXANT, value, sizeof(int)) < 0)
734 case WLCOMPAT_GET_TXANT:
736 if (wl_ioctl(dev, WLC_GET_TXANT, extra, sizeof(int)) < 0)
750 static const struct iw_priv_args wlcompat_private_args[] =
752 { WLCOMPAT_SET_MONITOR,
753 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
757 { WLCOMPAT_GET_MONITOR,
759 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
762 { WLCOMPAT_SET_TXPWR_LIMIT,
763 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
767 { WLCOMPAT_GET_TXPWR_LIMIT,
769 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
772 { WLCOMPAT_SET_ANTDIV,
773 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
777 { WLCOMPAT_GET_ANTDIV,
779 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
782 { WLCOMPAT_SET_TXANT,
783 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
787 { WLCOMPAT_GET_TXANT,
789 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
794 static const iw_handler wlcompat_private[] =
796 wlcompat_private_ioctl,
801 static const struct iw_handler_def wlcompat_handler_def =
803 .standard = (iw_handler *) wlcompat_handler,
804 .num_standard = sizeof(wlcompat_handler)/sizeof(iw_handler),
805 .private = wlcompat_private,
807 .private_args = wlcompat_private_args,
808 .num_private_args = sizeof(wlcompat_private_args) / sizeof(wlcompat_private_args[0])
813 void print_buffer(int len, unsigned char *buf) {
816 for (x=0;x<len && x<180 ;x++) {
819 printk("%02X",buf[x]);
828 static int (*old_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
829 static int new_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) {
831 struct iwreq *iwr = (struct iwreq *) ifr;
832 struct iw_request_info info;
835 printk("dev: %s ioctl: 0x%04x\n",dev->name,cmd);
838 if (cmd >= SIOCIWFIRSTPRIV) {
841 ret = wlcompat_private_ioctl(dev, &info, &(iwr->u), (char *) &(iwr->u));
843 } else if (cmd==SIOCDEVPRIVATE) {
844 wl_ioctl_t *ioc = (wl_ioctl_t *)ifr->ifr_data;
845 unsigned char *buf = ioc->buf;
846 printk(" cmd: %d buf: 0x%08x len: %d\n",ioc->cmd,&(ioc->buf),ioc->len);
848 print_buffer(ioc->len, buf);
849 ret = old_ioctl(dev,ifr,cmd);
851 print_buffer(ioc->len, buf);
852 printk(" ret: %d\n", ret);
855 ret = old_ioctl(dev,ifr,cmd);
860 static int __init wlcompat_init()
863 char *devname = "eth0";
865 while (!found && (dev = dev_get_by_name(devname))) {
866 if ((dev->wireless_handlers == NULL) && ((wl_ioctl(dev, WLC_GET_MAGIC, &i, sizeof(i)) == 0) && i == WLC_IOCTL_MAGIC))
872 printk("No Broadcom devices found.\n");
877 old_ioctl = dev->do_ioctl;
878 dev->do_ioctl = new_ioctl;
879 dev->wireless_handlers = (struct iw_handler_def *)&wlcompat_handler_def;
881 printk("broadcom driver private data: 0x%08x\n", dev->priv);
886 static void __exit wlcompat_exit()
888 dev->wireless_handlers = NULL;
889 dev->do_ioctl = old_ioctl;
894 MODULE_AUTHOR("openwrt.org");
895 MODULE_LICENSE("GPL");
897 module_init(wlcompat_init);
898 module_exit(wlcompat_exit);