b6a9c20b7f6dd931a68c410572e329c1c1ac01c6
[openwrt.git] / package / openwrt / wlcompat.c
1 /*
2  * wlcompat.c
3  *
4  * Copyright (C) 2005 Mike Baker,
5  *                    Felix Fietkau <nbd@vd-s.ath.cx>
6  *
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.
11  *
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.
16  *
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.
20  *
21  * $Id$
22  */
23
24
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>
31
32 #include <net/iw_handler.h>
33 #include <wlioctl.h>
34
35 #define DEBUG
36
37 static struct net_device *dev;
38 char buf[WLC_IOCTL_MAXLEN];
39
40 /* The frequency of each channel in MHz */
41 const long channel_frequency[] = {
42         2412, 2417, 2422, 2427, 2432, 2437, 2442,
43         2447, 2452, 2457, 2462, 2467, 2472, 2484
44 };
45 #define NUM_CHANNELS ( sizeof(channel_frequency) / sizeof(channel_frequency[0]) )
46
47 static int wl_ioctl(struct net_device *dev, int cmd, void *buf, int len)
48 {
49         mm_segment_t old_fs = get_fs();
50         struct ifreq ifr;
51         int ret;
52         wl_ioctl_t ioc;
53         ioc.cmd = cmd;
54         ioc.buf = buf;
55         ioc.len = len;
56         strncpy(ifr.ifr_name, dev->name, IFNAMSIZ);
57         ifr.ifr_data = (caddr_t) &ioc;
58         set_fs(KERNEL_DS);
59         ret = dev->do_ioctl(dev,&ifr,SIOCDEVPRIVATE);
60         set_fs (old_fs);
61         return ret;
62 }
63
64 static int wlcompat_ioctl_getiwrange(struct net_device *dev,
65                                     char *extra)
66 {
67         int i, k;
68         struct iw_range *range;
69
70         range = (struct iw_range *) extra;
71
72         range->we_version_compiled = WIRELESS_EXT;
73         range->we_version_source = WIRELESS_EXT;
74         
75         range->min_nwid = range->max_nwid = 0;
76         
77         range->num_channels = NUM_CHANNELS;
78         k = 0;
79         for (i = 0; i < NUM_CHANNELS; i++) {
80                 range->freq[k].i = i + 1;
81                 range->freq[k].m = channel_frequency[i] * 100000;
82                 range->freq[k].e = 1;
83                 k++;
84                 if (k >= IW_MAX_FREQUENCIES)
85                         break;
86         }
87         range->num_frequency = k;
88         range->sensitivity = 3;
89
90         /* nbd: don't know what this means, but other drivers set it this way */
91         range->pmp_flags = IW_POWER_PERIOD;
92         range->pmt_flags = IW_POWER_TIMEOUT;
93         range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
94
95         range->min_pmp = 0;
96         range->max_pmp = 65535000;
97         range->min_pmt = 0;
98         range->max_pmt = 65535 * 1000;
99
100         range->min_rts = 0;
101         if (wl_ioctl(dev, WLC_GET_RTS, &range->max_rts, sizeof(int)) < 0)
102                 range->max_rts = 2347;
103
104         range->min_frag = 256;
105         
106         if (wl_ioctl(dev, WLC_GET_FRAG, &range->max_frag, sizeof(int)) < 0)
107                 range->max_frag = 2346;
108
109         range->txpower_capa = IW_TXPOW_MWATT;
110
111         return 0;
112 }
113
114
115 static int wlcompat_set_scan(struct net_device *dev,
116                          struct iw_request_info *info,
117                          union iwreq_data *wrqu,
118                          char *extra)
119 {
120         int ap = 0, oldap = 0;
121         wl_scan_params_t params;
122
123         memset(&params, 0, sizeof(params));
124
125         /* use defaults (same parameters as wl scan) */
126         memset(&params.bssid, 0xff, sizeof(params.bssid));
127         params.bss_type = DOT11_BSSTYPE_ANY;
128         params.scan_type = -1;
129         params.nprobes = -1;
130         params.active_time = -1;
131         params.passive_time = -1;
132         params.home_time = -1;
133         
134         /* can only scan in STA mode */
135         wl_ioctl(dev, WLC_GET_AP, &oldap, sizeof(oldap));
136         if (oldap > 0)
137                 wl_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap));
138         
139         if (wl_ioctl(dev, WLC_SCAN, &params, 64) < 0)
140                 return -EINVAL;
141         
142         if (oldap > 0)
143                 wl_ioctl(dev, WLC_SET_AP, &oldap, sizeof(oldap));
144
145         return 0;
146 }
147
148
149 static int wlcompat_get_scan(struct net_device *dev,
150                          struct iw_request_info *info,
151                          union iwreq_data *wrqu,
152                          char *extra)
153 {
154         wl_scan_results_t *results = (wl_scan_results_t *) buf;
155         wl_bss_info_t *bss_info;
156         char *info_ptr;
157         char *current_ev = extra;
158         char *current_val;
159         char *end_buf = extra + IW_SCAN_MAX_DATA;
160         struct iw_event iwe;
161         int i, j;
162
163         if (wl_ioctl(dev, WLC_SCAN_RESULTS, buf, WLC_IOCTL_MAXLEN) < 0)
164                 return -EAGAIN;
165         
166         bss_info = &(results->bss_info[0]);
167         info_ptr = (char *) bss_info;
168         for (i = 0; i < results->count; i++) {
169
170                 /* send the cell address (must be sent first) */
171                 iwe.cmd = SIOCGIWAP;
172                 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
173                 memcpy(&iwe.u.ap_addr.sa_data, &bss_info->BSSID, sizeof(bss_info->BSSID));
174                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_ADDR_LEN);
175                 
176                 /* send the ESSID */
177                 iwe.cmd = SIOCGIWESSID;
178                 iwe.u.data.length = bss_info->SSID_len;
179                 if (iwe.u.data.length > IW_ESSID_MAX_SIZE)
180                         iwe.u.data.length = IW_ESSID_MAX_SIZE;
181                 iwe.u.data.flags = 1;
182                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, bss_info->SSID);
183
184                 /* send frequency/channel info */
185                 iwe.cmd = SIOCGIWFREQ;
186                 iwe.u.freq.e = 0;
187                 iwe.u.freq.m = bss_info->channel;
188                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_FREQ_LEN);
189
190                 /* add quality statistics */
191                 iwe.cmd = IWEVQUAL;
192                 iwe.u.qual.level = bss_info->RSSI;
193                 iwe.u.qual.noise = bss_info->phy_noise;
194                 iwe.u.qual.qual = 0;
195                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);
196         
197                 /* send rate information */
198                 iwe.cmd = SIOCGIWRATE;
199                 current_val = current_ev + IW_EV_LCP_LEN;
200                 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
201                 
202                 for(j = 0 ; j < bss_info->rateset.count ; j++) {
203                         iwe.u.bitrate.value = ((bss_info->rateset.rates[j] & 0x7f) * 500000);
204                         current_val = iwe_stream_add_value(current_ev, current_val, end_buf, &iwe, IW_EV_PARAM_LEN);
205                 }
206                 if((current_val - current_ev) > IW_EV_LCP_LEN)
207                         current_ev = current_val;
208
209                 info_ptr += sizeof(wl_bss_info_t);
210                 if (bss_info->ie_length % 4)
211                         info_ptr += bss_info->ie_length + 4 - (bss_info->ie_length % 4);
212                 else
213                         info_ptr += bss_info->ie_length;
214                 bss_info = (wl_bss_info_t *) info_ptr;
215         }
216         
217         wrqu->data.length = (current_ev - extra);
218         wrqu->data.flags = 0;
219
220         return 0;
221 }
222
223 static int wlcompat_ioctl(struct net_device *dev,
224                          struct iw_request_info *info,
225                          union iwreq_data *wrqu,
226                          char *extra)
227 {
228         switch (info->cmd) {
229                 case SIOCGIWNAME:
230                         strcpy(wrqu->name, "IEEE 802.11-DS");
231                         break;
232                 case SIOCGIWFREQ:
233                 {
234                         channel_info_t ci;
235
236                         if (wl_ioctl(dev,WLC_GET_CHANNEL, &ci, sizeof(ci)) < 0)
237                                 return -EINVAL;
238
239                         wrqu->freq.m = ci.target_channel;
240                         wrqu->freq.e = 0;
241                         break;
242                 }
243                 case SIOCSIWFREQ:
244                 {
245                         if (wrqu->freq.e == 1) {
246                                 int channel = 0;
247                                 int f = wrqu->freq.m / 100000;
248                                 while ((channel < NUM_CHANNELS + 1) && (f != channel_frequency[channel]))
249                                         channel++;
250                                 
251                                 if (channel == NUM_CHANNELS) // channel not found
252                                         return -EINVAL;
253
254                                 wrqu->freq.e = 0;
255                                 wrqu->freq.m = channel + 1;
256                         }
257                         if ((wrqu->freq.e == 0) && (wrqu->freq.m < 1000)) {
258                                 if (wl_ioctl(dev, WLC_SET_CHANNEL, &wrqu->freq.m, sizeof(int)) < 0)
259                                         return -EINVAL;
260                         } else {
261                                 return -EINVAL;
262                         }
263                         break;
264                 }
265                 case SIOCGIWAP:
266                 {
267                         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
268                         if (wl_ioctl(dev,WLC_GET_BSSID,wrqu->ap_addr.sa_data,6) < 0)
269                                 return -EINVAL;
270                         break;
271                 }
272                 case SIOCGIWESSID:
273                 {
274                         wlc_ssid_t ssid;
275                         
276                         if (wl_ioctl(dev,WLC_GET_SSID, &ssid, sizeof(wlc_ssid_t)) < 0)
277                                 return -EINVAL;
278
279                         wrqu->essid.flags = wrqu->data.flags = 1;
280                         wrqu->essid.length = wrqu->data.length = ssid.SSID_len + 1;
281                         memcpy(extra,ssid.SSID,ssid.SSID_len + 1);
282                         break;
283                 }
284                 case SIOCSIWESSID:
285                 {
286                         wlc_ssid_t ssid;
287                         memset(&ssid, 0, sizeof(ssid));
288                         ssid.SSID_len = strlen(extra);
289                         if (ssid.SSID_len > WLC_ESSID_MAX_SIZE)
290                                 ssid.SSID_len = WLC_ESSID_MAX_SIZE;
291                         memcpy(ssid.SSID, extra, ssid.SSID_len);
292                         if (wl_ioctl(dev, WLC_SET_SSID, &ssid, sizeof(ssid)) < 0)
293                                 return -EINVAL;
294                         break;
295                 }
296                 case SIOCGIWRTS:
297                 {
298                         if (wl_ioctl(dev,WLC_GET_RTS,&(wrqu->rts.value),sizeof(int)) < 0) 
299                                 return -EINVAL;
300                         break;
301                 }
302                 case SIOCSIWRTS:
303                 {
304                         if (wl_ioctl(dev,WLC_SET_RTS,&(wrqu->rts.value),sizeof(int)) < 0) 
305                                 return -EINVAL;
306                         break;
307                 }
308                 case SIOCGIWFRAG:
309                 {
310                         if (wl_ioctl(dev,WLC_GET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
311                                 return -EINVAL;
312                         break;
313                 }
314                 case SIOCSIWFRAG:
315                 {
316                         if (wl_ioctl(dev,WLC_SET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
317                                 return -EINVAL;
318                         break;
319                 }
320                 case SIOCGIWTXPOW:
321                 {
322                         wrqu->txpower.value = 0;
323                         if (wl_ioctl(dev,WLC_GET_TXPWR, &(wrqu->txpower.value), sizeof(int)) < 0)
324                                 return -EINVAL;
325                         wrqu->txpower.fixed = 0;
326                         wrqu->txpower.disabled = 0;
327                         wrqu->txpower.flags = IW_TXPOW_MWATT;
328                         break;
329                 }
330                 case SIOCSIWTXPOW:
331                 {
332                         if (wrqu->txpower.flags != IW_TXPOW_MWATT)
333                                 return -EINVAL;
334
335                         if (wl_ioctl(dev, WLC_SET_TXPWR, &wrqu->txpower.value, sizeof(int)) < 0)
336                                 return -EINVAL;
337                 }
338                 case SIOCGIWENCODE:
339                 {
340                         wrqu->data.flags = IW_ENCODE_DISABLED;
341                         break;
342                 }
343                 case SIOCGIWRANGE:
344                 {
345                         return wlcompat_ioctl_getiwrange(dev, extra);
346                         break;
347                 }
348                 case SIOCSIWMODE:
349                 {
350                         int ap = -1, infra = -1, passive = 0, wet = 0;
351                         
352                         switch (wrqu->mode) {
353                                 case IW_MODE_MONITOR:
354                                         passive = 1;
355                                         break;
356                                 case IW_MODE_ADHOC:
357                                         infra = 0;
358                                         ap = 0;
359                                         break;
360                                 case IW_MODE_MASTER:
361                                         infra = 1;
362                                         ap = 1;
363                                         break;
364                                 case IW_MODE_INFRA:
365                                         infra = 1;
366                                         ap = 0;
367                                         break;
368                                 case IW_MODE_REPEAT:
369                                         infra = 1;
370                                         ap = 0;
371                                         wet = 1;
372                                         break;
373                                 default:
374                                         return -EINVAL;
375                         }
376                         
377                         if (wl_ioctl(dev, WLC_SET_PASSIVE, &passive, sizeof(passive)) < 0)
378                                 return -EINVAL;
379                         if (wl_ioctl(dev, WLC_SET_MONITOR, &passive, sizeof(passive)) < 0)
380                                 return -EINVAL;
381                         if (wl_ioctl(dev, WLC_SET_WET, &wet, sizeof(wet)) < 0)
382                                 return -EINVAL;
383                         if (ap >= 0)
384                                 if (wl_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap)) < 0)
385                                         return -EINVAL;
386                         if (infra >= 0)
387                                 if (wl_ioctl(dev, WLC_SET_INFRA, &infra, sizeof(infra)) < 0)
388                                         return -EINVAL;
389
390                         break;
391                                                 
392                 }
393                 case SIOCGIWMODE:
394                 {
395                         int ap, infra, wet, passive;
396
397                         if (wl_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap)) < 0)
398                                 return -EINVAL;
399                         if (wl_ioctl(dev, WLC_GET_INFRA, &infra, sizeof(infra)) < 0)
400                                 return -EINVAL;
401                         if (wl_ioctl(dev, WLC_GET_PASSIVE, &passive, sizeof(passive)) < 0)
402                                 return -EINVAL;
403                         if (wl_ioctl(dev, WLC_GET_WET, &wet, sizeof(wet)) < 0)
404                                 return -EINVAL;
405
406                         if (passive) {
407                                 wrqu->mode = IW_MODE_MONITOR;
408                         } else if (!infra) {
409                                 wrqu->mode = IW_MODE_ADHOC;
410                         } else {
411                                 if (ap) {
412                                         wrqu->mode = IW_MODE_MASTER;
413                                 } else {
414                                         if (wet) {
415                                                 wrqu->mode = IW_MODE_REPEAT;
416                                         } else {
417                                                 wrqu->mode = IW_MODE_INFRA;
418                                         }
419                                 }
420                         }
421                         break;
422                 }
423                 default:
424                 {
425                         return -EINVAL;
426                 }
427         }
428         
429         return 0;
430 }
431
432 static const iw_handler  wlcompat_handler[] = {
433         NULL,                   /* SIOCSIWCOMMIT */
434         wlcompat_ioctl,         /* SIOCGIWNAME */
435         NULL,                   /* SIOCSIWNWID */
436         NULL,                   /* SIOCGIWNWID */
437         wlcompat_ioctl,         /* SIOCSIWFREQ */
438         wlcompat_ioctl,         /* SIOCGIWFREQ */
439         wlcompat_ioctl,         /* SIOCSIWMODE */
440         wlcompat_ioctl,         /* SIOCGIWMODE */
441         NULL,                   /* SIOCSIWSENS */
442         NULL,                   /* SIOCGIWSENS */
443         NULL,                   /* SIOCSIWRANGE, unused */
444         wlcompat_ioctl,         /* SIOCGIWRANGE */
445         NULL,                   /* SIOCSIWPRIV */
446         NULL,                   /* SIOCGIWPRIV */
447         NULL,                   /* SIOCSIWSTATS */
448         NULL,                   /* SIOCGIWSTATS */
449         iw_handler_set_spy,     /* SIOCSIWSPY */
450         iw_handler_get_spy,     /* SIOCGIWSPY */
451         iw_handler_set_thrspy,  /* SIOCSIWTHRSPY */
452         iw_handler_get_thrspy,  /* SIOCGIWTHRSPY */
453         NULL,                   /* SIOCSIWAP */
454         wlcompat_ioctl,         /* SIOCGIWAP */
455         NULL,                   /* -- hole -- */
456         NULL,                   /* SIOCGIWAPLIST */
457         wlcompat_set_scan,      /* SIOCSIWSCAN */
458         wlcompat_get_scan,      /* SIOCGIWSCAN */
459         wlcompat_ioctl,         /* SIOCSIWESSID */
460         wlcompat_ioctl,         /* SIOCGIWESSID */
461         NULL,                   /* SIOCSIWNICKN */
462         NULL,                   /* SIOCGIWNICKN */
463         NULL,                   /* -- hole -- */
464         NULL,                   /* -- hole -- */
465         NULL,                   /* SIOCSIWRATE */
466         NULL,                   /* SIOCGIWRATE */
467         wlcompat_ioctl,         /* SIOCSIWRTS */
468         wlcompat_ioctl,         /* SIOCGIWRTS */
469         wlcompat_ioctl,         /* SIOCSIWFRAG */
470         wlcompat_ioctl,         /* SIOCGIWFRAG */
471         wlcompat_ioctl,         /* SIOCSIWTXPOW */
472         wlcompat_ioctl,         /* SIOCGIWTXPOW */
473         NULL,                   /* SIOCSIWRETRY */
474         NULL,                   /* SIOCGIWRETRY */
475         NULL,                   /* SIOCSIWENCODE */
476         wlcompat_ioctl,         /* SIOCGIWENCODE */
477 };
478
479 static const struct iw_handler_def      wlcompat_handler_def =
480 {
481         .standard       = (iw_handler *) wlcompat_handler,
482         .num_standard   = sizeof(wlcompat_handler)/sizeof(iw_handler),
483         .private        = NULL,
484         .num_private    = 0,
485         .private_args   = NULL, 
486         .num_private_args = 0,
487 };
488
489 #ifdef DEBUG
490 static int (*old_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
491 static int new_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) {
492         int ret = old_ioctl(dev,ifr,cmd);
493         printk("dev: %s ioctl: 0x%04x\n",dev->name,cmd);
494         if (cmd==SIOCDEVPRIVATE) {
495                 int x;
496                 wl_ioctl_t *ioc = (wl_ioctl_t *)ifr->ifr_data;
497                 unsigned char *buf = ioc->buf;
498                 printk("   cmd: %d buf: 0x%08x len: %d\n",ioc->cmd,&(ioc->buf),ioc->len);
499                 printk("   ->");
500                 for (x=0;x<ioc->len && x<128 ;x++) {
501                         printk("%02X",buf[x]);
502                 }
503                 printk("\n");
504         }
505         return ret;
506 }
507 #endif
508
509 static int __init wlcompat_init()
510 {
511         dev = dev_get_by_name("eth1");
512 #ifdef DEBUG
513         old_ioctl = dev->do_ioctl;
514         dev->do_ioctl = new_ioctl;
515 #endif
516         dev->wireless_handlers = (struct iw_handler_def *)&wlcompat_handler_def;
517         return 0;
518 }
519
520 static void __exit wlcompat_exit()
521 {
522         dev->wireless_handlers = NULL;
523 #ifdef DEBUG
524         dev->do_ioctl = old_ioctl;
525 #endif
526         return;
527 }
528
529 EXPORT_NO_SYMBOLS;
530 MODULE_AUTHOR("openwrt.org");
531 MODULE_LICENSE("GPL");
532
533 module_init(wlcompat_init);
534 module_exit(wlcompat_exit);