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