*** empty log message ***
[openwrt.git] / package / openwrt / wlcompat.c
index eb85a9e..d010e2b 100644 (file)
@@ -1,5 +1,26 @@
-// insert header here
-// mbm. gpl.
+/*
+ * wlcompat.c
+ *
+ * Copyright (C) 2005 Mike Baker,
+ *                    Felix Fietkau <nbd@vd-s.ath.cx>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * $Id$
+ */
+
 
 #include <linux/config.h>
 #include <linux/module.h>
 
 #include <linux/config.h>
 #include <linux/module.h>
@@ -10,8 +31,7 @@
 
 #include <net/iw_handler.h>
 #include <wlioctl.h>
 
 #include <net/iw_handler.h>
 #include <wlioctl.h>
-
-#define DEBUG
+#include <wlcompat.h>
 
 static struct net_device *dev;
 char buf[WLC_IOCTL_MAXLEN];
 
 static struct net_device *dev;
 char buf[WLC_IOCTL_MAXLEN];
@@ -23,6 +43,13 @@ const long channel_frequency[] = {
 };
 #define NUM_CHANNELS ( sizeof(channel_frequency) / sizeof(channel_frequency[0]) )
 
 };
 #define NUM_CHANNELS ( sizeof(channel_frequency) / sizeof(channel_frequency[0]) )
 
+
+
+static int wlcompat_private_ioctl(struct net_device *dev,
+                        struct iw_request_info *info,
+                        union iwreq_data *wrqu,
+                        char *extra);
+
 static int wl_ioctl(struct net_device *dev, int cmd, void *buf, int len)
 {
        mm_segment_t old_fs = get_fs();
 static int wl_ioctl(struct net_device *dev, int cmd, void *buf, int len)
 {
        mm_segment_t old_fs = get_fs();
@@ -40,6 +67,50 @@ static int wl_ioctl(struct net_device *dev, int cmd, void *buf, int len)
        return ret;
 }
 
        return ret;
 }
 
