470466d132f3100cb16517cee777a12be1327e85
[linux-2.6.git] / drivers / net / wireless / prism54 / oid_mgt.c
1 /*   
2  *  Copyright (C) 2003,2004 Aurelien Alleaume <slts@free.fr>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
18
19 #include "prismcompat.h"
20 #include "islpci_dev.h"
21 #include "islpci_mgt.h"
22 #include "isl_oid.h"
23 #include "oid_mgt.h"
24 #include "isl_ioctl.h"
25
26 /* to convert between channel and freq */
27 const int frequency_list_bg[] = { 2412, 2417, 2422, 2427, 2432, 2437, 2442,
28         2447, 2452, 2457, 2462, 2467, 2472, 2484
29 };
30
31 const int frequency_list_a[] = { 5170, 5180, 5190, 5200, 5210, 5220, 5230,
32         5240, 5260, 5280, 5300, 5320
33 };
34
35 int
36 channel_of_freq(int f)
37 {
38         int c = 0;
39
40         if ((f >= 2412) && (f <= 2484)) {
41                 while ((c < 14) && (f != frequency_list_bg[c]))
42                         c++;
43                 return (c >= 14) ? 0 : ++c;
44         } else if ((f >= (int) 5170) && (f <= (int) 5320)) {
45                 while ((c < 12) && (f != frequency_list_a[c]))
46                         c++;
47                 return (c >= 12) ? 0 : (c + 37);
48         } else
49                 return 0;
50 }
51
52 #define OID_STRUCT(name,oid,s,t) [name] = {oid, 0, sizeof(s), t}
53 #define OID_STRUCT_C(name,oid,s,t) OID_STRUCT(name,oid,s,t | OID_FLAG_CACHED)
54 #define OID_U32(name,oid) OID_STRUCT(name,oid,u32,OID_TYPE_U32)
55 #define OID_U32_C(name,oid) OID_STRUCT_C(name,oid,u32,OID_TYPE_U32)
56 #define OID_STRUCT_MLME(name,oid) OID_STRUCT(name,oid,struct obj_mlme,OID_TYPE_MLME)
57 #define OID_STRUCT_MLMEEX(name,oid) OID_STRUCT(name,oid,struct obj_mlmeex,OID_TYPE_MLMEEX)
58
59 #define OID_UNKNOWN(name,oid) OID_STRUCT(name,oid,0,0)
60
61 struct oid_t isl_oid[] = {
62         OID_STRUCT(GEN_OID_MACADDRESS, 0x00000000, u8[6], OID_TYPE_ADDR),
63         OID_U32(GEN_OID_LINKSTATE, 0x00000001),
64         OID_UNKNOWN(GEN_OID_WATCHDOG, 0x00000002),
65         OID_UNKNOWN(GEN_OID_MIBOP, 0x00000003),
66         OID_UNKNOWN(GEN_OID_OPTIONS, 0x00000004),
67         OID_UNKNOWN(GEN_OID_LEDCONFIG, 0x00000005),
68
69         /* 802.11 */
70         OID_U32_C(DOT11_OID_BSSTYPE, 0x10000000),
71         OID_STRUCT_C(DOT11_OID_BSSID, 0x10000001, u8[6], OID_TYPE_SSID),
72         OID_STRUCT_C(DOT11_OID_SSID, 0x10000002, struct obj_ssid,
73                      OID_TYPE_SSID),
74         OID_U32(DOT11_OID_STATE, 0x10000003),
75         OID_U32(DOT11_OID_AID, 0x10000004),
76         OID_STRUCT(DOT11_OID_COUNTRYSTRING, 0x10000005, u8[4], OID_TYPE_RAW),
77         OID_STRUCT_C(DOT11_OID_SSIDOVERRIDE, 0x10000006, struct obj_ssid,
78                      OID_TYPE_SSID),
79
80         OID_U32(DOT11_OID_MEDIUMLIMIT, 0x11000000),
81         OID_U32_C(DOT11_OID_BEACONPERIOD, 0x11000001),
82         OID_U32(DOT11_OID_DTIMPERIOD, 0x11000002),
83         OID_U32(DOT11_OID_ATIMWINDOW, 0x11000003),
84         OID_U32(DOT11_OID_LISTENINTERVAL, 0x11000004),
85         OID_U32(DOT11_OID_CFPPERIOD, 0x11000005),
86         OID_U32(DOT11_OID_CFPDURATION, 0x11000006),
87
88         OID_U32_C(DOT11_OID_AUTHENABLE, 0x12000000),
89         OID_U32_C(DOT11_OID_PRIVACYINVOKED, 0x12000001),
90         OID_U32_C(DOT11_OID_EXUNENCRYPTED, 0x12000002),
91         OID_U32_C(DOT11_OID_DEFKEYID, 0x12000003),
92         [DOT11_OID_DEFKEYX] = {0x12000004, 3, sizeof (struct obj_key),
93                                OID_FLAG_CACHED | OID_TYPE_KEY}, /* DOT11_OID_DEFKEY1,...DOT11_OID_DEFKEY4 */
94         OID_UNKNOWN(DOT11_OID_STAKEY, 0x12000008),
95         OID_U32(DOT11_OID_REKEYTHRESHOLD, 0x12000009),
96         OID_UNKNOWN(DOT11_OID_STASC, 0x1200000a),
97
98         OID_U32(DOT11_OID_PRIVTXREJECTED, 0x1a000000),
99         OID_U32(DOT11_OID_PRIVRXPLAIN, 0x1a000001),
100         OID_U32(DOT11_OID_PRIVRXFAILED, 0x1a000002),
101         OID_U32(DOT11_OID_PRIVRXNOKEY, 0x1a000003),
102
103         OID_U32_C(DOT11_OID_RTSTHRESH, 0x13000000),
104         OID_U32_C(DOT11_OID_FRAGTHRESH, 0x13000001),
105         OID_U32_C(DOT11_OID_SHORTRETRIES, 0x13000002),
106         OID_U32_C(DOT11_OID_LONGRETRIES, 0x13000003),
107         OID_U32_C(DOT11_OID_MAXTXLIFETIME, 0x13000004),
108         OID_U32(DOT11_OID_MAXRXLIFETIME, 0x13000005),
109         OID_U32(DOT11_OID_AUTHRESPTIMEOUT, 0x13000006),
110         OID_U32(DOT11_OID_ASSOCRESPTIMEOUT, 0x13000007),
111
112         OID_UNKNOWN(DOT11_OID_ALOFT_TABLE, 0x1d000000),
113         OID_UNKNOWN(DOT11_OID_ALOFT_CTRL_TABLE, 0x1d000001),
114         OID_UNKNOWN(DOT11_OID_ALOFT_RETREAT, 0x1d000002),
115         OID_UNKNOWN(DOT11_OID_ALOFT_PROGRESS, 0x1d000003),
116         OID_U32(DOT11_OID_ALOFT_FIXEDRATE, 0x1d000004),
117         OID_UNKNOWN(DOT11_OID_ALOFT_RSSIGRAPH, 0x1d000005),
118         OID_UNKNOWN(DOT11_OID_ALOFT_CONFIG, 0x1d000006),
119
120         [DOT11_OID_VDCFX] = {0x1b000000, 7, 0, 0},
121         OID_U32(DOT11_OID_MAXFRAMEBURST, 0x1b000008),
122
123         OID_U32(DOT11_OID_PSM, 0x14000000),
124         OID_U32(DOT11_OID_CAMTIMEOUT, 0x14000001),
125         OID_U32(DOT11_OID_RECEIVEDTIMS, 0x14000002),
126         OID_U32(DOT11_OID_ROAMPREFERENCE, 0x14000003),
127
128         OID_U32(DOT11_OID_BRIDGELOCAL, 0x15000000),
129         OID_U32(DOT11_OID_CLIENTS, 0x15000001),
130         OID_U32(DOT11_OID_CLIENTSASSOCIATED, 0x15000002),
131         [DOT11_OID_CLIENTX] = {0x15000003, 2006, 0, 0}, /* DOT11_OID_CLIENTX,...DOT11_OID_CLIENT2007 */
132
133         OID_STRUCT(DOT11_OID_CLIENTFIND, 0x150007DB, u8[6], OID_TYPE_ADDR),
134         OID_STRUCT(DOT11_OID_WDSLINKADD, 0x150007DC, u8[6], OID_TYPE_ADDR),
135         OID_STRUCT(DOT11_OID_WDSLINKREMOVE, 0x150007DD, u8[6], OID_TYPE_ADDR),
136         OID_STRUCT(DOT11_OID_EAPAUTHSTA, 0x150007DE, u8[6], OID_TYPE_ADDR),
137         OID_STRUCT(DOT11_OID_EAPUNAUTHSTA, 0x150007DF, u8[6], OID_TYPE_ADDR),
138         OID_U32_C(DOT11_OID_DOT1XENABLE, 0x150007E0),
139         OID_UNKNOWN(DOT11_OID_MICFAILURE, 0x150007E1),
140         OID_UNKNOWN(DOT11_OID_REKEYINDICATE, 0x150007E2),
141
142         OID_U32(DOT11_OID_MPDUTXSUCCESSFUL, 0x16000000),
143         OID_U32(DOT11_OID_MPDUTXONERETRY, 0x16000001),
144         OID_U32(DOT11_OID_MPDUTXMULTIPLERETRIES, 0x16000002),
145         OID_U32(DOT11_OID_MPDUTXFAILED, 0x16000003),
146         OID_U32(DOT11_OID_MPDURXSUCCESSFUL, 0x16000004),
147         OID_U32(DOT11_OID_MPDURXDUPS, 0x16000005),
148         OID_U32(DOT11_OID_RTSSUCCESSFUL, 0x16000006),
149         OID_U32(DOT11_OID_RTSFAILED, 0x16000007),
150         OID_U32(DOT11_OID_ACKFAILED, 0x16000008),
151         OID_U32(DOT11_OID_FRAMERECEIVES, 0x16000009),
152         OID_U32(DOT11_OID_FRAMEERRORS, 0x1600000A),
153         OID_U32(DOT11_OID_FRAMEABORTS, 0x1600000B),
154         OID_U32(DOT11_OID_FRAMEABORTSPHY, 0x1600000C),
155
156         OID_U32(DOT11_OID_SLOTTIME, 0x17000000),
157         OID_U32(DOT11_OID_CWMIN, 0x17000001),
158         OID_U32(DOT11_OID_CWMAX, 0x17000002),
159         OID_U32(DOT11_OID_ACKWINDOW, 0x17000003),
160         OID_U32(DOT11_OID_ANTENNARX, 0x17000004),
161         OID_U32(DOT11_OID_ANTENNATX, 0x17000005),
162         OID_U32(DOT11_OID_ANTENNADIVERSITY, 0x17000006),
163         OID_U32_C(DOT11_OID_CHANNEL, 0x17000007),
164         OID_U32_C(DOT11_OID_EDTHRESHOLD, 0x17000008),
165         OID_U32(DOT11_OID_PREAMBLESETTINGS, 0x17000009),
166         OID_STRUCT(DOT11_OID_RATES, 0x1700000A, u8[IWMAX_BITRATES + 1],
167                    OID_TYPE_RAW),
168         OID_U32(DOT11_OID_CCAMODESUPPORTED, 0x1700000B),
169         OID_U32(DOT11_OID_CCAMODE, 0x1700000C),
170         OID_UNKNOWN(DOT11_OID_RSSIVECTOR, 0x1700000D),
171         OID_UNKNOWN(DOT11_OID_OUTPUTPOWERTABLE, 0x1700000E),
172         OID_U32(DOT11_OID_OUTPUTPOWER, 0x1700000F),
173         OID_STRUCT(DOT11_OID_SUPPORTEDRATES, 0x17000010,
174                    u8[IWMAX_BITRATES + 1], OID_TYPE_RAW),
175         OID_U32_C(DOT11_OID_FREQUENCY, 0x17000011),
176         [DOT11_OID_SUPPORTEDFREQUENCIES] =
177             {0x17000012, 0, sizeof (struct obj_frequencies)
178              + sizeof (u16) * IWMAX_FREQ, OID_TYPE_FREQUENCIES},
179
180         OID_U32(DOT11_OID_NOISEFLOOR, 0x17000013),
181         OID_STRUCT(DOT11_OID_FREQUENCYACTIVITY, 0x17000014, u8[IWMAX_FREQ + 1],
182                    OID_TYPE_RAW),
183         OID_UNKNOWN(DOT11_OID_IQCALIBRATIONTABLE, 0x17000015),
184         OID_U32(DOT11_OID_NONERPPROTECTION, 0x17000016),
185         OID_U32(DOT11_OID_SLOTSETTINGS, 0x17000017),
186         OID_U32(DOT11_OID_NONERPTIMEOUT, 0x17000018),
187         OID_U32(DOT11_OID_PROFILES, 0x17000019),
188         OID_STRUCT(DOT11_OID_EXTENDEDRATES, 0x17000020,
189                    u8[IWMAX_BITRATES + 1], OID_TYPE_RAW),
190
191         OID_STRUCT_MLME(DOT11_OID_DEAUTHENTICATE, 0x18000000),
192         OID_STRUCT_MLME(DOT11_OID_AUTHENTICATE, 0x18000001),
193         OID_STRUCT_MLME(DOT11_OID_DISASSOCIATE, 0x18000002),
194         OID_STRUCT_MLME(DOT11_OID_ASSOCIATE, 0x18000003),
195         OID_UNKNOWN(DOT11_OID_SCAN, 0x18000004),
196         OID_STRUCT_MLMEEX(DOT11_OID_BEACON, 0x18000005),
197         OID_STRUCT_MLMEEX(DOT11_OID_PROBE, 0x18000006),
198         OID_STRUCT_MLMEEX(DOT11_OID_DEAUTHENTICATEEX, 0x18000007),
199         OID_STRUCT_MLMEEX(DOT11_OID_AUTHENTICATEEX, 0x18000008),
200         OID_STRUCT_MLMEEX(DOT11_OID_DISASSOCIATEEX, 0x18000009),
201         OID_STRUCT_MLMEEX(DOT11_OID_ASSOCIATEEX, 0x1800000A),
202         OID_STRUCT_MLMEEX(DOT11_OID_REASSOCIATE, 0x1800000B),
203         OID_STRUCT_MLMEEX(DOT11_OID_REASSOCIATEEX, 0x1800000C),
204
205         OID_U32(DOT11_OID_NONERPSTATUS, 0x1E000000),
206
207         OID_U32(DOT11_OID_STATIMEOUT, 0x19000000),
208         OID_U32_C(DOT11_OID_MLMEAUTOLEVEL, 0x19000001),
209         OID_U32(DOT11_OID_BSSTIMEOUT, 0x19000002),
210         OID_UNKNOWN(DOT11_OID_ATTACHMENT, 0x19000003),
211         OID_STRUCT_C(DOT11_OID_PSMBUFFER, 0x19000004, struct obj_buffer,
212                      OID_TYPE_BUFFER),
213
214         OID_U32(DOT11_OID_BSSS, 0x1C000000),
215         [DOT11_OID_BSSX] = {0x1C000001, 63, sizeof (struct obj_bss),
216                             OID_TYPE_BSS},      /*DOT11_OID_BSS1,...,DOT11_OID_BSS64 */
217         OID_STRUCT(DOT11_OID_BSSFIND, 0x1C000042, struct obj_bss, OID_TYPE_BSS),
218         [DOT11_OID_BSSLIST] = {0x1C000043, 0, sizeof (struct
219                                                       obj_bsslist) +
220                                sizeof (struct obj_bss[IWMAX_BSS]),
221                                OID_TYPE_BSSLIST},
222
223         OID_UNKNOWN(OID_INL_TUNNEL, 0xFF020000),
224         OID_UNKNOWN(OID_INL_MEMADDR, 0xFF020001),
225         OID_UNKNOWN(OID_INL_MEMORY, 0xFF020002),
226         OID_U32_C(OID_INL_MODE, 0xFF020003),
227         OID_UNKNOWN(OID_INL_COMPONENT_NR, 0xFF020004),
228         OID_UNKNOWN(OID_INL_VERSION, 0xFF020005),
229         OID_UNKNOWN(OID_INL_INTERFACE_ID, 0xFF020006),
230         OID_UNKNOWN(OID_INL_COMPONENT_ID, 0xFF020007),
231         OID_U32_C(OID_INL_CONFIG, 0xFF020008),
232         OID_U32_C(OID_INL_DOT11D_CONFORMANCE, 0xFF02000C),
233         OID_U32(OID_INL_PHYCAPABILITIES, 0xFF02000D),
234         OID_U32_C(OID_INL_OUTPUTPOWER, 0xFF02000F),
235
236 };
237
238 int
239 mgt_init(islpci_private *priv)
240 {
241         int i;
242
243         priv->mib = kmalloc(OID_NUM_LAST * sizeof (void *), GFP_KERNEL);
244         if (!priv->mib)
245                 return -ENOMEM;
246
247         memset(priv->mib, 0, OID_NUM_LAST * sizeof (void *));
248
249         /* Alloc the cache */
250         for (i = 0; i < OID_NUM_LAST; i++) {
251                 if (isl_oid[i].flags & OID_FLAG_CACHED) {
252                         priv->mib[i] = kmalloc(isl_oid[i].size *
253                                                (isl_oid[i].range + 1),
254                                                GFP_KERNEL);
255                         if (!priv->mib[i])
256                                 return -ENOMEM;
257                         memset(priv->mib[i], 0,
258                                isl_oid[i].size * (isl_oid[i].range + 1));
259                 } else
260                         priv->mib[i] = NULL;
261         }
262
263         init_rwsem(&priv->mib_sem);
264         prism54_mib_init(priv);
265
266         return 0;
267 }
268
269 void
270 mgt_clean(islpci_private *priv)
271 {
272         int i;
273
274         if (!priv->mib)
275                 return;
276         for (i = 0; i < OID_NUM_LAST; i++)
277                 if (priv->mib[i]) {
278                         kfree(priv->mib[i]);
279                         priv->mib[i] = NULL;
280                 }
281         kfree(priv->mib);
282         priv->mib = NULL;
283 }
284
285 void
286 mgt_le_to_cpu(int type, void *data)
287 {
288         switch (type) {
289         case OID_TYPE_U32:
290                 *(u32 *) data = le32_to_cpu(*(u32 *) data);
291                 break;
292         case OID_TYPE_BUFFER:{
293                         struct obj_buffer *buff = data;
294                         buff->size = le32_to_cpu(buff->size);
295                         buff->addr = le32_to_cpu(buff->addr);
296                         break;
297                 }
298         case OID_TYPE_BSS:{
299                         struct obj_bss *bss = data;
300                         bss->age = le16_to_cpu(bss->age);
301                         bss->channel = le16_to_cpu(bss->channel);
302                         bss->capinfo = le16_to_cpu(bss->capinfo);
303                         bss->rates = le16_to_cpu(bss->rates);
304                         bss->basic_rates = le16_to_cpu(bss->basic_rates);
305                         break;
306                 }
307         case OID_TYPE_BSSLIST:{
308                         struct obj_bsslist *list = data;
309                         int i;
310                         list->nr = le32_to_cpu(list->nr);
311                         for (i = 0; i < list->nr; i++)
312                                 mgt_le_to_cpu(OID_TYPE_BSS, &list->bsslist[i]);
313                         break;
314                 }
315         case OID_TYPE_FREQUENCIES:{
316                         struct obj_frequencies *freq = data;
317                         int i;
318                         freq->nr = le16_to_cpu(freq->nr);
319                         for (i = 0; i < freq->nr; i++)
320                                 freq->mhz[i] = le16_to_cpu(freq->mhz[i]);
321                         break;
322                 }
323         case OID_TYPE_MLME:{
324                         struct obj_mlme *mlme = data;
325                         mlme->id = le16_to_cpu(mlme->id);
326                         mlme->state = le16_to_cpu(mlme->state);
327                         mlme->code = le16_to_cpu(mlme->code);
328                         break;
329                 }
330         case OID_TYPE_MLMEEX:{
331                         struct obj_mlmeex *mlme = data;
332                         mlme->id = le16_to_cpu(mlme->id);
333                         mlme->state = le16_to_cpu(mlme->state);
334                         mlme->code = le16_to_cpu(mlme->code);
335                         mlme->size = le16_to_cpu(mlme->size);
336                         break;
337                 }
338         case OID_TYPE_SSID:
339         case OID_TYPE_KEY:
340         case OID_TYPE_ADDR:
341         case OID_TYPE_RAW:
342                 break;
343         default:
344                 BUG();
345         }
346 }
347
348 static void
349 mgt_cpu_to_le(int type, void *data)
350 {
351         switch (type) {
352         case OID_TYPE_U32:
353                 *(u32 *) data = cpu_to_le32(*(u32 *) data);
354                 break;
355         case OID_TYPE_BUFFER:{
356                         struct obj_buffer *buff = data;
357                         buff->size = cpu_to_le32(buff->size);
358                         buff->addr = cpu_to_le32(buff->addr);
359                         break;
360                 }
361         case OID_TYPE_BSS:{
362                         struct obj_bss *bss = data;
363                         bss->age = cpu_to_le16(bss->age);
364                         bss->channel = cpu_to_le16(bss->channel);
365                         bss->capinfo = cpu_to_le16(bss->capinfo);
366                         bss->rates = cpu_to_le16(bss->rates);
367                         bss->basic_rates = cpu_to_le16(bss->basic_rates);
368                         break;
369                 }
370         case OID_TYPE_BSSLIST:{
371                         struct obj_bsslist *list = data;
372                         int i;
373                         list->nr = cpu_to_le32(list->nr);
374                         for (i = 0; i < list->nr; i++)
375                                 mgt_cpu_to_le(OID_TYPE_BSS, &list->bsslist[i]);
376                         break;
377                 }
378         case OID_TYPE_FREQUENCIES:{
379                         struct obj_frequencies *freq = data;
380                         int i;
381                         freq->nr = cpu_to_le16(freq->nr);
382                         for (i = 0; i < freq->nr; i++)
383                                 freq->mhz[i] = cpu_to_le16(freq->mhz[i]);
384                         break;
385                 }
386         case OID_TYPE_MLME:{
387                         struct obj_mlme *mlme = data;
388                         mlme->id = cpu_to_le16(mlme->id);
389                         mlme->state = cpu_to_le16(mlme->state);
390                         mlme->code = cpu_to_le16(mlme->code);
391                         break;
392                 }
393         case OID_TYPE_MLMEEX:{
394                         struct obj_mlmeex *mlme = data;
395                         mlme->id = cpu_to_le16(mlme->id);
396                         mlme->state = cpu_to_le16(mlme->state);
397                         mlme->code = cpu_to_le16(mlme->code);
398                         mlme->size = cpu_to_le16(mlme->size);
399                         break;
400                 }
401         case OID_TYPE_SSID:
402         case OID_TYPE_KEY:
403         case OID_TYPE_ADDR:
404         case OID_TYPE_RAW:
405                 break;
406         default:
407                 BUG();
408         }
409 }
410
411 /* Note : data is modified during this function */
412
413 int
414 mgt_set_request(islpci_private *priv, enum oid_num_t n, int extra, void *data)
415 {
416         int ret = 0;
417         struct islpci_mgmtframe *response;
418         int response_op = PIMFOR_OP_ERROR;
419         int dlen;
420         void *cache, *_data = data;
421         u32 oid;
422
423         BUG_ON(OID_NUM_LAST <= n);
424         BUG_ON(extra > isl_oid[n].range);
425
426         if (!priv->mib)
427                 /* memory has been freed */
428                 return -1;
429
430         dlen = isl_oid[n].size;
431         cache = priv->mib[n];
432         cache += (cache ? extra * dlen : 0);
433         oid = isl_oid[n].oid + extra;
434
435         if (_data == NULL)
436                 /* we are requested to re-set a cached value */
437                 _data = cache;
438         else
439                 mgt_cpu_to_le(isl_oid[n].flags & OID_FLAG_TYPE, _data);
440         /* If we are going to write to the cache, we don't want anyone to read
441          * it -> acquire write lock.
442          * Else we could acquire a read lock to be sure we don't bother the
443          * commit process (which takes a write lock). But I'm not sure if it's
444          * needed.
445          */
446         if (cache)
447                 down_write(&priv->mib_sem);
448
449         if (islpci_get_state(priv) >= PRV_STATE_READY) {
450                 ret = islpci_mgt_transaction(priv->ndev, PIMFOR_OP_SET, oid,
451                                              _data, dlen, &response);
452                 if (!ret) {
453                         response_op = response->header->operation;
454                         islpci_mgt_release(response);
455                 }
456                 if (ret || response_op == PIMFOR_OP_ERROR)
457                         ret = -EIO;
458         } else if (!cache)
459                 ret = -EIO;
460
461         if (cache) {
462                 if (!ret && data)
463                         memcpy(cache, _data, dlen);
464                 up_write(&priv->mib_sem);
465         }
466
467         /* re-set given data to what it was */
468         if (data)
469                 mgt_le_to_cpu(isl_oid[n].flags & OID_FLAG_TYPE, data);
470
471         return ret;
472 }
473
474 int
475 mgt_get_request(islpci_private *priv, enum oid_num_t n, int extra, void *data,
476                 union oid_res_t *res)
477 {
478
479         int ret = -EIO;
480         int reslen = 0;
481         struct islpci_mgmtframe *response = NULL;
482
483         int dlen;
484         void *cache, *_res = NULL;
485         u32 oid;
486
487         BUG_ON(OID_NUM_LAST <= n);
488         BUG_ON(extra > isl_oid[n].range);
489
490         if (!priv->mib)
491                 /* memory has been freed */
492                 return -1;
493
494         dlen = isl_oid[n].size;
495         cache = priv->mib[n];
496         cache += cache ? extra * dlen : 0;
497         oid = isl_oid[n].oid + extra;
498         reslen = dlen;
499
500         if (cache)
501                 down_read(&priv->mib_sem);
502
503         if (islpci_get_state(priv) >= PRV_STATE_READY) {
504                 ret = islpci_mgt_transaction(priv->ndev, PIMFOR_OP_GET,
505                                              oid, data, dlen, &response);
506                 if (ret || !response ||
507                     response->header->operation == PIMFOR_OP_ERROR) {
508                         if (response)
509                                 islpci_mgt_release(response);
510                         ret = -EIO;
511                 }
512                 if (!ret) {
513                         _res = response->data;
514                         reslen = response->header->length;
515                 }
516         } else if (cache) {
517                 _res = cache;
518                 ret = 0;
519         }
520         if ((isl_oid[n].flags & OID_FLAG_TYPE) == OID_TYPE_U32)
521                 res->u = ret ? 0 : le32_to_cpu(*(u32 *) _res);
522         else {
523                 res->ptr = kmalloc(reslen, GFP_KERNEL);
524                 BUG_ON(res->ptr == NULL);
525                 if (ret)
526                         memset(res->ptr, 0, reslen);
527                 else {
528                         memcpy(res->ptr, _res, reslen);
529                         mgt_le_to_cpu(isl_oid[n].flags & OID_FLAG_TYPE,
530                                       res->ptr);
531                 }
532         }
533         if (cache)
534                 up_read(&priv->mib_sem);
535
536         if (response && !ret)
537                 islpci_mgt_release(response);
538
539         if (reslen > isl_oid[n].size)
540                 printk(KERN_DEBUG
541                        "mgt_get_request(0x%x): received data length was bigger "
542                        "than expected (%d > %d). Memory is probably corrupted...",
543                        oid, reslen, isl_oid[n].size);
544
545         return ret;
546 }
547
548 /* lock outside */
549 int
550 mgt_commit_list(islpci_private *priv, enum oid_num_t *l, int n)
551 {
552         int i, ret = 0;
553         struct islpci_mgmtframe *response;
554
555         for (i = 0; i < n; i++) {
556                 struct oid_t *t = &(isl_oid[l[i]]);
557                 void *data = priv->mib[l[i]];
558                 int j = 0;
559                 u32 oid = t->oid;
560                 BUG_ON(data == NULL);
561                 while (j <= t->range) {
562                         response = NULL;
563                         ret |= islpci_mgt_transaction(priv->ndev, PIMFOR_OP_SET,
564                                                       oid, data, t->size,
565                                                       &response);
566                         if (response) {
567                                 ret |= (response->header->operation ==
568                                         PIMFOR_OP_ERROR);
569                                 islpci_mgt_release(response);
570                         }
571                         j++;
572                         oid++;
573                         data += t->size;
574                 }
575         }
576         return ret;
577 }
578
579 /* Lock outside */
580
581 void
582 mgt_set(islpci_private *priv, enum oid_num_t n, void *data)
583 {
584         BUG_ON(OID_NUM_LAST <= n);
585         BUG_ON(priv->mib[n] == NULL);
586
587         memcpy(priv->mib[n], data, isl_oid[n].size);
588         mgt_cpu_to_le(isl_oid[n].flags & OID_FLAG_TYPE, priv->mib[n]);
589 }
590
591 void
592 mgt_get(islpci_private *priv, enum oid_num_t n, void *res)
593 {
594         BUG_ON(OID_NUM_LAST <= n);
595         BUG_ON(priv->mib[n] == NULL);
596         BUG_ON(res == NULL);
597
598         memcpy(res, priv->mib[n], isl_oid[n].size);
599         mgt_le_to_cpu(isl_oid[n].flags & OID_FLAG_TYPE, res);
600 }
601
602 /* Commits the cache. Lock outside. */
603
604 static enum oid_num_t commit_part1[] = {
605         OID_INL_CONFIG,
606         OID_INL_MODE,
607         DOT11_OID_BSSTYPE,
608         DOT11_OID_CHANNEL,
609         DOT11_OID_MLMEAUTOLEVEL
610 };
611
612 static enum oid_num_t commit_part2[] = {
613         DOT11_OID_SSID,
614         DOT11_OID_PSMBUFFER,
615         DOT11_OID_AUTHENABLE,
616         DOT11_OID_PRIVACYINVOKED,
617         DOT11_OID_EXUNENCRYPTED,
618         DOT11_OID_DEFKEYX,      /* MULTIPLE */
619         DOT11_OID_DEFKEYID,
620         DOT11_OID_DOT1XENABLE,
621         OID_INL_DOT11D_CONFORMANCE,
622         OID_INL_OUTPUTPOWER,
623 };
624
625 /* update the MAC addr. */
626 static int
627 mgt_update_addr(islpci_private *priv)
628 {
629         struct islpci_mgmtframe *res;
630         int ret;
631
632         ret = islpci_mgt_transaction(priv->ndev, PIMFOR_OP_GET,
633                                      isl_oid[GEN_OID_MACADDRESS].oid, NULL,
634                                      isl_oid[GEN_OID_MACADDRESS].size, &res);
635
636         if ((ret == 0) && res && (res->header->operation != PIMFOR_OP_ERROR))
637                 memcpy(priv->ndev->dev_addr, res->data, 6);
638         else
639                 ret = -EIO;
640         if (res)
641                 islpci_mgt_release(res);
642
643         return ret;
644 }
645
646 void
647 mgt_commit(islpci_private *priv)
648 {
649         int rvalue;
650         u32 u;
651
652         if (islpci_get_state(priv) < PRV_STATE_INIT)
653                 return;
654
655         rvalue = mgt_commit_list(priv, commit_part1,
656                                  sizeof (commit_part1) /
657                                  sizeof (commit_part1[0]));
658
659         if (priv->iw_mode != IW_MODE_MONITOR)
660                 rvalue |= mgt_commit_list(priv, commit_part2,
661                                           sizeof (commit_part2) /
662                                           sizeof (commit_part2[0]));
663
664         u = OID_INL_MODE;
665         rvalue |= mgt_commit_list(priv, &u, 1);
666         rvalue |= mgt_update_addr(priv);
667
668         if (rvalue) {
669                 /* some request have failed. The device might be in an
670                    incoherent state. We should reset it ! */
671                 printk(KERN_DEBUG "%s: mgt_commit has failed. Restart the "
672                        "device \n", priv->ndev->name);
673         }
674 }
675
676 /* This will tell you if you are allowed to answer a mlme(ex) request .*/
677
678 inline int
679 mgt_mlme_answer(islpci_private *priv)
680 {
681         u32 mlmeautolevel;
682         /* Acquire a read lock because if we are in a mode change, it's
683          * possible to answer true, while the card is leaving master to managed
684          * mode. Answering to a mlme in this situation could hang the card.
685          */
686         down_read(&priv->mib_sem);
687         mlmeautolevel =
688             le32_to_cpu(*(u32 *) priv->mib[DOT11_OID_MLMEAUTOLEVEL]);
689         up_read(&priv->mib_sem);
690
691         return ((priv->iw_mode == IW_MODE_MASTER) &&
692                 (mlmeautolevel >= DOT11_MLME_INTERMEDIATE));
693 }
694
695 inline enum oid_num_t
696 mgt_oidtonum(u32 oid)
697 {
698         int i;
699
700         for (i = 0; i < OID_NUM_LAST; i++)
701                 if (isl_oid[i].oid == oid)
702                         return i;
703
704         printk(KERN_DEBUG "looking for an unknown oid 0x%x", oid);
705
706         return OID_NUM_LAST;
707 }
708
709 int
710 mgt_response_to_str(enum oid_num_t n, union oid_res_t *r, char *str)
711 {
712         switch (isl_oid[n].flags & OID_FLAG_TYPE) {
713         case OID_TYPE_U32:
714                 return snprintf(str, PRIV_STR_SIZE, "%u\n", r->u);
715                 break;
716         case OID_TYPE_BUFFER:{
717                         struct obj_buffer *buff = r->ptr;
718                         return snprintf(str, PRIV_STR_SIZE,
719                                         "size=%u\naddr=0x%X\n", buff->size,
720                                         buff->addr);
721                 }
722                 break;
723         case OID_TYPE_BSS:{
724                         struct obj_bss *bss = r->ptr;
725                         return snprintf(str, PRIV_STR_SIZE,
726                                         "age=%u\nchannel=%u\n"
727                                         "capinfo=0x%X\nrates=0x%X\n"
728                                         "basic_rates=0x%X\n", bss->age,
729                                         bss->channel, bss->capinfo,
730                                         bss->rates, bss->basic_rates);
731                 }
732                 break;
733         case OID_TYPE_BSSLIST:{
734                         struct obj_bsslist *list = r->ptr;
735                         int i, k;
736                         k = snprintf(str, PRIV_STR_SIZE, "nr=%u\n", list->nr);
737                         for (i = 0; i < list->nr; i++)
738                                 k += snprintf(str + k, PRIV_STR_SIZE - k,
739                                               "bss[%u] : \nage=%u\nchannel=%u\n"
740                                               "capinfo=0x%X\nrates=0x%X\n"
741                                               "basic_rates=0x%X\n",
742                                               i, list->bsslist[i].age,
743                                               list->bsslist[i].channel,
744                                               list->bsslist[i].capinfo,
745                                               list->bsslist[i].rates,
746                                               list->bsslist[i].basic_rates);
747                         return k;
748                 }
749                 break;
750         case OID_TYPE_FREQUENCIES:{
751                         struct obj_frequencies *freq = r->ptr;
752                         int i, t;
753                         printk("nr : %u\n", freq->nr);
754                         t = snprintf(str, PRIV_STR_SIZE, "nr=%u\n", freq->nr);
755                         for (i = 0; i < freq->nr; i++)
756                                 t += snprintf(str + t, PRIV_STR_SIZE - t,
757                                               "mhz[%u]=%u\n", i, freq->mhz[i]);
758                         return t;
759                 }
760                 break;
761         case OID_TYPE_MLME:{
762                         struct obj_mlme *mlme = r->ptr;
763                         return snprintf(str, PRIV_STR_SIZE,
764                                         "id=0x%X\nstate=0x%X\ncode=0x%X\n",
765                                         mlme->id, mlme->state, mlme->code);
766                 }
767                 break;
768         case OID_TYPE_MLMEEX:{
769                         struct obj_mlmeex *mlme = r->ptr;
770                         return snprintf(str, PRIV_STR_SIZE,
771                                         "id=0x%X\nstate=0x%X\n"
772                                         "code=0x%X\nsize=0x%X\n", mlme->id,
773                                         mlme->state, mlme->code, mlme->size);
774                 }
775                 break;
776         case OID_TYPE_SSID:{
777                         struct obj_ssid *ssid = r->ptr;
778                         return snprintf(str, PRIV_STR_SIZE,
779                                         "length=%u\noctets=%s\n",
780                                         ssid->length, ssid->octets);
781                 }
782                 break;
783         case OID_TYPE_KEY:{
784                         struct obj_key *key = r->ptr;
785                         int t, i;
786                         t = snprintf(str, PRIV_STR_SIZE,
787                                      "type=0x%X\nlength=0x%X\nkey=0x",
788                                      key->type, key->length);
789                         for (i = 0; i < key->length; i++)
790                                 t += snprintf(str + t, PRIV_STR_SIZE - t,
791                                               "%02X:", key->key[i]);
792                         t += snprintf(str + t, PRIV_STR_SIZE - t, "\n");
793                         return t;
794                 }
795                 break;
796         case OID_TYPE_RAW:
797         case OID_TYPE_ADDR:{
798                         unsigned char *buff = r->ptr;
799                         int t, i;
800                         t = snprintf(str, PRIV_STR_SIZE, "hex data=");
801                         for (i = 0; i < isl_oid[n].size; i++)
802                                 t += snprintf(str + t, PRIV_STR_SIZE - t,
803                                               "%02X:", buff[i]);
804                         t += snprintf(str + t, PRIV_STR_SIZE - t, "\n");
805                         return t;
806                 }
807                 break;
808         default:
809                 BUG();
810         }
811         return 0;
812 }