fix noise/signal level display (fixes #20)
[openwrt.git] / target / linux / package / wlcompat / wlcompat.c
1 /*
2  * wlcompat.c
3  *
4  * Copyright (C) 2005 Mike Baker,
5  *                    Felix Fietkau <openwrt@nbd.name>
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 static struct net_device *dev;
36 static unsigned short bss_force;
37 static struct iw_statistics wstats;
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 typedef struct internal_wsec_key {
48         uint8 index;            // 0x00
49         uint8 unknown_1;        // 0x01
50         uint8 type;             // 0x02
51         uint8 unknown_2[7];     // 0x03
52         uint8 len;              // 0x0a
53         uint8 pad[3];
54         char data[32];          // 0x0e
55 } wkey;
56
57
58 static int wlcompat_private_ioctl(struct net_device *dev,
59                          struct iw_request_info *info,
60                          union iwreq_data *wrqu,
61                          char *extra);
62 #ifdef DEBUG
63 void print_buffer(int len, unsigned char *buf);
64 #endif
65
66 static int wl_ioctl(struct net_device *dev, int cmd, void *buf, int len)
67 {
68         mm_segment_t old_fs = get_fs();
69         struct ifreq ifr;
70         int ret;
71         wl_ioctl_t ioc;
72         ioc.cmd = cmd;
73         ioc.buf = buf;
74         ioc.len = len;
75         strncpy(ifr.ifr_name, dev->name, IFNAMSIZ);
76         ifr.ifr_data = (caddr_t) &ioc;
77         set_fs(KERNEL_DS);
78         ret = dev->do_ioctl(dev,&ifr,SIOCDEVPRIVATE);
79         set_fs (old_fs);
80         return ret;
81 }
82
83 static int wl_set_val(struct net_device *dev, char *var, void *val, int len)
84 {
85         char buf[128];
86         int buf_len;
87         int ret;
88
89         /* check for overflow */
90         if ((buf_len = strlen(var)) + 1 + len > sizeof(buf))
91                 return -1;
92         
93         strcpy(buf, var);
94         buf_len += 1;
95
96         /* append int value onto the end of the name string */
97         memcpy(&(buf[buf_len]), val, len);
98         buf_len += len;
99
100         ret = wl_ioctl(dev, WLC_SET_VAR, buf, buf_len);
101         return ret;
102 }
103
104 static int wl_get_val(struct net_device *dev, char *var, void *val, int len)
105 {
106         char buf[128];
107         int buf_len;
108         int ret;
109
110         /* check for overflow */
111         if ((buf_len = strlen(var)) + 1 > sizeof(buf) || len > sizeof(buf))
112                 return -1;
113         
114         strcpy(buf, var);
115         if (ret = wl_ioctl(dev, WLC_GET_VAR, buf, buf_len + len))
116                 return ret;
117
118         memcpy(val, buf, len);
119         return 0;
120 }
121
122 int get_primary_key(struct net_device *dev)
123 {
124         int key, val;
125         
126         for (key = val = 0; (key < 4) && (val == 0); key++) {
127                 val = key;
128                 if (wl_ioctl(dev, WLC_GET_KEY_PRIMARY, &val, sizeof(val)) < 0)
129                         return -EINVAL;
130         }
131         return key;
132 }
133
134
135 static int wlcompat_ioctl_getiwrange(struct net_device *dev,
136                                     char *extra)
137 {
138         int i, k;
139         struct iw_range *range;
140
141         range = (struct iw_range *) extra;
142
143         range->we_version_compiled = WIRELESS_EXT;
144         range->we_version_source = WIRELESS_EXT;
145         
146         range->min_nwid = range->max_nwid = 0;
147         
148         range->num_channels = NUM_CHANNELS;
149         k = 0;
150         for (i = 0; i < NUM_CHANNELS; i++) {
151                 range->freq[k].i = i + 1;
152                 range->freq[k].m = channel_frequency[i] * 100000;
153                 range->freq[k].e = 1;
154                 k++;
155                 if (k >= IW_MAX_FREQUENCIES)
156                         break;
157         }
158         range->num_frequency = k;
159         range->sensitivity = 3;
160
161         /* nbd: don't know what this means, but other drivers set it this way */
162         range->pmp_flags = IW_POWER_PERIOD;
163         range->pmt_flags = IW_POWER_TIMEOUT;
164         range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
165
166         range->min_pmp = 0;
167         range->max_pmp = 65535000;
168         range->min_pmt = 0;
169         range->max_pmt = 65535 * 1000;
170
171         range->max_qual.qual = 0;
172         range->max_qual.level = 100;
173         range->max_qual.noise = 100;
174         
175         range->min_rts = 0;
176         if (wl_ioctl(dev, WLC_GET_RTS, &range->max_rts, sizeof(int)) < 0)
177                 range->max_rts = 2347;
178
179         range->min_frag = 256;
180         
181         if (wl_ioctl(dev, WLC_GET_FRAG, &range->max_frag, sizeof(int)) < 0)
182                 range->max_frag = 2346;
183
184         range->txpower_capa = IW_TXPOW_DBM;
185
186         return 0;
187 }
188
189
190 static int wlcompat_set_scan(struct net_device *dev,
191                          struct iw_request_info *info,
192                          union iwreq_data *wrqu,
193                          char *extra)
194 {
195         int ap = 0, oldap = 0;
196         wl_scan_params_t params;
197
198         memset(&params, 0, sizeof(params));
199
200         /* use defaults (same parameters as wl scan) */
201         memset(&params.bssid, 0xff, sizeof(params.bssid));
202         params.bss_type = DOT11_BSSTYPE_ANY;
203         params.scan_type = -1;
204         params.nprobes = -1;
205         params.active_time = -1;
206         params.passive_time = -1;
207         params.home_time = -1;
208         
209         /* can only scan in STA mode */
210         wl_ioctl(dev, WLC_GET_AP, &oldap, sizeof(oldap));
211         if (oldap > 0)
212                 wl_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap));
213         
214         if (wl_ioctl(dev, WLC_SCAN, &params, 64) < 0)
215                 return -EINVAL;
216         
217         if (oldap > 0)
218                 wl_ioctl(dev, WLC_SET_AP, &oldap, sizeof(oldap));
219
220         return 0;
221 }
222
223
224 struct iw_statistics *wlcompat_get_wireless_stats(struct net_device *dev)
225 {
226         wl_bss_info_t *bss_info = (wl_bss_info_t *) buf;
227         get_pktcnt_t pkt;
228         int rssi, noise, ap;
229         
230         memset(&wstats, 0, sizeof(wstats));
231         memset(&pkt, 0, sizeof(pkt));
232         memset(buf, 0, sizeof(buf));
233         bss_info->version = 0x2000;
234         wl_ioctl(dev, WLC_GET_BSS_INFO, bss_info, WLC_IOCTL_MAXLEN);
235         wl_ioctl(dev, WLC_GET_PKTCNTS, &pkt, sizeof(pkt));
236
237         wl_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap));
238         if (!ap) {
239                 // somehow the structure doesn't fit here
240                 rssi = buf[82];
241                 noise = buf[84];
242         } else {
243                 noise = 0;
244                 rssi = 0;
245         }
246
247         wstats.qual.level = rssi;
248         wstats.qual.noise = noise;
249         wstats.discard.misc = pkt.rx_bad_pkt;
250         wstats.discard.retries = pkt.tx_bad_pkt;
251
252         return &wstats;
253 }
254
255 static int wlcompat_get_scan(struct net_device *dev,
256                          struct iw_request_info *info,
257                          union iwreq_data *wrqu,
258                          char *extra)
259 {
260         wl_scan_results_t *results = (wl_scan_results_t *) buf;
261         wl_bss_info_t *bss_info;
262         char *info_ptr;
263         char *current_ev = extra;
264         char *current_val;
265         char *end_buf = extra + IW_SCAN_MAX_DATA;
266         struct iw_event iwe;
267         int i, j;
268         int rssi, noise;
269         
270         results->buflen = WLC_IOCTL_MAXLEN - sizeof(wl_scan_results_t);
271         
272         if (wl_ioctl(dev, WLC_SCAN_RESULTS, buf, WLC_IOCTL_MAXLEN) < 0)
273                 return -EAGAIN;
274         
275         bss_info = &(results->bss_info[0]);
276         info_ptr = (char *) bss_info;
277         for (i = 0; i < results->count; i++) {
278
279                 /* send the cell address (must be sent first) */
280                 iwe.cmd = SIOCGIWAP;
281                 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
282                 memcpy(&iwe.u.ap_addr.sa_data, &bss_info->BSSID, sizeof(bss_info->BSSID));
283                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_ADDR_LEN);
284                 
285                 /* send the ESSID */
286                 iwe.cmd = SIOCGIWESSID;
287                 iwe.u.data.length = bss_info->SSID_len;
288                 if (iwe.u.data.length > IW_ESSID_MAX_SIZE)
289                         iwe.u.data.length = IW_ESSID_MAX_SIZE;
290                 iwe.u.data.flags = 1;
291                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, bss_info->SSID);
292
293                 /* send frequency/channel info */
294                 iwe.cmd = SIOCGIWFREQ;
295                 iwe.u.freq.e = 0;
296                 iwe.u.freq.m = bss_info->channel;
297                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_FREQ_LEN);
298
299                 /* add quality statistics */
300                 iwe.cmd = IWEVQUAL;
301                 iwe.u.qual.qual = 0;
302                 iwe.u.qual.level = bss_info->RSSI;
303                 iwe.u.qual.noise = bss_info->phy_noise;
304                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);
305         
306                 /* send rate information */
307                 iwe.cmd = SIOCGIWRATE;
308                 current_val = current_ev + IW_EV_LCP_LEN;
309                 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
310                 
311                 for(j = 0 ; j < bss_info->rateset.count ; j++) {
312                         iwe.u.bitrate.value = ((bss_info->rateset.rates[j] & 0x7f) * 500000);
313                         current_val = iwe_stream_add_value(current_ev, current_val, end_buf, &iwe, IW_EV_PARAM_LEN);
314                 }
315                 if((current_val - current_ev) > IW_EV_LCP_LEN)
316                         current_ev = current_val;
317
318                 info_ptr += sizeof(wl_bss_info_t);
319                 if (bss_info->ie_length % 4)
320                         info_ptr += bss_info->ie_length + 4 - (bss_info->ie_length % 4);
321                 else
322                         info_ptr += bss_info->ie_length;
323                 bss_info = (wl_bss_info_t *) info_ptr;
324         }
325         
326         wrqu->data.length = (current_ev - extra);
327         wrqu->data.flags = 0;
328
329         return 0;
330 }
331
332 static int wlcompat_ioctl(struct net_device *dev,
333                          struct iw_request_info *info,
334                          union iwreq_data *wrqu,
335                          char *extra)
336 {
337         switch (info->cmd) {
338                 case SIOCGIWNAME:
339                         strcpy(wrqu->name, "IEEE 802.11-DS");
340                         break;
341                 case SIOCGIWFREQ:
342                 {
343                         channel_info_t ci;
344
345                         if (wl_ioctl(dev,WLC_GET_CHANNEL, &ci, sizeof(ci)) < 0)
346                                 return -EINVAL;
347
348                         wrqu->freq.m = ci.target_channel;
349                         wrqu->freq.e = 0;
350                         break;
351                 }
352                 case SIOCSIWFREQ:
353                 {
354                         if (wrqu->freq.m == -1) {
355                                 wrqu->freq.m = 0;
356                                 if (wl_ioctl(dev, WLC_SET_CHANNEL, &wrqu->freq.m, sizeof(int)) < 0)
357                                         return -EINVAL;
358                         } else {
359                                 if (wrqu->freq.e == 1) {
360                                         int channel = 0;
361                                         int f = wrqu->freq.m / 100000;
362                                         while ((channel < NUM_CHANNELS + 1) && (f != channel_frequency[channel]))
363                                                 channel++;
364                                         
365                                         if (channel == NUM_CHANNELS) // channel not found
366                                                 return -EINVAL;
367
368                                         wrqu->freq.e = 0;
369                                         wrqu->freq.m = channel + 1;
370                                 }
371                                 if ((wrqu->freq.e == 0) && (wrqu->freq.m < 1000)) {
372                                         if (wl_ioctl(dev, WLC_SET_CHANNEL, &wrqu->freq.m, sizeof(int)) < 0)
373                                                 return -EINVAL;
374                                 } else {
375                                         return -EINVAL;
376                                 }
377                         }
378                         break;
379                 }
380                 case SIOCSIWAP:
381                 {
382                         int ap = 0;
383                         int infra = 0;
384                         rw_reg_t reg;
385
386                         memset(&reg, 0, sizeof(reg));
387
388                         if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
389                                 return -EINVAL;
390
391                         if (wl_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap)) < 0)
392                                 return -EINVAL;
393
394                         if (wl_ioctl(dev, WLC_GET_INFRA, &infra, sizeof(infra)) < 0)
395                                 return -EINVAL;
396
397                         if (!infra) {
398                                 wl_ioctl(dev, WLC_SET_BSSID, wrqu->ap_addr.sa_data, 6);
399
400                                 reg.size = 4;
401                                 reg.byteoff = 0x184;
402                                 reg.val = bss_force << 16 | bss_force;
403                                 wl_ioctl(dev, WLC_W_REG, &reg, sizeof(reg));
404                                 
405                                 reg.byteoff = 0x180;
406                                 wl_ioctl(dev, WLC_R_REG, &reg, sizeof(reg));
407                                 reg.val = bss_force << 16;
408                                 wl_ioctl(dev, WLC_W_REG, &reg, sizeof(reg));
409                         }
410
411                         if (wl_ioctl(dev, ((ap || !infra) ? WLC_SET_BSSID : WLC_REASSOC), wrqu->ap_addr.sa_data, 6) < 0)
412                                 return -EINVAL;
413
414                         break;
415                 }
416                 case SIOCGIWAP:
417                 {
418 #ifdef DEBUG
419                         rw_reg_t reg;
420                         memset(&reg, 0, sizeof(reg));
421
422                         reg.size = 4;
423                         reg.byteoff = 0x184;
424                         wl_ioctl(dev, WLC_R_REG, &reg, sizeof(reg));
425                         printk("bss time = 0x%08x", reg.val);
426                         
427                         reg.byteoff = 0x180;
428                         wl_ioctl(dev, WLC_R_REG, &reg, sizeof(reg));
429                         printk("%08x\n", reg.val);
430 #endif
431                         
432                         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
433                         if (wl_ioctl(dev,WLC_GET_BSSID,wrqu->ap_addr.sa_data,6) < 0)
434                                 return -EINVAL;
435                         break;
436                 }
437                 case SIOCGIWESSID:
438                 {
439                         wlc_ssid_t ssid;
440                         
441                         if (wl_ioctl(dev,WLC_GET_SSID, &ssid, sizeof(wlc_ssid_t)) < 0)
442                                 return -EINVAL;
443
444                         wrqu->essid.flags = wrqu->data.flags = 1;
445                         wrqu->essid.length = wrqu->data.length = ssid.SSID_len + 1;
446                         memcpy(extra,ssid.SSID,ssid.SSID_len + 1);
447                         break;
448                 }
449                 case SIOCSIWESSID:
450                 {
451                         wlc_ssid_t ssid;
452                         memset(&ssid, 0, sizeof(ssid));
453                         ssid.SSID_len = strlen(extra);
454                         if (ssid.SSID_len > WLC_ESSID_MAX_SIZE)
455                                 ssid.SSID_len = WLC_ESSID_MAX_SIZE;
456                         memcpy(ssid.SSID, extra, ssid.SSID_len);
457                         if (wl_ioctl(dev, WLC_SET_SSID, &ssid, sizeof(ssid)) < 0)
458                                 return -EINVAL;
459                         break;
460                 }
461                 case SIOCGIWRTS:
462                 {
463                         if (wl_ioctl(dev,WLC_GET_RTS,&(wrqu->rts.value),sizeof(int)) < 0) 
464                                 return -EINVAL;
465                         break;
466                 }
467                 case SIOCSIWRTS:
468                 {
469                         if (wl_ioctl(dev,WLC_SET_RTS,&(wrqu->rts.value),sizeof(int)) < 0) 
470                                 return -EINVAL;
471                         break;
472                 }
473                 case SIOCGIWFRAG:
474                 {
475                         if (wl_ioctl(dev,WLC_GET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
476                                 return -EINVAL;
477                         break;
478                 }
479                 case SIOCSIWFRAG:
480                 {
481                         if (wl_ioctl(dev,WLC_SET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
482                                 return -EINVAL;
483                         break;
484                 }
485                 case SIOCGIWTXPOW:
486                 {
487                         int radio;
488
489                         wl_ioctl(dev, WLC_GET_RADIO, &radio, sizeof(int));
490                         
491                         if (wl_get_val(dev, "qtxpower", &(wrqu->txpower.value), sizeof(int)) < 0)
492                                 return -EINVAL;
493                         
494                         wrqu->txpower.value &= ~WL_TXPWR_OVERRIDE;
495                         wrqu->txpower.value /= 4;
496                                 
497                         wrqu->txpower.fixed = 0;
498                         wrqu->txpower.disabled = radio;
499                         wrqu->txpower.flags = IW_TXPOW_DBM;
500                         break;
501                 }
502                 case SIOCSIWTXPOW:
503                 {
504                         /* This is weird: WLC_SET_RADIO with 1 as argument disables the radio */
505                         int radio = wrqu->txpower.disabled;
506
507                         wl_ioctl(dev, WLC_SET_RADIO, &radio, sizeof(int));
508                         
509                         if (!wrqu->txpower.disabled && (wrqu->txpower.value > 0)) {
510                                 int value;
511                                 
512                                 if (wl_get_val(dev, "qtxpower", &value, sizeof(int)) < 0)
513                                         return -EINVAL;
514                                 
515                                 value &= WL_TXPWR_OVERRIDE;
516                                 wrqu->txpower.value *= 4;
517                                 wrqu->txpower.value |= value;
518                                 
519                                 if (wrqu->txpower.flags != IW_TXPOW_DBM)
520                                         return -EINVAL;
521                                 
522                                 if (wrqu->txpower.value > 0)
523                                         if (wl_set_val(dev, "qtxpower", &(wrqu->txpower.value), sizeof(int)) < 0)
524                                                 return -EINVAL;
525                         }
526                         break;
527                 }
528                 case SIOCSIWENCODE:
529                 {
530                         int val = 0, wep = 1, wrestrict = 1;
531                         int index = (wrqu->data.flags & IW_ENCODE_INDEX) - 1;
532
533                         if (index < 0)
534                                 index = get_primary_key(dev);
535                         
536                         if (wrqu->data.flags & IW_ENCODE_DISABLED) {
537                                 wep = 0;
538                                 if (wl_ioctl(dev, WLC_SET_WSEC, &wep, sizeof(val)) < 0)
539                                         return -EINVAL;
540                                 return 0;
541                         }
542
543                         if (wl_ioctl(dev, WLC_SET_WSEC, &wep, sizeof(val)) < 0)
544                                 return -EINVAL;
545
546                         if (wrqu->data.flags & IW_ENCODE_OPEN)
547                                 wrestrict = 0;
548                         
549                         if (wrqu->data.pointer && (wrqu->data.length > 0) && (wrqu->data.length <= 16)) {
550                                 wl_wsec_key_t key;
551                                 memset(&key, 0, sizeof(key));
552
553                                 key.flags = WL_PRIMARY_KEY;
554                                 key.len = wrqu->data.length;
555                                 key.index = index;
556                                 memcpy(key.data, wrqu->data.pointer, wrqu->data.length);
557
558                                 if (wl_ioctl(dev, WLC_SET_KEY, &key, sizeof(key)) < 0)
559                                         return -EINVAL;
560                         }
561
562                         if (index >= 0)
563                                 wl_ioctl(dev, WLC_SET_KEY_PRIMARY, &index, sizeof(index));
564                         
565                         if (wrestrict >= 0)
566                                 wl_ioctl(dev, WLC_SET_WEP_RESTRICT, &wrestrict, sizeof(wrestrict));
567
568                         break;
569                 }
570                 case SIOCGIWENCODE:
571                 {
572                         int val;
573                         
574                         if (wl_ioctl(dev, WLC_GET_WEP, &val, sizeof(val)) < 0)
575                                 return -EINVAL;
576                         
577
578                         if (val > 0) {
579                                 int key = get_primary_key(dev);
580                                 
581                                 wrqu->data.flags = IW_ENCODE_ENABLED;
582                                 if (key-- > 0) {
583                                         int *info_addr; 
584                                         wkey *wep_key;
585                                         
586                                         info_addr = (int *) dev->priv;
587                                         wep_key = (wkey *) ((*info_addr) + 0x2752 + (key * 0x110));
588                                         
589                                         wrqu->data.flags |= key + 1;
590                                         wrqu->data.length = wep_key->len;
591
592                                         memset(extra, 0, 16);
593                                         memcpy(extra, wep_key->data, 16);
594                                 } else {
595                                         wrqu->data.flags |= IW_ENCODE_NOKEY;
596                                 }
597                         } else {
598                                 wrqu->data.flags = IW_ENCODE_DISABLED;
599                         }
600                         
601                         break;
602                 }
603                 case SIOCGIWRANGE:
604                 {
605                         return wlcompat_ioctl_getiwrange(dev, extra);
606                         break;
607                 }
608                 case SIOCSIWMODE:
609                 {
610                         int ap = -1, infra = -1, passive = 0, wet = 0;
611                         
612                         switch (wrqu->mode) {
613                                 case IW_MODE_MONITOR:
614                                         passive = 1;
615                                         break;
616                                 case IW_MODE_ADHOC:
617                                         infra = 0;
618                                         ap = 0;
619                                         break;
620                                 case IW_MODE_MASTER:
621                                         infra = 1;
622                                         ap = 1;
623                                         break;
624                                 case IW_MODE_INFRA:
625                                         infra = 1;
626                                         ap = 0;
627                                         break;
628                                 case IW_MODE_REPEAT:
629                                         infra = 1;
630                                         ap = 0;
631                                         wet = 1;
632                                         break;
633                                         
634                                 default:
635                                         return -EINVAL;
636                         }
637                         
638                         wl_ioctl(dev, WLC_SET_PASSIVE, &passive, sizeof(passive));
639                         wl_ioctl(dev, WLC_SET_MONITOR, &passive, sizeof(passive));
640                         wl_ioctl(dev, WLC_SET_WET, &wet, sizeof(wet));
641                         if (ap >= 0) 
642                                 wl_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap));
643                         if (infra >= 0)
644                                 wl_ioctl(dev, WLC_SET_INFRA, &infra, sizeof(infra));
645
646                         break;
647                                                 
648                 }
649                 case SIOCGIWMODE:
650                 {
651                         int ap, infra, wet, passive;
652
653                         if (wl_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap)) < 0)
654                                 return -EINVAL;
655                         if (wl_ioctl(dev, WLC_GET_INFRA, &infra, sizeof(infra)) < 0)
656                                 return -EINVAL;
657                         if (wl_ioctl(dev, WLC_GET_PASSIVE, &passive, sizeof(passive)) < 0)
658                                 return -EINVAL;
659                         if (wl_ioctl(dev, WLC_GET_WET, &wet, sizeof(wet)) < 0)
660                                 return -EINVAL;
661
662                         if (passive) {
663                                 wrqu->mode = IW_MODE_MONITOR;
664                         } else if (!infra) {
665                                 wrqu->mode = IW_MODE_ADHOC;
666                         } else {
667                                 if (ap) {
668                                         wrqu->mode = IW_MODE_MASTER;
669                                 } else {
670                                         if (wet) {
671                                                 wrqu->mode = IW_MODE_REPEAT;
672                                         } else {
673                                                 wrqu->mode = IW_MODE_INFRA;
674                                         }
675                                 }
676                         }
677                         break;
678                 }
679                 default:
680                 {
681                         if (info->cmd >= SIOCIWFIRSTPRIV)
682                                 return wlcompat_private_ioctl(dev, info, wrqu, extra);
683
684                         return -EINVAL;
685                 }
686         }
687         
688         return 0;
689 }
690
691 static const iw_handler  wlcompat_handler[] = {
692         NULL,                   /* SIOCSIWCOMMIT */
693         wlcompat_ioctl,         /* SIOCGIWNAME */
694         NULL,                   /* SIOCSIWNWID */
695         NULL,                   /* SIOCGIWNWID */
696         wlcompat_ioctl,         /* SIOCSIWFREQ */
697         wlcompat_ioctl,         /* SIOCGIWFREQ */
698         wlcompat_ioctl,         /* SIOCSIWMODE */
699         wlcompat_ioctl,         /* SIOCGIWMODE */
700         NULL,                   /* SIOCSIWSENS */
701         NULL,                   /* SIOCGIWSENS */
702         NULL,                   /* SIOCSIWRANGE, unused */
703         wlcompat_ioctl,         /* SIOCGIWRANGE */
704         NULL,                   /* SIOCSIWPRIV */
705         NULL,                   /* SIOCGIWPRIV */
706         NULL,                   /* SIOCSIWSTATS */
707         NULL,                   /* SIOCGIWSTATS */
708         iw_handler_set_spy,     /* SIOCSIWSPY */
709         iw_handler_get_spy,     /* SIOCGIWSPY */
710         iw_handler_set_thrspy,  /* SIOCSIWTHRSPY */
711         iw_handler_get_thrspy,  /* SIOCGIWTHRSPY */
712         wlcompat_ioctl,         /* SIOCSIWAP */
713         wlcompat_ioctl,         /* SIOCGIWAP */
714         NULL,                   /* -- hole -- */
715         NULL,                   /* SIOCGIWAPLIST */
716         wlcompat_set_scan,      /* SIOCSIWSCAN */
717         wlcompat_get_scan,      /* SIOCGIWSCAN */
718         wlcompat_ioctl,         /* SIOCSIWESSID */
719         wlcompat_ioctl,         /* SIOCGIWESSID */
720         NULL,                   /* SIOCSIWNICKN */
721         NULL,                   /* SIOCGIWNICKN */
722         NULL,                   /* -- hole -- */
723         NULL,                   /* -- hole -- */
724         NULL,                   /* SIOCSIWRATE */
725         NULL,                   /* SIOCGIWRATE */
726         wlcompat_ioctl,         /* SIOCSIWRTS */
727         wlcompat_ioctl,         /* SIOCGIWRTS */
728         wlcompat_ioctl,         /* SIOCSIWFRAG */
729         wlcompat_ioctl,         /* SIOCGIWFRAG */
730         wlcompat_ioctl,         /* SIOCSIWTXPOW */
731         wlcompat_ioctl,         /* SIOCGIWTXPOW */
732         NULL,                   /* SIOCSIWRETRY */
733         NULL,                   /* SIOCGIWRETRY */
734         wlcompat_ioctl,         /* SIOCSIWENCODE */
735         wlcompat_ioctl,         /* SIOCGIWENCODE */
736 };
737
738
739 #define WLCOMPAT_SET_MONITOR            SIOCIWFIRSTPRIV + 0
740 #define WLCOMPAT_GET_MONITOR            SIOCIWFIRSTPRIV + 1
741 #define WLCOMPAT_SET_TXPWR_LIMIT        SIOCIWFIRSTPRIV + 2
742 #define WLCOMPAT_GET_TXPWR_LIMIT        SIOCIWFIRSTPRIV + 3
743 #define WLCOMPAT_SET_ANTDIV             SIOCIWFIRSTPRIV + 4
744 #define WLCOMPAT_GET_ANTDIV             SIOCIWFIRSTPRIV + 5
745 #define WLCOMPAT_SET_TXANT              SIOCIWFIRSTPRIV + 6
746 #define WLCOMPAT_GET_TXANT              SIOCIWFIRSTPRIV + 7
747 #define WLCOMPAT_SET_BSS_FORCE          SIOCIWFIRSTPRIV + 8
748 #define WLCOMPAT_GET_BSS_FORCE          SIOCIWFIRSTPRIV + 9
749
750
751 static int wlcompat_private_ioctl(struct net_device *dev,
752                          struct iw_request_info *info,
753                          union iwreq_data *wrqu,
754                          char *extra)
755 {
756         int *value = (int *) wrqu->name;
757
758         switch (info->cmd) {
759                 case WLCOMPAT_SET_MONITOR:
760                 {
761                         if (wl_ioctl(dev, WLC_SET_MONITOR, value, sizeof(int)) < 0)
762                                 return -EINVAL;
763
764                         break;
765                 }
766                 case WLCOMPAT_GET_MONITOR:
767                 {
768                         if (wl_ioctl(dev, WLC_GET_MONITOR, extra, sizeof(int)) < 0)
769                                 return -EINVAL;
770
771                         break;
772                 }
773                 case WLCOMPAT_SET_TXPWR_LIMIT:
774                 {
775                         int val;
776                         
777
778                         if (wl_get_val(dev, "qtxpower", &val, sizeof(int)) < 0)
779                                 return -EINVAL;
780                         
781                         if (*extra > 0)
782                                 val |= WL_TXPWR_OVERRIDE;
783                         else
784                                 val &= ~WL_TXPWR_OVERRIDE;
785                         
786                         if (wl_set_val(dev, "qtxpower", &val, sizeof(int)) < 0)
787                                 return -EINVAL;
788                         
789                         break;
790                 }
791                 case WLCOMPAT_GET_TXPWR_LIMIT:
792                 {
793                         if (wl_get_val(dev, "qtxpower", value, sizeof(int)) < 0)
794                                 return -EINVAL;
795
796                         *value = ((*value & WL_TXPWR_OVERRIDE) == WL_TXPWR_OVERRIDE ? 1 : 0);
797
798                         break;
799                 }
800                 case WLCOMPAT_SET_ANTDIV:
801                 {
802                         if (wl_ioctl(dev, WLC_SET_ANTDIV, value, sizeof(int)) < 0)
803                                 return -EINVAL;
804
805                         break;
806                 }
807                 case WLCOMPAT_GET_ANTDIV:
808                 {
809                         if (wl_ioctl(dev, WLC_GET_ANTDIV, extra, sizeof(int)) < 0)
810                                 return -EINVAL;
811
812                         break;
813                 }
814                 case WLCOMPAT_SET_TXANT:
815                 {
816                         if (wl_ioctl(dev, WLC_SET_TXANT, value, sizeof(int)) < 0)
817                                 return -EINVAL;
818
819                         break;
820                 }
821                 case WLCOMPAT_GET_TXANT:
822                 {
823                         if (wl_ioctl(dev, WLC_GET_TXANT, extra, sizeof(int)) < 0)
824                                 return -EINVAL;
825
826                         break;
827                 }
828                 case WLCOMPAT_SET_BSS_FORCE:
829                 {
830                         bss_force = (unsigned short) *value;
831                         break;
832                 }
833                 case WLCOMPAT_GET_BSS_FORCE:
834                 {
835                         *extra = (int) bss_force;
836                         break;
837                 }
838                 default:
839                 {
840                         return -EINVAL;
841                 }
842                         
843         }
844         return 0;
845 }
846
847 static const struct iw_priv_args wlcompat_private_args[] = 
848 {
849         {       WLCOMPAT_SET_MONITOR, 
850                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
851                 0,
852                 "set_monitor"
853         },
854         {       WLCOMPAT_GET_MONITOR, 
855                 0,
856                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
857                 "get_monitor"
858         },
859         {       WLCOMPAT_SET_TXPWR_LIMIT, 
860                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
861                 0,
862                 "set_txpwr_force"
863         },
864         {       WLCOMPAT_GET_TXPWR_LIMIT, 
865                 0,
866                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
867                 "get_txpwr_force"
868         },
869         {       WLCOMPAT_SET_ANTDIV, 
870                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
871                 0,
872                 "set_antdiv"
873         },
874         {       WLCOMPAT_GET_ANTDIV, 
875                 0,
876                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
877                 "get_antdiv"
878         },
879         {       WLCOMPAT_SET_TXANT, 
880                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
881                 0,
882                 "set_txant"
883         },
884         {       WLCOMPAT_GET_TXANT, 
885                 0,
886                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
887                 "get_txant"
888         },
889         {       WLCOMPAT_SET_BSS_FORCE, 
890                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
891                 0,
892                 "set_bss_force"
893         },
894         {       WLCOMPAT_GET_BSS_FORCE, 
895                 0,
896                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
897                 "get_bss_force"
898         },
899 };
900
901 static const iw_handler wlcompat_private[] =
902 {
903         wlcompat_private_ioctl,
904         NULL
905 };
906
907
908 static const struct iw_handler_def wlcompat_handler_def =
909 {
910         .standard       = (iw_handler *) wlcompat_handler,
911         .num_standard   = sizeof(wlcompat_handler)/sizeof(iw_handler),
912         .private        = wlcompat_private,
913         .num_private    = 1,
914         .private_args   = wlcompat_private_args,
915         .num_private_args = sizeof(wlcompat_private_args) / sizeof(wlcompat_private_args[0])
916 };
917
918
919 #ifdef DEBUG
920 void print_buffer(int len, unsigned char *buf) {
921         int x;
922         if (buf != NULL) {
923                 for (x=0;x<len && x<180 ;x++) {
924                         if ((x % 4) == 0)
925                                 printk(" ");
926                         printk("%02X",buf[x]);
927                 }
928         } else {
929                 printk(" NULL");
930         }
931         printk("\n");
932
933 }
934 #endif
935 static int (*old_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
936 static int new_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) {
937         int ret = 0;
938         struct iwreq *iwr = (struct iwreq *) ifr;
939         struct iw_request_info info;
940         
941 #ifdef DEBUG
942         printk("dev: %s ioctl: 0x%04x\n",dev->name,cmd);
943 #endif
944
945         if (cmd >= SIOCIWFIRSTPRIV) {
946                 info.cmd = cmd;
947                 info.flags = 0;
948                 ret = wlcompat_private_ioctl(dev, &info, &(iwr->u), (char *) &(iwr->u));
949 #ifdef DEBUG
950         } else if (cmd==SIOCDEVPRIVATE) {
951                 wl_ioctl_t *ioc = (wl_ioctl_t *)ifr->ifr_data;
952                 unsigned char *buf = ioc->buf;
953                 printk("   cmd: %d buf: 0x%08x len: %d\n",ioc->cmd,&(ioc->buf),ioc->len);
954                 printk("   send: ->");
955                 print_buffer(ioc->len, buf);
956                 ret = old_ioctl(dev,ifr,cmd);
957                 printk("   recv: ->");
958                 print_buffer(ioc->len, buf);
959                 printk("   ret: %d\n", ret);
960 #endif
961         } else {
962                 ret = old_ioctl(dev,ifr,cmd);
963         }
964         return ret;
965 }
966
967 static int __init wlcompat_init()
968 {
969         int found = 0, i;
970         char *devname = "eth0";
971         bss_force = 0;
972         
973         while (!found && (dev = dev_get_by_name(devname))) {
974                 if ((dev->wireless_handlers == NULL) && ((wl_ioctl(dev, WLC_GET_MAGIC, &i, sizeof(i)) == 0) && i == WLC_IOCTL_MAGIC))
975                         found = 1;
976                 devname[3]++;
977         }
978         
979         if (!found) {
980                 printk("No Broadcom devices found.\n");
981                 return -ENODEV;
982         }
983                 
984
985         old_ioctl = dev->do_ioctl;
986         dev->do_ioctl = new_ioctl;
987         dev->wireless_handlers = (struct iw_handler_def *)&wlcompat_handler_def;
988         dev->get_wireless_stats = wlcompat_get_wireless_stats;
989 #ifdef DEBUG
990         printk("broadcom driver private data: 0x%08x\n", dev->priv);
991 #endif
992         return 0;
993 }
994
995 static void __exit wlcompat_exit()
996 {
997         dev->get_wireless_stats = NULL;
998         dev->wireless_handlers = NULL;
999         dev->do_ioctl = old_ioctl;
1000         return;
1001 }
1002
1003 EXPORT_NO_SYMBOLS;
1004 MODULE_AUTHOR("openwrt.org");
1005 MODULE_LICENSE("GPL");
1006
1007 module_init(wlcompat_init);
1008 module_exit(wlcompat_exit);