+static int wl_set_val(struct net_device *dev, char *var, void *val, int len)
+{
+       char buf[128];
+       int buf_len;
+
+       /* check for overflow */
+       if ((buf_len = strlen(var)) + 1 + len > sizeof(buf))
+               return -1;
+       
+       strcpy(buf, var);
+       buf_len += 1;
+
+       /* append int value onto the end of the name string */
+       memcpy(&buf[buf_len], val, len);
+       buf_len += len;
+
+       return wl_ioctl(dev, WLC_SET_VAR, buf, buf_len);
+}
+
+static int wl_get_val(struct net_device *dev, char *var, void *val, int len)
+{
+       char buf[128];
+       int ret;
+
+       /* check for overflow */
+       if (strlen(var) + 1 > sizeof(buf) || len > sizeof(buf))
+               return -1;
+       
+       strcpy(buf, var);
+       if ((ret = wl_ioctl(dev, WLC_GET_VAR, buf, sizeof(buf))))
+               return ret;
+
+       memcpy(val, buf, len);
+       return 0;
+}
+
+int read_shmem(struct net_device *dev, int offset)
+{
+       if (wl_ioctl(dev, WLC_GET_SHMEM, &offset, sizeof(offset)) < 0)
+               return -EINVAL;
+
+       return offset;
+}
+
 static int wlcompat_ioctl_getiwrange(struct net_device *dev,
                                    char *extra)
 {
 static int wlcompat_ioctl_getiwrange(struct net_device *dev,
                                    char *extra)
 {
@@ -137,7 +208,7 @@ static int wlcompat_get_scan(struct net_device *dev,
        char *current_val;
        char *end_buf = extra + IW_SCAN_MAX_DATA;
        struct iw_event iwe;
        char *current_val;
        char *end_buf = extra + IW_SCAN_MAX_DATA;
        struct iw_event iwe;
-       int i;
+       int i, j;
 
        if (wl_ioctl(dev, WLC_SCAN_RESULTS, buf, WLC_IOCTL_MAXLEN) < 0)
                return -EAGAIN;
 
        if (wl_ioctl(dev, WLC_SCAN_RESULTS, buf, WLC_IOCTL_MAXLEN) < 0)
                return -EAGAIN;
@@ -178,8 +249,8 @@ static int wlcompat_get_scan(struct net_device *dev,
                current_val = current_ev + IW_EV_LCP_LEN;
                iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
                
                current_val = current_ev + IW_EV_LCP_LEN;
                iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
                
-               for(i = 0 ; i < bss_info->rateset.count ; i++) {
-                       iwe.u.bitrate.value = ((bss_info->rateset.rates[i] & 0x7f) * 500000);
+               for(j = 0 ; j < bss_info->rateset.count ; j++) {
+                       iwe.u.bitrate.value = ((bss_info->rateset.rates[j] & 0x7f) * 500000);
                        current_val = iwe_stream_add_value(current_ev, current_val, end_buf, &iwe, IW_EV_PARAM_LEN);
                }
                if((current_val - current_ev) > IW_EV_LCP_LEN)
                        current_val = iwe_stream_add_value(current_ev, current_val, end_buf, &iwe, IW_EV_PARAM_LEN);
                }
                if((current_val - current_ev) > IW_EV_LCP_LEN)
@@ -241,6 +312,21 @@ static int wlcompat_ioctl(struct net_device *dev,
                        }
                        break;
                }
                        }
                        break;
                }
+               case SIOCSIWAP:
+               {
+                       int ap = 0;
+                       
+                       if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
+                               return -EINVAL;
+
+                       if (wl_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap)) < 0)
+                               return -EINVAL;
+                       
+                       if (wl_ioctl(dev, (ap ? WLC_SET_BSSID : WLC_REASSOC), wrqu->ap_addr.sa_data, 6) < 0)
+                               return -EINVAL;
+
+                       break;
+               }
                case SIOCGIWAP:
                {
                        wrqu->ap_addr.sa_family = ARPHRD_ETHER;
                case SIOCGIWAP:
                {
                        wrqu->ap_addr.sa_family = ARPHRD_ETHER;
@@ -298,9 +384,11 @@ static int wlcompat_ioctl(struct net_device *dev,
                }
                case SIOCGIWTXPOW:
                {
                }
                case SIOCGIWTXPOW:
                {
-                       wrqu->txpower.value = 0;
-                       if (wl_ioctl(dev,WLC_GET_TXPWR, &(wrqu->txpower.value), sizeof(int)) < 0)
+                       if (wl_get_val(dev, "qtxpower", &(wrqu->txpower.value), sizeof(int)) < 0)
                                return -EINVAL;
                                return -EINVAL;
+                       
+                       wrqu->txpower.value &= ~WL_TXPWR_OVERRIDE;
+                               
                        wrqu->txpower.fixed = 0;
                        wrqu->txpower.disabled = 0;
                        wrqu->txpower.flags = IW_TXPOW_MWATT;
                        wrqu->txpower.fixed = 0;
                        wrqu->txpower.disabled = 0;
                        wrqu->txpower.flags = IW_TXPOW_MWATT;
@@ -308,15 +396,59 @@ static int wlcompat_ioctl(struct net_device *dev,
                }
                case SIOCSIWTXPOW:
                {
                }
                case SIOCSIWTXPOW:
                {
+                       int override;
+                       
+                       if (wl_get_val(dev, "qtxpower", &override, sizeof(int)) < 0)
+                               return -EINVAL;
+                       
+                       wrqu->txpower.value |= override & WL_TXPWR_OVERRIDE;
+                       
                        if (wrqu->txpower.flags != IW_TXPOW_MWATT)
                                return -EINVAL;
 
                        if (wrqu->txpower.flags != IW_TXPOW_MWATT)
                                return -EINVAL;
 
-                       if (wl_ioctl(dev, WLC_SET_TXPWR, &wrqu->txpower.value, sizeof(int)) < 0)
+                       if (wl_set_val(dev, "qtxpower", &wrqu->txpower.value, sizeof(int)) < 0)
                                return -EINVAL;
                }
                case SIOCGIWENCODE:
                {
                                return -EINVAL;
                }
                case SIOCGIWENCODE:
                {
-                       wrqu->data.flags = IW_ENCODE_DISABLED;
+                       int val;
+                       
+                       if (wl_ioctl(dev, WLC_GET_WEP, &val, sizeof(val)) < 0)
+                               return -EINVAL;
+                       
+
+                       if (val > 0) {
+                               int key;
+                               
+                               for (key = val = 0; (key < 4) && (val == 0); key++) {
+                                       val = key;
+                                       if (wl_ioctl(dev, WLC_GET_KEY_PRIMARY, &val, sizeof(val)) < 0)
+                                               return -EINVAL;
+                               }
+                               
+                               wrqu->data.flags = IW_ENCODE_ENABLED;
+                               if (key-- > 0) {
+                                       int magic_offset;
+                                       int16 buffer[8];
+                                       
+                                       magic_offset = read_shmem(dev, 0x56) * 2;
+
+                                       wrqu->data.flags |= key + 1;
+                                       wrqu->data.length = 16;
+                                       
+                                       for (val = 0; val < 8; val++) {
+                                               buffer[val] = read_shmem(dev, magic_offset + (key * 16) + val * 2);
+                                       }
+                                       
+                                       memset(extra, 0, 16);
+                                       memcpy(extra, buffer, 16);
+                               } else {
+                                       wrqu->data.flags |= IW_ENCODE_NOKEY;
+                               }
+                       } else {
+                               wrqu->data.flags = IW_ENCODE_DISABLED;
+                       }
+                       
                        break;
                }
                case SIOCGIWRANGE:
                        break;
                }
                case SIOCGIWRANGE:
@@ -401,6 +533,9 @@ static int wlcompat_ioctl(struct net_device *dev,
                }
                default:
                {
                }
                default:
                {
+                       if (info->cmd >= SIOCIWFIRSTPRIV)
+                               return wlcompat_private_ioctl(dev, info, wrqu, extra);
+
                        return -EINVAL;
                }
        }
                        return -EINVAL;
                }
        }
