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