ebed8c365419d797fc8572ef8fb620cf02c0e079
[openwrt.git] / openwrt / 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 }
133 case SIOCGIWAP:
134 {
135 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
136 if (wl_ioctl(dev,WLC_GET_BSSID,wrqu->ap_addr.sa_data,6) < 0)
137 return -EINVAL;
138 break;
139 }
140 case SIOCGIWESSID:
141 {
142 wlc_ssid_t ssid;
143
144 if (wl_ioctl(dev,WLC_GET_SSID, &ssid, sizeof(wlc_ssid_t)) < 0)
145 return -EINVAL;
146
147 wrqu->essid.flags = wrqu->data.flags = 1;
148 wrqu->essid.length = wrqu->data.length = ssid.SSID_len + 1;
149 memcpy(extra,ssid.SSID,ssid.SSID_len + 1);
150 break;
151 }
152 case SIOCSIWESSID:
153 {
154 wlc_ssid_t ssid;
155 memset(&ssid, 0, sizeof(ssid));
156 ssid.SSID_len = strlen(extra);
157 if (ssid.SSID_len > WLC_ESSID_MAX_SIZE)
158 ssid.SSID_len = WLC_ESSID_MAX_SIZE;
159 memcpy(ssid.SSID, extra, ssid.SSID_len);
160 if (wl_ioctl(dev, WLC_SET_SSID, &ssid, sizeof(ssid)) < 0)
161 return -EINVAL;
162 break;
163 }
164 case SIOCGIWRTS:
165 {
166 if (wl_ioctl(dev,WLC_GET_RTS,&(wrqu->rts.value),sizeof(int)) < 0)
167 return -EINVAL;
168 break;
169 }
170 case SIOCGIWFRAG:
171 {
172 if (wl_ioctl(dev,WLC_GET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
173 return -EINVAL;
174 break;
175 }
176 case SIOCGIWTXPOW:
177 {
178 wrqu->txpower.value = 0;
179 if (wl_ioctl(dev,WLC_GET_TXPWR, &(wrqu->txpower.value), sizeof(int)) < 0)
180 return -EINVAL;
181 wrqu->txpower.fixed = 0;
182 wrqu->txpower.disabled = 0;
183 wrqu->txpower.flags = IW_TXPOW_MWATT;
184 break;
185 }
186 case SIOCSIWTXPOW:
187 {
188 if (wrqu->txpower.flags != IW_TXPOW_MWATT)
189 return -EINVAL;
190
191 if (wl_ioctl(dev, WLC_SET_TXPWR, &wrqu->txpower.value, sizeof(int)) < 0)
192 return -EINVAL;
193 }
194 case SIOCGIWENCODE:
195 {
196 wrqu->data.flags = IW_ENCODE_DISABLED;
197 break;
198 }
199 case SIOCGIWRANGE:
200 {
201 return wlcompat_ioctl_getiwrange(dev, extra);
202 break;
203 }
204 case SIOCSIWMODE:
205 {
206 int ap = -1, infra = -1, passive = -1;
207
208 switch (wrqu->mode) {
209 case IW_MODE_MONITOR:
210 passive = 1;
211 break;
212 case IW_MODE_ADHOC:
213 passive = 0;
214 infra = 0;
215 ap = 0;
216 break;
217 case IW_MODE_MASTER:
218 passive = 0;
219 infra = 1;
220 ap = 1;
221 break;
222 case IW_MODE_INFRA:
223 passive = 0;
224 infra = 1;
225 ap = 0;
226 break;
227 default:
228 return -EINVAL;
229 }
230
231 if (passive >= 0) {
232 if (wl_ioctl(dev, WLC_SET_PASSIVE, &passive, sizeof(passive)) < 0)
233 return -EINVAL;
234 if (wl_ioctl(dev, WLC_SET_MONITOR, &passive, sizeof(passive)) < 0)
235 return -EINVAL;
236 }
237 if (ap >= 0)
238 if (wl_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap)) < 0)
239 return -EINVAL;
240 if (infra >= 0)
241 if (wl_ioctl(dev, WLC_SET_INFRA, &infra, sizeof(infra)) < 0)
242 return -EINVAL;
243
244 break;
245
246 }
247 case SIOCGIWMODE:
248 {
249 int ap, infra, passive;
250
251 if (wl_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap)) < 0)
252 return -EINVAL;
253
254 if (wl_ioctl(dev, WLC_GET_INFRA, &infra, sizeof(infra)) < 0)
255 return -EINVAL;
256
257 if (wl_ioctl(dev, WLC_GET_PASSIVE, &passive, sizeof(passive)) < 0)
258 return -EINVAL;
259
260 if (passive) {
261 wrqu->mode = IW_MODE_MONITOR;
262 } else if (!infra) {
263 wrqu->mode = IW_MODE_ADHOC;
264 } else {
265 if (ap) {
266 wrqu->mode = IW_MODE_MASTER;
267 } else {
268 wrqu->mode = IW_MODE_INFRA;
269 }
270 }
271 break;
272 }
273 default:
274 {
275 return -EINVAL;
276 }
277 }
278
279 return 0;
280 }
281
282 static const iw_handler wlcompat_handler[] = {
283 NULL, /* SIOCSIWCOMMIT */
284 wlcompat_ioctl, /* SIOCGIWNAME */
285 NULL, /* SIOCSIWNWID */
286 NULL, /* SIOCGIWNWID */
287 wlcompat_ioctl, /* SIOCSIWFREQ */
288 wlcompat_ioctl, /* SIOCGIWFREQ */
289 wlcompat_ioctl, /* SIOCSIWMODE */
290 wlcompat_ioctl, /* SIOCGIWMODE */
291 NULL, /* SIOCSIWSENS */
292 NULL, /* SIOCGIWSENS */
293 NULL, /* SIOCSIWRANGE */
294 wlcompat_ioctl, /* SIOCGIWRANGE */
295 NULL, /* SIOCSIWPRIV */
296 NULL, /* SIOCGIWPRIV */
297 NULL, /* SIOCSIWSTATS */
298 NULL, /* SIOCGIWSTATS */
299 iw_handler_set_spy, /* SIOCSIWSPY */
300 iw_handler_get_spy, /* SIOCGIWSPY */
301 iw_handler_set_thrspy, /* SIOCSIWTHRSPY */
302 iw_handler_get_thrspy, /* SIOCGIWTHRSPY */
303 NULL, /* SIOCSIWAP */
304 wlcompat_ioctl, /* SIOCGIWAP */
305 NULL, /* -- hole -- */
306 NULL, /* SIOCGIWAPLIST */
307 NULL, /* -- hole -- */
308 NULL, /* -- hole -- */
309 wlcompat_ioctl, /* SIOCSIWESSID */
310 wlcompat_ioctl, /* SIOCGIWESSID */
311 NULL, /* SIOCSIWNICKN */
312 NULL, /* SIOCGIWNICKN */
313 NULL, /* -- hole -- */
314 NULL, /* -- hole -- */
315 NULL, /* SIOCSIWRATE */
316 NULL, /* SIOCGIWRATE */
317 NULL, /* SIOCSIWRTS */
318 wlcompat_ioctl, /* SIOCGIWRTS */
319 NULL, /* SIOCSIWFRAG */
320 wlcompat_ioctl, /* SIOCGIWFRAG */
321 wlcompat_ioctl, /* SIOCSIWTXPOW */
322 wlcompat_ioctl, /* SIOCGIWTXPOW */
323 NULL, /* SIOCSIWRETRY */
324 NULL, /* SIOCGIWRETRY */
325 NULL, /* SIOCSIWENCODE */
326 wlcompat_ioctl, /* SIOCGIWENCODE */
327 };
328
329 static const struct iw_handler_def wlcompat_handler_def =
330 {
331 .standard = (iw_handler *) wlcompat_handler,
332 .num_standard = sizeof(wlcompat_handler)/sizeof(iw_handler),
333 .private = NULL,
334 .num_private = 0,
335 .private_args = NULL,
336 .num_private_args = 0,
337 };
338
339 #ifdef DEBUG
340 static int (*old_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
341 static int new_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) {
342 int ret = old_ioctl(dev,ifr,cmd);
343 printk("dev: %s ioctl: 0x%04x\n",dev->name,cmd);
344 if (cmd==SIOCDEVPRIVATE) {
345 int x;
346 wl_ioctl_t *ioc = (wl_ioctl_t *)ifr->ifr_data;
347 unsigned char *buf = ioc->buf;
348 printk(" cmd: %d buf: 0x%08x len: %d\n",ioc->cmd,&(ioc->buf),ioc->len);
349 printk(" ->");
350 for (x=0;x<ioc->len && x<128 ;x++) {
351 printk("%02X",buf[x]);
352 }
353 printk("\n");
354 }
355 return ret;
356 }
357 #endif
358
359 static int __init wlcompat_init()
360 {
361 dev = dev_get_by_name("eth1");
362 #ifdef DEBUG
363 old_ioctl = dev->do_ioctl;
364 dev->do_ioctl = new_ioctl;
365 #endif
366 dev->wireless_handlers = (struct iw_handler_def *)&wlcompat_handler_def;
367 return 0;
368 }
369
370 static void __exit wlcompat_exit()
371 {
372 dev->wireless_handlers = NULL;
373 #ifdef DEBUG
374 dev->do_ioctl = old_ioctl;
375 #endif
376 return;
377 }
378
379 EXPORT_NO_SYMBOLS;
380 MODULE_AUTHOR("openwrt.org");
381 MODULE_LICENSE("GPL");
382
383 module_init(wlcompat_init);
384 module_exit(wlcompat_exit);
This page took 0.098798 seconds and 3 git commands to generate.