@@ -429,7 +564,7 @@ static const iw_handler      wlcompat_handler[] = {
        iw_handler_get_spy,     /* SIOCGIWSPY */
        iw_handler_set_thrspy,  /* SIOCSIWTHRSPY */
        iw_handler_get_thrspy,  /* SIOCGIWTHRSPY */
        iw_handler_get_spy,     /* SIOCGIWSPY */
        iw_handler_set_thrspy,  /* SIOCSIWTHRSPY */
        iw_handler_get_thrspy,  /* SIOCGIWTHRSPY */
-       NULL,                   /* SIOCSIWAP */
+       wlcompat_ioctl,         /* SIOCSIWAP */
        wlcompat_ioctl,         /* SIOCGIWAP */
        NULL,                   /* -- hole -- */
        NULL,                   /* SIOCGIWAPLIST */
        wlcompat_ioctl,         /* SIOCGIWAP */
        NULL,                   /* -- hole -- */
        NULL,                   /* SIOCGIWAPLIST */
@@ -455,43 +590,221 @@ static const iw_handler   wlcompat_handler[] = {
        wlcompat_ioctl,         /* SIOCGIWENCODE */
 };
 
        wlcompat_ioctl,         /* SIOCGIWENCODE */
 };
 
-static const struct iw_handler_def      wlcompat_handler_def =
+static int wlcompat_private_ioctl(struct net_device *dev,
+                        struct iw_request_info *info,
+                        union iwreq_data *wrqu,
+                        char *extra)
+{
+       int *value = (int *) wrqu->name;
+
+       switch (info->cmd) {
+               case WLCOMPAT_SET_MONITOR:
+               {
+                       if (wl_ioctl(dev, WLC_SET_MONITOR, value, sizeof(int)) < 0)
+                               return -EINVAL;
+
+                       break;
+               }
+               case WLCOMPAT_GET_MONITOR:
+               {
+                       if (wl_ioctl(dev, WLC_GET_MONITOR, extra, sizeof(int)) < 0)
+                               return -EINVAL;
+
+                       break;
+               }
+               case WLCOMPAT_SET_TXPWR_LIMIT:
+               {
+                       int val;
+                       
+
+                       if (wl_get_val(dev, "qtxpower", &val, sizeof(int)) < 0)
+                               return -EINVAL;
+                       
+                       if (*extra > 0)
+                               val |= WL_TXPWR_OVERRIDE;
+                       else
+                               val &= ~WL_TXPWR_OVERRIDE;
+                       
+                       if (wl_set_val(dev, "qtxpower", &val, sizeof(int)) < 0)
+                               return -EINVAL;
+                       
+                       break;
+               }
+               case WLCOMPAT_GET_TXPWR_LIMIT:
+               {
+                       if (wl_get_val(dev, "qtxpower", value, sizeof(int)) < 0)
+                               return -EINVAL;
+
+                       *value = ((*value & WL_TXPWR_OVERRIDE) == WL_TXPWR_OVERRIDE ? 1 : 0);
+
+                       break;
+               }
+               case WLCOMPAT_SET_ANTDIV:
+               {
+                       if (wl_ioctl(dev, WLC_SET_ANTDIV, value, sizeof(int)) < 0)
+                               return -EINVAL;
+
+                       break;
+               }
+               case WLCOMPAT_GET_ANTDIV:
+               {
+                       if (wl_ioctl(dev, WLC_GET_ANTDIV, extra, sizeof(int)) < 0)
+                               return -EINVAL;
+
+                       break;
+               }
+               case WLCOMPAT_SET_TXANT:
+               {
+                       if (wl_ioctl(dev, WLC_SET_TXANT, value, sizeof(int)) < 0)
+                               return -EINVAL;
+
+                       break;
+               }
+               case WLCOMPAT_GET_TXANT:
+               {
+                       if (wl_ioctl(dev, WLC_GET_TXANT, extra, sizeof(int)) < 0)
+                               return -EINVAL;
+
+                       break;
+               }
+               default:
+               {
+                       return -EINVAL;
+               }
+                       
+       }
+       return 0;
+}
+
+static const struct iw_priv_args wlcompat_private_args[] = 
+{
+       {       WLCOMPAT_SET_MONITOR, 
+               IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
+               0,
+               "set_monitor"
+       },
+       {       WLCOMPAT_GET_MONITOR, 
+               0,
+               IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
+               "get_monitor"
+       },
+       {       WLCOMPAT_SET_TXPWR_LIMIT, 
+               IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
+               0,
+               "set_txpwr_force"
+       },
+       {       WLCOMPAT_GET_TXPWR_LIMIT, 
+               0,
+               IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
+               "get_txpwr_force"
+       },
+       {       WLCOMPAT_SET_ANTDIV, 
+               IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
+               0,
+               "set_antdiv"
+       },
+       {       WLCOMPAT_GET_ANTDIV, 
+               0,
+               IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
+               "get_antdiv"
+       },
+       {       WLCOMPAT_SET_TXANT, 
+               IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
+               0,
+               "set_txant"
+       },
+       {       WLCOMPAT_GET_TXANT, 
+               0,
+               IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
+               "get_txant"
+       },
+};
+
+static const iw_handler wlcompat_private[] =
+{
+       wlcompat_private_ioctl,
+       NULL
+};
+
+
+static const struct iw_handler_def wlcompat_handler_def =
 {
        .standard       = (iw_handler *) wlcompat_handler,
        .num_standard   = sizeof(wlcompat_handler)/sizeof(iw_handler),
 {
        .standard       = (iw_handler *) wlcompat_handler,
        .num_standard   = sizeof(wlcompat_handler)/sizeof(iw_handler),
-       .private        = NULL,
-       .num_private    = 0,
-       .private_args   = NULL, 
-       .num_private_args = 0,
+       .private        = wlcompat_private,
+       .num_private    = 1,
+       .private_args   = wlcompat_private_args,
+       .num_private_args = sizeof(wlcompat_private_args) / sizeof(wlcompat_private_args[0])
 };
 
 };
 
