fix SIOCSIWFREQ
[openwrt.git] / package / openwrt / wlcompat.c
1 // insert header here
2 // mbm. gpl.
3
4 #include <linux/config.h>
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/if_arp.h>
8 #include <asm/uaccess.h>
9 #include <linux/wireless.h>
10
11 #include <net/iw_handler.h>
12 #include <wlioctl.h>
13
14 #define DEBUG
15
16 static struct net_device *dev;
17
18 /* The frequency of each channel in MHz */
19 const long channel_frequency[] = {
20 2412, 2417, 2422, 2427, 2432, 2437, 2442,
21 2447, 2452, 2457, 2462, 2467, 2472, 2484
22 };
23 #define NUM_CHANNELS ( sizeof(channel_frequency) / sizeof(channel_frequency[0]) )
24
25 static int wl_ioctl(struct net_device *dev, int cmd, void *buf, int len)
26 {
27 mm_segment_t old_fs = get_fs();
28 struct ifreq ifr;
29 int ret;
30 wl_ioctl_t ioc;
31 ioc.cmd = cmd;
32 ioc.buf = buf;
33 ioc.len = len;
34 strncpy(ifr.ifr_name, dev->name, IFNAMSIZ);
35 ifr.ifr_data = (caddr_t) &ioc;
36 set_fs(KERNEL_DS);
37 ret = dev->do_ioctl(dev,&ifr,SIOCDEVPRIVATE);
38 set_fs (old_fs);
39 return ret;
40 }
41
42 static int wlcompat_ioctl_getiwrange(struct net_device *dev,
43 char *extra)
44 {
45 int i, k;
46 struct iw_range *range;
47
48 range = (struct iw_range *) extra;
49
50 range->we_version_compiled = WIRELESS_EXT;
51 range->we_version_source = WIRELESS_EXT;
52
53 range->min_nwid = range->max_nwid = 0;
54
55 range->num_channels = NUM_CHANNELS;
56 k = 0;
57 for (i = 0; i < NUM_CHANNELS; i++) {
58 range->freq[k].i = i + 1;
59 range->freq[k].m = channel_frequency[i] * 100000;
60 range->freq[k].e = 1;
61 k++;
62 if (k >= IW_MAX_FREQUENCIES)
63 break;
64 }
65 range->num_frequency = k;
66 range->sensitivity = 3;
67
68 /* nbd: don't know what this means, but other drivers set it this way */
69 range->pmp_flags = IW_POWER_PERIOD;
70 range->pmt_flags = IW_POWER_TIMEOUT;
71 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
72
73 range->min_rts = 0;
74 if (wl_ioctl(dev, WLC_GET_RTS, &range->max_rts, sizeof(int)) < 0)
75 range->max_rts = 2347;
76
77 range->min_frag = 256;
78
79 if (wl_ioctl(dev, WLC_GET_FRAG, &range->max_frag, sizeof(int)) < 0)
80 range->max_frag = 2346;
81
82 range->min_pmp = 0;
83 range->max_pmp = 65535000;
84 range->min_pmt = 0;
85 range->max_pmt = 65535 * 1000;
86
87 range->txpower_capa = IW_TXPOW_MWATT;
88
89 return 0;
90 }
91
92 static int wlcompat_ioctl(struct net_device *dev,
93 struct iw_request_info *info,
94 union iwreq_data *wrqu,
95 char *extra)
96 {
97 switch (info->cmd) {
98 case SIOCGIWNAME:
99 strcpy(wrqu->name, "IEEE 802.11-DS");
100 break;
101 case SIOCGIWFREQ:
102 {
103 channel_info_t ci;
104
105 if (wl_ioctl(dev,WLC_GET_CHANNEL, &ci, sizeof(ci)) < 0)
106 return -EINVAL;
107
108 wrqu->freq.m = ci.target_channel;
109 wrqu->freq.e = 0;
110 break;
111 }
112 case SIOCSIWFREQ:
113 {
114 if (wrqu->freq.e == 1) {
115 int channel = 0;
116 int f = wrqu->freq.m / 100000;
117 while ((channel < NUM_CHANNELS + 1) && (f != channel_frequency[channel]))
118 channel++;
119
120 if (channel == NUM_CHANNELS) // channel not found
121 return -EINVAL;
122
123 wrqu->freq.e = 0;
124 wrqu->freq.m = channel + 1;
125 }
126 if ((wrqu->freq.e == 0) && (wrqu->freq.m < 1000)) {
127 if (wl_ioctl(dev, WLC_SET_CHANNEL, &wrqu->freq.m, sizeof(int)) < 0)
128 return -EINVAL;
129 } else {
130 return -EINVAL;
131 }
132 break;
133 }
134 case SIOCGIWAP:
135 {
136 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
137 if (wl_ioctl(dev,WLC_GET_BSSID,wrqu->ap_addr.sa_data,6) < 0)
138 return -EINVAL;
139 break;
140 }
141 case SIOCGIWESSID:
142 {
143 wlc_ssid_t ssid;
144
145 if (wl_ioctl(dev,WLC_GET_SSID, &ssid, sizeof(wlc_ssid_t)) < 0)
146 return -EINVAL;
147
148 wrqu->essid.flags = wrqu->data.flags = 1;
149 wrqu->essid.length = wrqu->data.length = ssid.SSID_len + 1;
150 memcpy(extra,ssid.SSID,ssid.SSID_len + 1);
151 break;
152 }
153 case SIOCSIWESSID:
154 {
155 wlc_ssid_t ssid;
156 memset(&ssid, 0, sizeof(ssid));
157 ssid.SSID_len = strlen(extra);
158 if (ssid.SSID_len > WLC_ESSID_MAX_SIZE)
159 ssid.SSID_len = WLC_ESSID_MAX_SIZE;
160 memcpy(ssid.SSID, extra, ssid.SSID_len);
161 if (wl_ioctl(dev, WLC_SET_SSID, &ssid, sizeof(ssid)) < 0)
162 return -EINVAL;
163 break;
164 }
165 case SIOCGIWRTS:
166 {
167 if (wl_ioctl(dev,WLC_GET_RTS,&(wrqu->rts.value),sizeof(int)) < 0)
168 return -EINVAL;
169 break;
170 }
171 case SIOCGIWFRAG:
172 {
173 if (wl_ioctl(dev,WLC_GET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
174 return -EINVAL;
175 break;
176 }
177 case SIOCGIWTXPOW:
178 {
179 wrqu->txpower.value = 0;
180 if (wl_ioctl(dev,WLC_GET_TXPWR, &(wrqu->txpower.value), sizeof(int)) < 0)
181 return -EINVAL;
182 wrqu->txpower.fixed = 0;
183 wrqu->txpower.disabled = 0;
184 wrqu->txpower.flags = IW_TXPOW_MWATT;
185 break;
186 }
187 case SIOCSIWTXPOW:
188 {
189 if (wrqu->txpower.flags != IW_TXPOW_MWATT)
190 return -EINVAL;
191
192 if (wl_ioctl(dev, WLC_SET_TXPWR, &wrqu->txpower.value, sizeof(int)) < 0)
193 return -EINVAL;
194 }
195 case SIOCGIWENCODE:
196 {
197 wrqu->data.flags = IW_ENCODE_DISABLED;
198 break;
199 }
200 case SIOCGIWRANGE:
201 {
202 return wlcompat_ioctl_getiwrange(dev, extra);
203 break;
204 }
205 case SIOCSIWMODE:
206 {
207 int ap = -1, infra = -1, passive = 0, wet = 0;
208
209 switch (wrqu->mode) {
210 case IW_MODE_MONITOR:
211 passive = 1;
212 break;
213 case IW_MODE_ADHOC:
214 infra = 0;
215 ap = 0;
216 break;
217 case IW_MODE_MASTER:
218 infra = 1;
219 ap = 1;
220 break;
221 case IW_MODE_INFRA:
222 infra = 1;
223 ap = 0;
224 break;
225 case IW_MODE_REPEAT:
226 infra = 1;
227 ap = 0;
228 wet = 1;
229 break;
230 default:
231 return -EINVAL;
232 }
233
234 if (wl_ioctl(dev, WLC_SET_PASSIVE, &passive, sizeof(passive)) < 0)
235 return -EINVAL;
236 if (wl_ioctl(dev, WLC_SET_MONITOR, &passive, sizeof(passive)) < 0)
237 return -EINVAL;
238 if (wl_ioctl(dev, WLC_SET_WET, &wet, sizeof(wet)) < 0)
239 return -EINVAL;
240 if (ap >= 0)
241 if (wl_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap)) < 0)
242 return -EINVAL;
243 if (infra >= 0)
244 if (wl_ioctl(dev, WLC_SET_INFRA, &infra, sizeof(infra)) < 0)
245 return -EINVAL;
246
247 break;
248
249 }
250 case SIOCGIWMODE:
251 {
252 int ap, infra, wet, passive;
253
254 if (wl_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap)) < 0)
255 return -EINVAL;
256 if (wl_ioctl(dev, WLC_GET_INFRA, &infra, sizeof(infra)) < 0)
257 return -EINVAL;
258 if (wl_ioctl(dev, WLC_GET_PASSIVE, &passive, sizeof(passive)) < 0)
259 return -EINVAL;
260 if (wl_ioctl(dev, WLC_GET_WET, &wet, sizeof(wet)) < 0)
261 return -EINVAL;
262
263 if (passive) {
264 wrqu->mode = IW_MODE_MONITOR;
265 } else if (!infra) {
266 wrqu->mode = IW_MODE_ADHOC;
267 } else {
268 if (ap) {
269 wrqu->mode = IW_MODE_MASTER;
270 } else {
271 if (wet) {
272 wrqu->mode = IW_MODE_REPEAT;
273 } else {
274 wrqu->mode = IW_MODE_INFRA;
275 }
276 }
277 }
278 break;
279 }
280 default:
281 {
282 return -EINVAL;
283 }
284 }
285
286 return 0;
287 }
288
289 static const iw_handler wlcompat_handler[] = {
290 NULL, /* SIOCSIWCOMMIT */
291 wlcompat_ioctl, /* SIOCGIWNAME */
292 NULL, /* SIOCSIWNWID */
293 NULL, /* SIOCGIWNWID */
294 wlcompat_ioctl, /* SIOCSIWFREQ */
295 wlcompat_ioctl, /* SIOCGIWFREQ */
296 wlcompat_ioctl, /* SIOCSIWMODE */
297 wlcompat_ioctl, /* SIOCGIWMODE */
298 NULL, /* SIOCSIWSENS */
299 NULL, /* SIOCGIWSENS */
300 NULL, /* SIOCSIWRANGE */
301 wlcompat_ioctl, /* SIOCGIWRANGE */
302 NULL, /* SIOCSIWPRIV */
303 NULL, /* SIOCGIWPRIV */
304 NULL, /* SIOCSIWSTATS */
305 NULL, /* SIOCGIWSTATS */
306 iw_handler_set_spy, /* SIOCSIWSPY */
307 iw_handler_get_spy, /* SIOCGIWSPY */
308 iw_handler_set_thrspy, /* SIOCSIWTHRSPY */
309 iw_handler_get_thrspy, /* SIOCGIWTHRSPY */
310 NULL, /* SIOCSIWAP */
311 wlcompat_ioctl, /* SIOCGIWAP */
312 NULL, /* -- hole -- */
313 NULL, /* SIOCGIWAPLIST */
314 NULL, /* -- hole -- */
315 NULL, /* -- hole -- */
316 wlcompat_ioctl, /* SIOCSIWESSID */
317 wlcompat_ioctl, /* SIOCGIWESSID */
318 NULL, /* SIOCSIWNICKN */
319 NULL, /* SIOCGIWNICKN */
320 NULL, /* -- hole -- */
321 NULL, /* -- hole -- */
322 NULL, /* SIOCSIWRATE */
323 NULL, /* SIOCGIWRATE */
324 NULL, /* SIOCSIWRTS */
325 wlcompat_ioctl, /* SIOCGIWRTS */
326 NULL, /* SIOCSIWFRAG */
327 wlcompat_ioctl, /* SIOCGIWFRAG */
328 wlcompat_ioctl, /* SIOCSIWTXPOW */
329 wlcompat_ioctl, /* SIOCGIWTXPOW */
330 NULL, /* SIOCSIWRETRY */
331 NULL, /* SIOCGIWRETRY */
332 NULL, /* SIOCSIWENCODE */
333 wlcompat_ioctl, /* SIOCGIWENCODE */
334 };
335
336 static const struct iw_handler_def wlcompat_handler_def =
337 {
338 .standard = (iw_handler *) wlcompat_handler,
339 .num_standard = sizeof(wlcompat_handler)/sizeof(iw_handler),
340 .private = NULL,
341 .num_private = 0,
342 .private_args = NULL,
343 .num_private_args = 0,
344 };
345
346 #ifdef DEBUG
347 static int (*old_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
348 static int new_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) {
349 int ret = old_ioctl(dev,ifr,cmd);
350 printk("dev: %s ioctl: 0x%04x\n",dev->name,cmd);
351 if (cmd==SIOCDEVPRIVATE) {
352 int x;
353 wl_ioctl_t *ioc = (wl_ioctl_t *)ifr->ifr_data;
354 unsigned char *buf = ioc->buf;
355 printk(" cmd: %d buf: 0x%08x len: %d\n",ioc->cmd,&(ioc->buf),ioc->len);
356 printk(" ->");
357 for (x=0;x<ioc->len && x<128 ;x++) {
358 printk("%02X",buf[x]);
359 }
360 printk("\n");
361 }
362 return ret;
363 }
364 #endif
365
366 static int __init wlcompat_init()
367 {
368 dev = dev_get_by_name("eth1");
369 #ifdef DEBUG
370 old_ioctl = dev->do_ioctl;
371 dev->do_ioctl = new_ioctl;
372 #endif
373 dev->wireless_handlers = (struct iw_handler_def *)&wlcompat_handler_def;
374 return 0;
375 }
376
377 static void __exit wlcompat_exit()
378 {
379 dev->wireless_handlers = NULL;
380 #ifdef DEBUG
381 dev->do_ioctl = old_ioctl;
382 #endif
383 return;
384 }
385
386 EXPORT_NO_SYMBOLS;
387 MODULE_AUTHOR("openwrt.org");
388 MODULE_LICENSE("GPL");
389
390 module_init(wlcompat_init);
391 module_exit(wlcompat_exit);
This page took 0.119988 seconds and 5 git commands to generate.