add wl_ioctl error handling
[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 range->max_rts = 2347;
75 range->min_frag = 256;
76 range->max_frag = 2346;
77
78 range->min_pmp = 0;
79 range->max_pmp = 65535000;
80 range->min_pmt = 0;
81 range->max_pmt = 65535 * 1000;
82
83 range->txpower_capa = IW_TXPOW_MWATT;
84
85 return 0;
86 }
87
88 static int wlcompat_ioctl(struct net_device *dev,
89 struct iw_request_info *info,
90 union iwreq_data *wrqu,
91 char *extra)
92 {
93 switch (info->cmd) {
94 case SIOCGIWNAME:
95 strcpy(wrqu->name, "IEEE 802.11-DS");
96 break;
97 case SIOCGIWFREQ:
98 {
99 channel_info_t ci;
100
101 if (wl_ioctl(dev,WLC_GET_CHANNEL, &ci, sizeof(ci)) < 0)
102 return -EINVAL;
103
104 wrqu->freq.m = ci.target_channel;
105 wrqu->freq.e = 0;
106 break;
107 }
108 case SIOCSIWFREQ:
109 {
110 if (wrqu->freq.e == 1) {
111 int channel = 0;
112 int f = wrqu->freq.m / 100000;
113 while ((channel < NUM_CHANNELS + 1) && (f != channel_frequency[channel]))
114 channel++;
115
116 if (channel == NUM_CHANNELS) // channel not found
117 return -EINVAL;
118
119 wrqu->freq.e = 0;
120 wrqu->freq.m = channel + 1;
121 }
122 if ((wrqu->freq.e == 0) && (wrqu->freq.m < 1000)) {
123 if (wl_ioctl(dev, WLC_SET_CHANNEL, &wrqu->freq.m, sizeof(int)) < 0)
124 return -EINVAL;
125 } else {
126 return -EINVAL;
127 }
128 }
129 case SIOCGIWAP:
130 {
131 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
132 if (wl_ioctl(dev,WLC_GET_BSSID,wrqu->ap_addr.sa_data,6) < 0)
133 return -EINVAL;
134 break;
135 }
136 case SIOCGIWESSID:
137 {
138 wlc_ssid_t ssid;
139
140 if (wl_ioctl(dev,WLC_GET_SSID, &ssid, sizeof(wlc_ssid_t)) < 0)
141 return -EINVAL;
142
143 wrqu->essid.flags = wrqu->data.flags = 1;
144 wrqu->essid.length = wrqu->data.length = ssid.SSID_len + 1;
145 memcpy(extra,ssid.SSID,ssid.SSID_len + 1);
146 break;
147 }
148 case SIOCSIWESSID:
149 {
150 wlc_ssid_t ssid;
151 memset(&ssid, 0, sizeof(ssid));
152 ssid.SSID_len = strlen(extra);
153 if (ssid.SSID_len > WLC_ESSID_MAX_SIZE)
154 ssid.SSID_len = WLC_ESSID_MAX_SIZE;
155 memcpy(ssid.SSID, extra, ssid.SSID_len);
156 if (wl_ioctl(dev, WLC_SET_SSID, &ssid, sizeof(ssid)) < 0)
157 return -EINVAL;
158 break;
159 }
160 case SIOCGIWRTS:
161 {
162 if (wl_ioctl(dev,WLC_GET_RTS,&(wrqu->rts.value),sizeof(int)) < 0)
163 return -EINVAL;
164 break;
165 }
166 case SIOCGIWFRAG:
167 {
168 if (wl_ioctl(dev,WLC_GET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
169 return -EINVAL;
170 break;
171 }
172 case SIOCGIWTXPOW:
173 {
174 wrqu->txpower.value = 0;
175 if (wl_ioctl(dev,WLC_GET_TXPWR, &(wrqu->txpower.value), sizeof(int)) < 0)
176 return -EINVAL;
177 wrqu->txpower.fixed = 0;
178 wrqu->txpower.disabled = 0;
179 wrqu->txpower.flags = IW_TXPOW_MWATT;
180 break;
181 }
182 case SIOCSIWTXPOW:
183 {
184 if (wrqu->txpower.flags != IW_TXPOW_MWATT)
185 return -EINVAL;
186
187 if (wl_ioctl(dev, WLC_SET_TXPWR, &wrqu->txpower.value, sizeof(int)) < 0)
188 return -EINVAL;
189 }
190 case SIOCGIWENCODE:
191 {
192 wrqu->data.flags = IW_ENCODE_DISABLED;
193 break;
194 }
195 case SIOCGIWRANGE:
196 {
197 return wlcompat_ioctl_getiwrange(dev, extra);
198 break;
199 }
200 default:
201 {
202 return -EINVAL;
203 }
204 }
205
206 return 0;
207 }
208
209 static const iw_handler wlcompat_handler[] = {
210 NULL, /* SIOCSIWCOMMIT */
211 wlcompat_ioctl, /* SIOCGIWNAME */
212 NULL, /* SIOCSIWNWID */
213 NULL, /* SIOCGIWNWID */
214 wlcompat_ioctl, /* SIOCSIWFREQ */
215 wlcompat_ioctl, /* SIOCGIWFREQ */
216 NULL, /* SIOCSIWMODE */
217 NULL, /* SIOCGIWMODE */
218 NULL, /* SIOCSIWSENS */
219 NULL, /* SIOCGIWSENS */
220 NULL, /* SIOCSIWRANGE */
221 wlcompat_ioctl, /* SIOCGIWRANGE */
222 NULL, /* SIOCSIWPRIV */
223 NULL, /* SIOCGIWPRIV */
224 NULL, /* SIOCSIWSTATS */
225 NULL, /* SIOCGIWSTATS */
226 iw_handler_set_spy, /* SIOCSIWSPY */
227 iw_handler_get_spy, /* SIOCGIWSPY */
228 iw_handler_set_thrspy, /* SIOCSIWTHRSPY */
229 iw_handler_get_thrspy, /* SIOCGIWTHRSPY */
230 NULL, /* SIOCSIWAP */
231 wlcompat_ioctl, /* SIOCGIWAP */
232 NULL, /* -- hole -- */
233 NULL, /* SIOCGIWAPLIST */
234 NULL, /* -- hole -- */
235 NULL, /* -- hole -- */
236 wlcompat_ioctl, /* SIOCSIWESSID */
237 wlcompat_ioctl, /* SIOCGIWESSID */
238 NULL, /* SIOCSIWNICKN */
239 NULL, /* SIOCGIWNICKN */
240 NULL, /* -- hole -- */
241 NULL, /* -- hole -- */
242 NULL, /* SIOCSIWRATE */
243 NULL, /* SIOCGIWRATE */
244 NULL, /* SIOCSIWRTS */
245 wlcompat_ioctl, /* SIOCGIWRTS */
246 NULL, /* SIOCSIWFRAG */
247 wlcompat_ioctl, /* SIOCGIWFRAG */
248 wlcompat_ioctl, /* SIOCSIWTXPOW */
249 wlcompat_ioctl, /* SIOCGIWTXPOW */
250 NULL, /* SIOCSIWRETRY */
251 NULL, /* SIOCGIWRETRY */
252 NULL, /* SIOCSIWENCODE */
253 wlcompat_ioctl, /* SIOCGIWENCODE */
254 };
255
256 static const struct iw_handler_def wlcompat_handler_def =
257 {
258 .standard = (iw_handler *) wlcompat_handler,
259 .num_standard = sizeof(wlcompat_handler)/sizeof(iw_handler),
260 .private = NULL,
261 .num_private = 0,
262 .private_args = NULL,
263 .num_private_args = 0,
264 };
265
266 #ifdef DEBUG
267 static int (*old_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
268 static int new_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) {
269 int ret = old_ioctl(dev,ifr,cmd);
270 printk("dev: %s ioctl: 0x%04x\n",dev->name,cmd);
271 if (cmd==SIOCDEVPRIVATE) {
272 int x;
273 wl_ioctl_t *ioc = (wl_ioctl_t *)ifr->ifr_data;
274 unsigned char *buf = ioc->buf;
275 printk(" cmd: %d buf: 0x%08x len: %d\n",ioc->cmd,&(ioc->buf),ioc->len);
276 printk(" ->");
277 for (x=0;x<ioc->len && x<128 ;x++) {
278 printk("%02X",buf[x]);
279 }
280 printk("\n");
281 }
282 return ret;
283 }
284 #endif
285
286 static int __init wlcompat_init()
287 {
288 dev = dev_get_by_name("eth1");
289 #ifdef DEBUG
290 old_ioctl = dev->do_ioctl;
291 dev->do_ioctl = new_ioctl;
292 #endif
293 dev->wireless_handlers = (struct iw_handler_def *)&wlcompat_handler_def;
294 return 0;
295 }
296
297 static void __exit wlcompat_exit()
298 {
299 dev->wireless_handlers = NULL;
300 #ifdef DEBUG
301 dev->do_ioctl = old_ioctl;
302 #endif
303 return;
304 }
305
306 EXPORT_NO_SYMBOLS;
307 MODULE_AUTHOR("openwrt.org");
308 MODULE_LICENSE("GPL");
309
310 module_init(wlcompat_init);
311 module_exit(wlcompat_exit);
This page took 0.058684 seconds and 5 git commands to generate.