+
 #ifdef DEBUG
 #ifdef DEBUG
+void print_buffer(int len, unsigned char *buf) {
+       int x;
+       if (buf != NULL) {
+               for (x=0;x<len && x<180 ;x++) {
+                       if ((x % 4) == 0)
+                               printk(" ");
+                       printk("%02X",buf[x]);
+               }
+       } else {
+               printk(" NULL");
+       }
+       printk("\n");
+
+}
+#endif
 static int (*old_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
 static int new_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) {
 static int (*old_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
 static int new_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) {
-       int ret = old_ioctl(dev,ifr,cmd);
+       int ret = 0;
+       struct iwreq *iwr = (struct iwreq *) ifr;
+       struct iw_request_info info;
+       
+#ifdef DEBUG
        printk("dev: %s ioctl: 0x%04x\n",dev->name,cmd);
        printk("dev: %s ioctl: 0x%04x\n",dev->name,cmd);
-       if (cmd==SIOCDEVPRIVATE) {
-               int x;
+#endif
+
+       if (cmd >= SIOCIWFIRSTPRIV) {
+               info.cmd = cmd;
+               info.flags = 0;
+               ret = wlcompat_private_ioctl(dev, &info, &(iwr->u), (char *) &(iwr->u));
+#ifdef DEBUG
+       } else if (cmd==SIOCDEVPRIVATE) {
                wl_ioctl_t *ioc = (wl_ioctl_t *)ifr->ifr_data;
                unsigned char *buf = ioc->buf;
                printk("   cmd: %d buf: 0x%08x len: %d\n",ioc->cmd,&(ioc->buf),ioc->len);
                wl_ioctl_t *ioc = (wl_ioctl_t *)ifr->ifr_data;
                unsigned char *buf = ioc->buf;
                printk("   cmd: %d buf: 0x%08x len: %d\n",ioc->cmd,&(ioc->buf),ioc->len);
-               printk("   ->");
-               for (x=0;x<ioc->len && x<128 ;x++) {
-                       printk("%02X",buf[x]);
-               }
-               printk("\n");
+               printk("   send: ->");
+               print_buffer(ioc->len, buf);
+               ret = old_ioctl(dev,ifr,cmd);
+               printk("   recv: ->");
+               print_buffer(ioc->len, buf);
+               printk("   ret: %d\n", ret);
+#endif
+       } else {
+               ret = old_ioctl(dev,ifr,cmd);
        }
        return ret;
 }
        }
        return ret;
 }
