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