]> git.enpas.org Git - openwrt.git/blob - package/openwrt/wlcompat.c
add SIOCSIWTXPOW
[openwrt.git] / package / openwrt / wlcompat.c
1 // insert header here
2 // mbm. gpl.
3
4 #include <linux/config.h>
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/if_arp.h>
8 #include <asm/uaccess.h>
9 #include <linux/wireless.h>
10
11 #include <net/iw_handler.h>
12 #include <wlioctl.h>
13
14 #define DEBUG
15
16 static struct net_device *dev;
17
18 /* The frequency of each channel in MHz */
19 const long channel_frequency[] = {
20         2412, 2417, 2422, 2427, 2432, 2437, 2442,
21         2447, 2452, 2457, 2462, 2467, 2472, 2484
22 };
23 #define NUM_CHANNELS ( sizeof(channel_frequency) / sizeof(channel_frequency[0]) )
24
25 static int wl_ioctl(struct net_device *dev, int cmd, void *buf, int len)
26 {
27         mm_segment_t old_fs = get_fs();
28         struct ifreq ifr;
29         int ret;
30         wl_ioctl_t ioc;
31         ioc.cmd = cmd;
32         ioc.buf = buf;
33         ioc.len = len;
34         strncpy(ifr.ifr_name, dev->name, IFNAMSIZ);
35         ifr.ifr_data = (caddr_t) &ioc;
36         set_fs(KERNEL_DS);
37         ret = dev->do_ioctl(dev,&ifr,SIOCDEVPRIVATE);
38         set_fs (old_fs);
39         return ret;
40 }
41
42 static int wlcompat_ioctl_getiwrange(struct net_device *dev,
43                                     char *extra)
44 {
45         int i, k;
46         struct iw_range *range;
47
48         range = (struct iw_range *) extra;
49
50         range->we_version_compiled = WIRELESS_EXT;
51         range->we_version_source = WIRELESS_EXT;
52         
53         range->min_nwid = range->max_nwid = 0;
54         
55         range->num_channels = NUM_CHANNELS;
56         k = 0;
57         for (i = 0; i < NUM_CHANNELS; i++) {
58                 range->freq[k].i = i + 1;
59                 range->freq[k].m = channel_frequency[i] * 100000;
60                 range->freq[k].e = 1;
61                 k++;
62                 if (k >= IW_MAX_FREQUENCIES)
63                         break;
64         }
65         range->num_frequency = k;
66         range->sensitivity = 3;
67
68         /* nbd: don't know what this means, but other drivers set it this way */
69         range->pmp_flags = IW_POWER_PERIOD;
70         range->pmt_flags = IW_POWER_TIMEOUT;
71         range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
72
73         range->min_rts = 0;
74         range->max_rts = 2347;
75         range->min_frag = 256;
76         range->max_frag = 2346;
77
78         range->min_pmp = 0;
79         range->max_pmp = 65535000;
80         range->min_pmt = 0;
81         range->max_pmt = 65535 * 1000;
82
83         range->txpower_capa = IW_TXPOW_MWATT;
84
85         return 0;
86 }
87
88 static int wlcompat_ioctl(struct net_device *dev,
89                          struct iw_request_info *info,
90                          union iwreq_data *wrqu,
91                          char *extra)
92 {
93         int err = 0;
94         
95         switch (info->cmd) {
96                 case SIOCGIWNAME:
97                         strcpy(wrqu->name, "IEEE 802.11-DS");
98                         break;
99                 case SIOCGIWFREQ:
100                 {
101                         channel_info_t ci;
102                         wl_ioctl(dev,WLC_GET_CHANNEL, &ci, sizeof(ci));
103                         wrqu->freq.m = ci.target_channel;
104                         wrqu->freq.e = 0;
105                         break;
106                 }
107                 case SIOCSIWFREQ:
108                 {
109                         if (wrqu->freq.e == 1) {
110                                 int channel = 0;
111                                 int f = wrqu->freq.m / 100000;
112                                 while ((channel < NUM_CHANNELS + 1) && (f != channel_frequency[channel]))
113                                         channel++;
114                                 
115                                 if (channel == NUM_CHANNELS) { // channel not found
116                                         err = -EINVAL;
117                                 } else {
118                                         wrqu->freq.e = 0;
119                                         wrqu->freq.m = channel + 1;
120                                 }
121                         }
122                         if ((wrqu->freq.e == 0) && (wrqu->freq.m < 1000)) {
123                                 wl_ioctl(dev, WLC_SET_CHANNEL, &wrqu->freq.m, sizeof(int));
124                         } else {
125                                 err = -EINVAL;
126                         }
127                 }
128                 case SIOCGIWAP:
129                 {
130                         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
131                         wl_ioctl(dev,WLC_GET_BSSID,wrqu->ap_addr.sa_data,6);
132                         break;
133                 }
134                 case SIOCGIWESSID:
135                 {
136                         wlc_ssid_t ssid;
137                         wl_ioctl(dev,WLC_GET_SSID, &ssid, sizeof(wlc_ssid_t));
138                         wrqu->essid.flags = wrqu->data.flags = 1;
139                         wrqu->essid.length = wrqu->data.length = ssid.SSID_len + 1;
140                         memcpy(extra,ssid.SSID,ssid.SSID_len + 1);
141                         break;
142                 }
143                 case SIOCSIWESSID:
144                 {
145                         wlc_ssid_t ssid;
146                         memset(&ssid, 0, sizeof(ssid));
147                         ssid.SSID_len = strlen(extra);
148                         if (ssid.SSID_len > WLC_ESSID_MAX_SIZE)
149                                 ssid.SSID_len = WLC_ESSID_MAX_SIZE;
150                         memcpy(ssid.SSID, extra, ssid.SSID_len);
151                         wl_ioctl(dev, WLC_SET_SSID, &ssid, sizeof(ssid));
152                         break;
153                 }
154                 case SIOCGIWRTS:
155                 {
156                         wl_ioctl(dev,WLC_GET_RTS,&(wrqu->rts.value),sizeof(int));
157                         break;
158                 }
159                 case SIOCGIWFRAG:
160                 {
161                         wl_ioctl(dev,WLC_GET_FRAG,&(wrqu->frag.value),sizeof(int));
162                         break;
163                 }
164                 case SIOCGIWTXPOW:
165                 {
166                         wrqu->txpower.value = 0;
167                         wl_ioctl(dev,WLC_GET_TXPWR, &(wrqu->txpower.value), sizeof(int));
168                         wrqu->txpower.fixed = 0;
169                         wrqu->txpower.disabled = 0;
170                         wrqu->txpower.flags = IW_TXPOW_MWATT;
171                         break;
172                 }
173                 case SIOCSIWTXPOW:
174                 {
175                         if (wrqu->txpower.flags != IW_TXPOW_MWATT) {
176                                 err = -EINVAL;
177                         } else {
178                                 wl_ioctl(dev, WLC_SET_TXPWR, &wrqu->txpower.value, sizeof(int));
179                         }
180                 }
181                 case SIOCGIWENCODE:
182                 {
183                         wrqu->data.flags = IW_ENCODE_DISABLED;
184                         break;
185                 }
186                 case SIOCGIWRANGE:
187                 {
188                         err = wlcompat_ioctl_getiwrange(dev, extra);
189                         break;
190                 }
191                 default:
192                 {
193                         err = -EINVAL;
194                         break;
195                 }
196         }
197         
198         return err;
199 }
200
201 static const iw_handler  wlcompat_handler[] = {
202         NULL,                   /* SIOCSIWCOMMIT */
203         wlcompat_ioctl,         /* SIOCGIWNAME */
204         NULL,                   /* SIOCSIWNWID */
205         NULL,                   /* SIOCGIWNWID */
206         wlcompat_ioctl,         /* SIOCSIWFREQ */
207         wlcompat_ioctl,         /* SIOCGIWFREQ */
208         NULL,                   /* SIOCSIWMODE */
209         NULL,                   /* SIOCGIWMODE */
210         NULL,                   /* SIOCSIWSENS */
211         NULL,                   /* SIOCGIWSENS */
212         NULL,                   /* SIOCSIWRANGE */
213         wlcompat_ioctl,         /* SIOCGIWRANGE */
214         NULL,                   /* SIOCSIWPRIV */
215         NULL,                   /* SIOCGIWPRIV */
216         NULL,                   /* SIOCSIWSTATS */
217         NULL,                   /* SIOCGIWSTATS */
218         iw_handler_set_spy,     /* SIOCSIWSPY */
219         iw_handler_get_spy,     /* SIOCGIWSPY */
220         iw_handler_set_thrspy,  /* SIOCSIWTHRSPY */
221         iw_handler_get_thrspy,  /* SIOCGIWTHRSPY */
222         NULL,                   /* SIOCSIWAP */
223         wlcompat_ioctl,         /* SIOCGIWAP */
224         NULL,                   /* -- hole -- */
225         NULL,                   /* SIOCGIWAPLIST */
226         NULL,                   /* -- hole -- */
227         NULL,                   /* -- hole -- */
228         wlcompat_ioctl,         /* SIOCSIWESSID */
229         wlcompat_ioctl,         /* SIOCGIWESSID */
230         NULL,                   /* SIOCSIWNICKN */
231         NULL,                   /* SIOCGIWNICKN */
232         NULL,                   /* -- hole -- */
233         NULL,                   /* -- hole -- */
234         NULL,                   /* SIOCSIWRATE */
235         NULL,                   /* SIOCGIWRATE */
236         NULL,                   /* SIOCSIWRTS */
237         wlcompat_ioctl,         /* SIOCGIWRTS */
238         NULL,                   /* SIOCSIWFRAG */
239         wlcompat_ioctl,         /* SIOCGIWFRAG */
240         wlcompat_ioctl,         /* SIOCSIWTXPOW */
241         wlcompat_ioctl,         /* SIOCGIWTXPOW */
242         NULL,                   /* SIOCSIWRETRY */
243         NULL,                   /* SIOCGIWRETRY */
244         NULL,                   /* SIOCSIWENCODE */
245         wlcompat_ioctl,         /* SIOCGIWENCODE */
246 };
247
248 static const struct iw_handler_def      wlcompat_handler_def =
249 {
250         .standard       = (iw_handler *) wlcompat_handler,
251         .num_standard   = sizeof(wlcompat_handler)/sizeof(iw_handler),
252         .private        = NULL,
253         .num_private    = 0,
254         .private_args   = NULL, 
255         .num_private_args = 0,
256 };
257
258 #ifdef DEBUG
259 static int (*old_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
260 static int new_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) {
261         int ret = old_ioctl(dev,ifr,cmd);
262         printk("dev: %s ioctl: 0x%04x\n",dev->name,cmd);
263         if (cmd==SIOCDEVPRIVATE) {
264                 int x;
265                 wl_ioctl_t *ioc = (wl_ioctl_t *)ifr->ifr_data;
266                 unsigned char *buf = ioc->buf;
267                 printk("   cmd: %d buf: 0x%08x len: %d\n",ioc->cmd,&(ioc->buf),ioc->len);
268                 printk("   ->");
269                 for (x=0;x<ioc->len && x<128 ;x++) {
270                         printk("%02X",buf[x]);
271                 }
272                 printk("\n");
273         }
274         return ret;
275 }
276 #endif
277
278 static int __init wlcompat_init()
279 {
280         dev = dev_get_by_name("eth1");
281 #ifdef DEBUG
282         old_ioctl = dev->do_ioctl;
283         dev->do_ioctl = new_ioctl;
284 #endif
285         dev->wireless_handlers = (struct iw_handler_def *)&wlcompat_handler_def;
286         return 0;
287 }
288
289 static void __exit wlcompat_exit()
290 {
291         dev->wireless_handlers = NULL;
292 #ifdef DEBUG
293         dev->do_ioctl = old_ioctl;
294 #endif
295         return;
296 }
297
298 EXPORT_NO_SYMBOLS;
299 MODULE_AUTHOR("openwrt.org");
300 MODULE_LICENSE("GPL");
301
302 module_init(wlcompat_init);
303 module_exit(wlcompat_exit);