-#endif
 
 static int __init wlcompat_init()
 {
 
 static int __init wlcompat_init()
 {
-       dev = dev_get_by_name("eth1");
-#ifdef DEBUG
+       int found = 0, i;
+       char *devname = "eth0";
+       
+       while (!found && (dev = dev_get_by_name(devname))) {
+               if ((dev->wireless_handlers == NULL) && ((wl_ioctl(dev, WLC_GET_MAGIC, &i, sizeof(i)) == 0) && i == WLC_IOCTL_MAGIC))
+                       found = 1;
+               devname[3]++;
+       }
+       
+       if (!found) {
+               printk("No Broadcom devices found.\n");
+               return -ENODEV;
+       }
+               
+
        old_ioctl = dev->do_ioctl;
        dev->do_ioctl = new_ioctl;
        old_ioctl = dev->do_ioctl;
        dev->do_ioctl = new_ioctl;
-#endif
        dev->wireless_handlers = (struct iw_handler_def *)&wlcompat_handler_def;
        return 0;
 }
        dev->wireless_handlers = (struct iw_handler_def *)&wlcompat_handler_def;
        return 0;
 }
@@ -499,9 +812,7 @@ static int __init wlcompat_init()
 static void __exit wlcompat_exit()
 {
        dev->wireless_handlers = NULL;
 static void __exit wlcompat_exit()
 {
        dev->wireless_handlers = NULL;
-#ifdef DEBUG
        dev->do_ioctl = old_ioctl;
        dev->do_ioctl = old_ioctl;
-#endif
        return;
 }
 
        return;
 }
 
This page took 0.036511 seconds and 4 git commands to generate.