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 static int wlcompat_private_ioctl(struct net_device *dev,
47 struct iw_request_info *info,
48 union iwreq_data *wrqu,
51 static int wl_ioctl(struct net_device *dev, int cmd, void *buf, int len)
53 mm_segment_t old_fs = get_fs();
60 strncpy(ifr.ifr_name, dev->name, IFNAMSIZ);
61 ifr.ifr_data = (caddr_t) &ioc;
63 ret = dev->do_ioctl(dev,&ifr,SIOCDEVPRIVATE);
68 static int wl_set_val(struct net_device *dev, char *var, void *val, int len)
73 /* check for overflow */
74 if ((buf_len = strlen(var)) + 1 + len > sizeof(buf))
80 /* append int value onto the end of the name string */
81 memcpy(&buf[buf_len], val, len);
84 return wl_ioctl(dev, WLC_SET_VAR, buf, buf_len);
87 static int wl_get_val(struct net_device *dev, char *var, void *val, int len)
92 /* check for overflow */
93 if (strlen(var) + 1 > sizeof(buf) || len > sizeof(buf))
97 if ((ret = wl_ioctl(dev, WLC_GET_VAR, buf, sizeof(buf))))
100 memcpy(val, buf, len);
105 static int wlcompat_ioctl_getiwrange(struct net_device *dev,
109 struct iw_range *range;
111 range = (struct iw_range *) extra;
113 range->we_version_compiled = WIRELESS_EXT;
114 range->we_version_source = WIRELESS_EXT;
116 range->min_nwid = range->max_nwid = 0;
118 range->num_channels = NUM_CHANNELS;
120 for (i = 0; i < NUM_CHANNELS; i++) {
121 range->freq[k].i = i + 1;
122 range->freq[k].m = channel_frequency[i] * 100000;
123 range->freq[k].e = 1;
125 if (k >= IW_MAX_FREQUENCIES)
128 range->num_frequency = k;
129 range->sensitivity = 3;
131 /* nbd: don't know what this means, but other drivers set it this way */
132 range->pmp_flags = IW_POWER_PERIOD;
133 range->pmt_flags = IW_POWER_TIMEOUT;
134 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
137 range->max_pmp = 65535000;
139 range->max_pmt = 65535 * 1000;
142 if (wl_ioctl(dev, WLC_GET_RTS, &range->max_rts, sizeof(int)) < 0)
143 range->max_rts = 2347;
145 range->min_frag = 256;
147 if (wl_ioctl(dev, WLC_GET_FRAG, &range->max_frag, sizeof(int)) < 0)
148 range->max_frag = 2346;
150 range->txpower_capa = IW_TXPOW_MWATT;
156 static int wlcompat_set_scan(struct net_device *dev,
157 struct iw_request_info *info,
158 union iwreq_data *wrqu,
161 int ap = 0, oldap = 0;
162 wl_scan_params_t params;
164 memset(¶ms, 0, sizeof(params));
166 /* use defaults (same parameters as wl scan) */
167 memset(¶ms.bssid, 0xff, sizeof(params.bssid));
168 params.bss_type = DOT11_BSSTYPE_ANY;
169 params.scan_type = -1;
171 params.active_time = -1;
172 params.passive_time = -1;
173 params.home_time = -1;
175 /* can only scan in STA mode */
176 wl_ioctl(dev, WLC_GET_AP, &oldap, sizeof(oldap));
178 wl_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap));
180 if (wl_ioctl(dev, WLC_SCAN, ¶ms, 64) < 0)
184 wl_ioctl(dev, WLC_SET_AP, &oldap, sizeof(oldap));
190 static int wlcompat_get_scan(struct net_device *dev,
191 struct iw_request_info *info,
192 union iwreq_data *wrqu,
195 wl_scan_results_t *results = (wl_scan_results_t *) buf;
196 wl_bss_info_t *bss_info;
198 char *current_ev = extra;
200 char *end_buf = extra + IW_SCAN_MAX_DATA;
204 if (wl_ioctl(dev, WLC_SCAN_RESULTS, buf, WLC_IOCTL_MAXLEN) < 0)
207 bss_info = &(results->bss_info[0]);
208 info_ptr = (char *) bss_info;
209 for (i = 0; i < results->count; i++) {
211 /* send the cell address (must be sent first) */
213 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
214 memcpy(&iwe.u.ap_addr.sa_data, &bss_info->BSSID, sizeof(bss_info->BSSID));
215 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_ADDR_LEN);
218 iwe.cmd = SIOCGIWESSID;
219 iwe.u.data.length = bss_info->SSID_len;
220 if (iwe.u.data.length > IW_ESSID_MAX_SIZE)
221 iwe.u.data.length = IW_ESSID_MAX_SIZE;
222 iwe.u.data.flags = 1;
223 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, bss_info->SSID);
225 /* send frequency/channel info */
226 iwe.cmd = SIOCGIWFREQ;
228 iwe.u.freq.m = bss_info->channel;
229 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_FREQ_LEN);
231 /* add quality statistics */
233 iwe.u.qual.level = bss_info->RSSI;
234 iwe.u.qual.noise = bss_info->phy_noise;
236 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);
238 /* send rate information */
239 iwe.cmd = SIOCGIWRATE;
240 current_val = current_ev + IW_EV_LCP_LEN;
241 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
243 for(j = 0 ; j < bss_info->rateset.count ; j++) {
244 iwe.u.bitrate.value = ((bss_info->rateset.rates[j] & 0x7f) * 500000);
245 current_val = iwe_stream_add_value(current_ev, current_val, end_buf, &iwe, IW_EV_PARAM_LEN);
247 if((current_val - current_ev) > IW_EV_LCP_LEN)
248 current_ev = current_val;
250 info_ptr += sizeof(wl_bss_info_t);
251 if (bss_info->ie_length % 4)
252 info_ptr += bss_info->ie_length + 4 - (bss_info->ie_length % 4);
254 info_ptr += bss_info->ie_length;
255 bss_info = (wl_bss_info_t *) info_ptr;
258 wrqu->data.length = (current_ev - extra);
259 wrqu->data.flags = 0;
264 static int wlcompat_ioctl(struct net_device *dev,
265 struct iw_request_info *info,
266 union iwreq_data *wrqu,
271 strcpy(wrqu->name, "IEEE 802.11-DS");
277 if (wl_ioctl(dev,WLC_GET_CHANNEL, &ci, sizeof(ci)) < 0)
280 wrqu->freq.m = ci.target_channel;
286 if (wrqu->freq.e == 1) {
288 int f = wrqu->freq.m / 100000;
289 while ((channel < NUM_CHANNELS + 1) && (f != channel_frequency[channel]))
292 if (channel == NUM_CHANNELS) // channel not found
296 wrqu->freq.m = channel + 1;
298 if ((wrqu->freq.e == 0) && (wrqu->freq.m < 1000)) {
299 if (wl_ioctl(dev, WLC_SET_CHANNEL, &wrqu->freq.m, sizeof(int)) < 0)
308 if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
311 if (wl_ioctl(dev,WLC_SET_BSSID,wrqu->ap_addr.sa_data,6) < 0)
318 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
319 if (wl_ioctl(dev,WLC_GET_BSSID,wrqu->ap_addr.sa_data,6) < 0)
327 if (wl_ioctl(dev,WLC_GET_SSID, &ssid, sizeof(wlc_ssid_t)) < 0)
330 wrqu->essid.flags = wrqu->data.flags = 1;
331 wrqu->essid.length = wrqu->data.length = ssid.SSID_len + 1;
332 memcpy(extra,ssid.SSID,ssid.SSID_len + 1);
338 memset(&ssid, 0, sizeof(ssid));
339 ssid.SSID_len = strlen(extra);
340 if (ssid.SSID_len > WLC_ESSID_MAX_SIZE)
341 ssid.SSID_len = WLC_ESSID_MAX_SIZE;
342 memcpy(ssid.SSID, extra, ssid.SSID_len);
343 if (wl_ioctl(dev, WLC_SET_SSID, &ssid, sizeof(ssid)) < 0)
349 if (wl_ioctl(dev,WLC_GET_RTS,&(wrqu->rts.value),sizeof(int)) < 0)
355 if (wl_ioctl(dev,WLC_SET_RTS,&(wrqu->rts.value),sizeof(int)) < 0)
361 if (wl_ioctl(dev,WLC_GET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
367 if (wl_ioctl(dev,WLC_SET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
373 if (wl_get_val(dev, "qtxpower", &(wrqu->txpower.value), sizeof(int)) < 0)
376 wrqu->txpower.value &= ~WL_TXPWR_OVERRIDE;
378 wrqu->txpower.fixed = 0;
379 wrqu->txpower.disabled = 0;
380 wrqu->txpower.flags = IW_TXPOW_MWATT;
387 if (wl_get_val(dev, "qtxpower", &override, sizeof(int)) < 0)
390 wrqu->txpower.value |= override & WL_TXPWR_OVERRIDE;
392 if (wrqu->txpower.flags != IW_TXPOW_MWATT)
395 if (wl_set_val(dev, "qtxpower", &wrqu->txpower.value, sizeof(int)) < 0)
401 if (wl_ioctl(dev, WLC_GET_WEP, &val, sizeof(val)) < 0)
405 wrqu->data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
407 wrqu->data.flags = IW_ENCODE_DISABLED;
416 return wlcompat_ioctl_getiwrange(dev, extra);
421 int ap = -1, infra = -1, passive = 0, wet = 0;
423 switch (wrqu->mode) {
424 case IW_MODE_MONITOR:
448 if (wl_ioctl(dev, WLC_SET_PASSIVE, &passive, sizeof(passive)) < 0)
450 if (wl_ioctl(dev, WLC_SET_MONITOR, &passive, sizeof(passive)) < 0)
452 if (wl_ioctl(dev, WLC_SET_WET, &wet, sizeof(wet)) < 0)
455 if (wl_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap)) < 0)
458 if (wl_ioctl(dev, WLC_SET_INFRA, &infra, sizeof(infra)) < 0)
466 int ap, infra, wet, passive;
468 if (wl_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap)) < 0)
470 if (wl_ioctl(dev, WLC_GET_INFRA, &infra, sizeof(infra)) < 0)
472 if (wl_ioctl(dev, WLC_GET_PASSIVE, &passive, sizeof(passive)) < 0)
474 if (wl_ioctl(dev, WLC_GET_WET, &wet, sizeof(wet)) < 0)
478 wrqu->mode = IW_MODE_MONITOR;
480 wrqu->mode = IW_MODE_ADHOC;
483 wrqu->mode = IW_MODE_MASTER;
486 wrqu->mode = IW_MODE_REPEAT;
488 wrqu->mode = IW_MODE_INFRA;
496 if (info->cmd >= SIOCIWFIRSTPRIV)
497 return wlcompat_private_ioctl(dev, info, wrqu, extra);
506 static const iw_handler wlcompat_handler[] = {
507 NULL, /* SIOCSIWCOMMIT */
508 wlcompat_ioctl, /* SIOCGIWNAME */
509 NULL, /* SIOCSIWNWID */
510 NULL, /* SIOCGIWNWID */
511 wlcompat_ioctl, /* SIOCSIWFREQ */
512 wlcompat_ioctl, /* SIOCGIWFREQ */
513 wlcompat_ioctl, /* SIOCSIWMODE */
514 wlcompat_ioctl, /* SIOCGIWMODE */
515 NULL, /* SIOCSIWSENS */
516 NULL, /* SIOCGIWSENS */
517 NULL, /* SIOCSIWRANGE, unused */
518 wlcompat_ioctl, /* SIOCGIWRANGE */
519 NULL, /* SIOCSIWPRIV */
520 NULL, /* SIOCGIWPRIV */
521 NULL, /* SIOCSIWSTATS */
522 NULL, /* SIOCGIWSTATS */
523 iw_handler_set_spy, /* SIOCSIWSPY */
524 iw_handler_get_spy, /* SIOCGIWSPY */
525 iw_handler_set_thrspy, /* SIOCSIWTHRSPY */
526 iw_handler_get_thrspy, /* SIOCGIWTHRSPY */
527 wlcompat_ioctl, /* SIOCSIWAP */
528 wlcompat_ioctl, /* SIOCGIWAP */
529 NULL, /* -- hole -- */
530 NULL, /* SIOCGIWAPLIST */
531 wlcompat_set_scan, /* SIOCSIWSCAN */
532 wlcompat_get_scan, /* SIOCGIWSCAN */
533 wlcompat_ioctl, /* SIOCSIWESSID */
534 wlcompat_ioctl, /* SIOCGIWESSID */
535 NULL, /* SIOCSIWNICKN */
536 NULL, /* SIOCGIWNICKN */
537 NULL, /* -- hole -- */
538 NULL, /* -- hole -- */
539 NULL, /* SIOCSIWRATE */
540 NULL, /* SIOCGIWRATE */
541 wlcompat_ioctl, /* SIOCSIWRTS */
542 wlcompat_ioctl, /* SIOCGIWRTS */
543 wlcompat_ioctl, /* SIOCSIWFRAG */
544 wlcompat_ioctl, /* SIOCGIWFRAG */
545 wlcompat_ioctl, /* SIOCSIWTXPOW */
546 wlcompat_ioctl, /* SIOCGIWTXPOW */
547 NULL, /* SIOCSIWRETRY */
548 NULL, /* SIOCGIWRETRY */
549 NULL, /* SIOCSIWENCODE */
550 wlcompat_ioctl, /* SIOCGIWENCODE */
553 static int wlcompat_private_ioctl(struct net_device *dev,
554 struct iw_request_info *info,
555 union iwreq_data *wrqu,
558 int *value = (int *) wrqu->name;
561 case WLCOMPAT_SET_MONITOR:
563 if (wl_ioctl(dev, WLC_SET_MONITOR, value, sizeof(int)) < 0)
568 case WLCOMPAT_GET_MONITOR:
570 if (wl_ioctl(dev, WLC_GET_MONITOR, extra, sizeof(int)) < 0)
575 case WLCOMPAT_SET_TXPWR_LIMIT:
580 if (wl_get_val(dev, "qtxpower", &val, sizeof(int)) < 0)
584 val |= WL_TXPWR_OVERRIDE;
586 val &= ~WL_TXPWR_OVERRIDE;
588 if (wl_set_val(dev, "qtxpower", &val, sizeof(int)) < 0)
593 case WLCOMPAT_GET_TXPWR_LIMIT:
595 if (wl_get_val(dev, "qtxpower", value, sizeof(int)) < 0)
598 *value = ((*value & WL_TXPWR_OVERRIDE) == WL_TXPWR_OVERRIDE ? 1 : 0);
611 static const struct iw_priv_args wlcompat_private_args[] =
613 { WLCOMPAT_SET_MONITOR,
614 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
618 { WLCOMPAT_GET_MONITOR,
620 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
623 { WLCOMPAT_SET_TXPWR_LIMIT,
624 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
628 { WLCOMPAT_GET_TXPWR_LIMIT,
630 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
635 static const iw_handler wlcompat_private[] =
637 wlcompat_private_ioctl,
642 static const struct iw_handler_def wlcompat_handler_def =
644 .standard = (iw_handler *) wlcompat_handler,
645 .num_standard = sizeof(wlcompat_handler)/sizeof(iw_handler),
646 .private = wlcompat_private,
648 .private_args = wlcompat_private_args,
649 .num_private_args = sizeof(wlcompat_private_args) / sizeof(wlcompat_private_args[0])
654 void print_buffer(int len, unsigned char *buf) {
657 for (x=0;x<len && x<180 ;x++) {
660 printk("%02X",buf[x]);
669 static int (*old_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
670 static int new_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) {
672 struct iwreq *iwr = (struct iwreq *) ifr;
673 struct iw_request_info info;
676 printk("dev: %s ioctl: 0x%04x\n",dev->name,cmd);
679 if (cmd >= SIOCIWFIRSTPRIV) {
682 ret = wlcompat_private_ioctl(dev, &info, &(iwr->u), (char *) &(iwr->u));
684 } else if (cmd==SIOCDEVPRIVATE) {
685 wl_ioctl_t *ioc = (wl_ioctl_t *)ifr->ifr_data;
686 unsigned char *buf = ioc->buf;
687 printk(" cmd: %d buf: 0x%08x len: %d\n",ioc->cmd,&(ioc->buf),ioc->len);
689 print_buffer(ioc->len, buf);
690 ret = old_ioctl(dev,ifr,cmd);
692 print_buffer(ioc->len, buf);
693 printk(" ret: %d\n", ret);
696 ret = old_ioctl(dev,ifr,cmd);
701 static int __init wlcompat_init()
704 char *devname = "eth0";
706 while (!found && (dev = dev_get_by_name(devname))) {
707 if ((dev->wireless_handlers == NULL) && ((wl_ioctl(dev, WLC_GET_MAGIC, &i, sizeof(i)) == 0) && i == WLC_IOCTL_MAGIC))
713 printk("No Broadcom devices found.\n");
718 old_ioctl = dev->do_ioctl;
719 dev->do_ioctl = new_ioctl;
720 dev->wireless_handlers = (struct iw_handler_def *)&wlcompat_handler_def;
724 static void __exit wlcompat_exit()
726 dev->wireless_handlers = NULL;
727 dev->do_ioctl = old_ioctl;
732 MODULE_AUTHOR("openwrt.org");
733 MODULE_LICENSE("GPL");
735 module_init(wlcompat_init);
736 module_exit(wlcompat_exit);