[CRYPTO]: crypto_free_tfm() callers no longer need to check for NULL
[safe/jmp/linux-2.6] / drivers / net / wireless / airo.c
1 /*======================================================================
2
3     Aironet driver for 4500 and 4800 series cards
4
5     This code is released under both the GPL version 2 and BSD licenses.
6     Either license may be used.  The respective licenses are found at
7     the end of this file.
8
9     This code was developed by Benjamin Reed <breed@users.sourceforge.net>
10     including portions of which come from the Aironet PC4500
11     Developer's Reference Manual and used with permission.  Copyright
12     (C) 1999 Benjamin Reed.  All Rights Reserved.  Permission to use
13     code in the Developer's manual was granted for this driver by
14     Aironet.  Major code contributions were received from Javier Achirica
15     <achirica@users.sourceforge.net> and Jean Tourrilhes <jt@hpl.hp.com>.
16     Code was also integrated from the Cisco Aironet driver for Linux.
17     Support for MPI350 cards was added by Fabrice Bellet
18     <fabrice@bellet.info>.
19
20 ======================================================================*/
21
22 #include <linux/config.h>
23 #include <linux/init.h>
24
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/proc_fs.h>
28 #include <linux/smp_lock.h>
29
30 #include <linux/sched.h>
31 #include <linux/ptrace.h>
32 #include <linux/slab.h>
33 #include <linux/string.h>
34 #include <linux/timer.h>
35 #include <linux/interrupt.h>
36 #include <linux/in.h>
37 #include <linux/bitops.h>
38 #include <asm/io.h>
39 #include <asm/system.h>
40
41 #include <linux/netdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/if_arp.h>
45 #include <linux/ioport.h>
46 #include <linux/pci.h>
47 #include <asm/uaccess.h>
48
49 #ifdef CONFIG_PCI
50 static struct pci_device_id card_ids[] = {
51         { 0x14b9, 1, PCI_ANY_ID, PCI_ANY_ID, },
52         { 0x14b9, 0x4500, PCI_ANY_ID, PCI_ANY_ID },
53         { 0x14b9, 0x4800, PCI_ANY_ID, PCI_ANY_ID, },
54         { 0x14b9, 0x0340, PCI_ANY_ID, PCI_ANY_ID, },
55         { 0x14b9, 0x0350, PCI_ANY_ID, PCI_ANY_ID, },
56         { 0x14b9, 0x5000, PCI_ANY_ID, PCI_ANY_ID, },
57         { 0x14b9, 0xa504, PCI_ANY_ID, PCI_ANY_ID, },
58         { 0, }
59 };
60 MODULE_DEVICE_TABLE(pci, card_ids);
61
62 static int airo_pci_probe(struct pci_dev *, const struct pci_device_id *);
63 static void airo_pci_remove(struct pci_dev *);
64 static int airo_pci_suspend(struct pci_dev *pdev, pm_message_t state);
65 static int airo_pci_resume(struct pci_dev *pdev);
66
67 static struct pci_driver airo_driver = {
68         .name     = "airo",
69         .id_table = card_ids,
70         .probe    = airo_pci_probe,
71         .remove   = __devexit_p(airo_pci_remove),
72         .suspend  = airo_pci_suspend,
73         .resume   = airo_pci_resume,
74 };
75 #endif /* CONFIG_PCI */
76
77 /* Include Wireless Extension definition and check version - Jean II */
78 #include <linux/wireless.h>
79 #define WIRELESS_SPY            // enable iwspy support
80 #include <net/iw_handler.h>     // New driver API
81
82 #define CISCO_EXT               // enable Cisco extensions
83 #ifdef CISCO_EXT
84 #include <linux/delay.h>
85 #endif
86
87 /* Support Cisco MIC feature */
88 #define MICSUPPORT
89
90 #if defined(MICSUPPORT) && !defined(CONFIG_CRYPTO)
91 #warning MIC support requires Crypto API
92 #undef MICSUPPORT
93 #endif
94
95 /* Hack to do some power saving */
96 #define POWER_ON_DOWN
97
98 /* As you can see this list is HUGH!
99    I really don't know what a lot of these counts are about, but they
100    are all here for completeness.  If the IGNLABEL macro is put in
101    infront of the label, that statistic will not be included in the list
102    of statistics in the /proc filesystem */
103
104 #define IGNLABEL(comment) NULL
105 static char *statsLabels[] = {
106         "RxOverrun",
107         IGNLABEL("RxPlcpCrcErr"),
108         IGNLABEL("RxPlcpFormatErr"),
109         IGNLABEL("RxPlcpLengthErr"),
110         "RxMacCrcErr",
111         "RxMacCrcOk",
112         "RxWepErr",
113         "RxWepOk",
114         "RetryLong",
115         "RetryShort",
116         "MaxRetries",
117         "NoAck",
118         "NoCts",
119         "RxAck",
120         "RxCts",
121         "TxAck",
122         "TxRts",
123         "TxCts",
124         "TxMc",
125         "TxBc",
126         "TxUcFrags",
127         "TxUcPackets",
128         "TxBeacon",
129         "RxBeacon",
130         "TxSinColl",
131         "TxMulColl",
132         "DefersNo",
133         "DefersProt",
134         "DefersEngy",
135         "DupFram",
136         "RxFragDisc",
137         "TxAged",
138         "RxAged",
139         "LostSync-MaxRetry",
140         "LostSync-MissedBeacons",
141         "LostSync-ArlExceeded",
142         "LostSync-Deauth",
143         "LostSync-Disassoced",
144         "LostSync-TsfTiming",
145         "HostTxMc",
146         "HostTxBc",
147         "HostTxUc",
148         "HostTxFail",
149         "HostRxMc",
150         "HostRxBc",
151         "HostRxUc",
152         "HostRxDiscard",
153         IGNLABEL("HmacTxMc"),
154         IGNLABEL("HmacTxBc"),
155         IGNLABEL("HmacTxUc"),
156         IGNLABEL("HmacTxFail"),
157         IGNLABEL("HmacRxMc"),
158         IGNLABEL("HmacRxBc"),
159         IGNLABEL("HmacRxUc"),
160         IGNLABEL("HmacRxDiscard"),
161         IGNLABEL("HmacRxAccepted"),
162         "SsidMismatch",
163         "ApMismatch",
164         "RatesMismatch",
165         "AuthReject",
166         "AuthTimeout",
167         "AssocReject",
168         "AssocTimeout",
169         IGNLABEL("ReasonOutsideTable"),
170         IGNLABEL("ReasonStatus1"),
171         IGNLABEL("ReasonStatus2"),
172         IGNLABEL("ReasonStatus3"),
173         IGNLABEL("ReasonStatus4"),
174         IGNLABEL("ReasonStatus5"),
175         IGNLABEL("ReasonStatus6"),
176         IGNLABEL("ReasonStatus7"),
177         IGNLABEL("ReasonStatus8"),
178         IGNLABEL("ReasonStatus9"),
179         IGNLABEL("ReasonStatus10"),
180         IGNLABEL("ReasonStatus11"),
181         IGNLABEL("ReasonStatus12"),
182         IGNLABEL("ReasonStatus13"),
183         IGNLABEL("ReasonStatus14"),
184         IGNLABEL("ReasonStatus15"),
185         IGNLABEL("ReasonStatus16"),
186         IGNLABEL("ReasonStatus17"),
187         IGNLABEL("ReasonStatus18"),
188         IGNLABEL("ReasonStatus19"),
189         "RxMan",
190         "TxMan",
191         "RxRefresh",
192         "TxRefresh",
193         "RxPoll",
194         "TxPoll",
195         "HostRetries",
196         "LostSync-HostReq",
197         "HostTxBytes",
198         "HostRxBytes",
199         "ElapsedUsec",
200         "ElapsedSec",
201         "LostSyncBetterAP",
202         "PrivacyMismatch",
203         "Jammed",
204         "DiscRxNotWepped",
205         "PhyEleMismatch",
206         (char*)-1 };
207 #ifndef RUN_AT
208 #define RUN_AT(x) (jiffies+(x))
209 #endif
210
211
212 /* These variables are for insmod, since it seems that the rates
213    can only be set in setup_card.  Rates should be a comma separated
214    (no spaces) list of rates (up to 8). */
215
216 static int rates[8];
217 static int basic_rate;
218 static char *ssids[3];
219
220 static int io[4];
221 static int irq[4];
222
223 static
224 int maxencrypt /* = 0 */; /* The highest rate that the card can encrypt at.
225                        0 means no limit.  For old cards this was 4 */
226
227 static int auto_wep /* = 0 */; /* If set, it tries to figure out the wep mode */
228 static int aux_bap /* = 0 */; /* Checks to see if the aux ports are needed to read
229                     the bap, needed on some older cards and buses. */
230 static int adhoc;
231
232 static int probe = 1;
233
234 static int proc_uid /* = 0 */;
235
236 static int proc_gid /* = 0 */;
237
238 static int airo_perm = 0555;
239
240 static int proc_perm = 0644;
241
242 MODULE_AUTHOR("Benjamin Reed");
243 MODULE_DESCRIPTION("Support for Cisco/Aironet 802.11 wireless ethernet \
244                    cards.  Direct support for ISA/PCI/MPI cards and support \
245                    for PCMCIA when used with airo_cs.");
246 MODULE_LICENSE("Dual BSD/GPL");
247 MODULE_SUPPORTED_DEVICE("Aironet 4500, 4800 and Cisco 340/350");
248 module_param_array(io, int, NULL, 0);
249 module_param_array(irq, int, NULL, 0);
250 module_param(basic_rate, int, 0);
251 module_param_array(rates, int, NULL, 0);
252 module_param_array(ssids, charp, NULL, 0);
253 module_param(auto_wep, int, 0);
254 MODULE_PARM_DESC(auto_wep, "If non-zero, the driver will keep looping through \
255 the authentication options until an association is made.  The value of \
256 auto_wep is number of the wep keys to check.  A value of 2 will try using \
257 the key at index 0 and index 1.");
258 module_param(aux_bap, int, 0);
259 MODULE_PARM_DESC(aux_bap, "If non-zero, the driver will switch into a mode \
260 than seems to work better for older cards with some older buses.  Before \
261 switching it checks that the switch is needed.");
262 module_param(maxencrypt, int, 0);
263 MODULE_PARM_DESC(maxencrypt, "The maximum speed that the card can do \
264 encryption.  Units are in 512kbs.  Zero (default) means there is no limit. \
265 Older cards used to be limited to 2mbs (4).");
266 module_param(adhoc, int, 0);
267 MODULE_PARM_DESC(adhoc, "If non-zero, the card will start in adhoc mode.");
268 module_param(probe, int, 0);
269 MODULE_PARM_DESC(probe, "If zero, the driver won't start the card.");
270
271 module_param(proc_uid, int, 0);
272 MODULE_PARM_DESC(proc_uid, "The uid that the /proc files will belong to.");
273 module_param(proc_gid, int, 0);
274 MODULE_PARM_DESC(proc_gid, "The gid that the /proc files will belong to.");
275 module_param(airo_perm, int, 0);
276 MODULE_PARM_DESC(airo_perm, "The permission bits of /proc/[driver/]aironet.");
277 module_param(proc_perm, int, 0);
278 MODULE_PARM_DESC(proc_perm, "The permission bits of the files in /proc");
279
280 /* This is a kind of sloppy hack to get this information to OUT4500 and
281    IN4500.  I would be extremely interested in the situation where this
282    doesn't work though!!! */
283 static int do8bitIO = 0;
284
285 /* Return codes */
286 #define SUCCESS 0
287 #define ERROR -1
288 #define NO_PACKET -2
289
290 /* Commands */
291 #define NOP2            0x0000
292 #define MAC_ENABLE      0x0001
293 #define MAC_DISABLE     0x0002
294 #define CMD_LOSE_SYNC   0x0003 /* Not sure what this does... */
295 #define CMD_SOFTRESET   0x0004
296 #define HOSTSLEEP       0x0005
297 #define CMD_MAGIC_PKT   0x0006
298 #define CMD_SETWAKEMASK 0x0007
299 #define CMD_READCFG     0x0008
300 #define CMD_SETMODE     0x0009
301 #define CMD_ALLOCATETX  0x000a
302 #define CMD_TRANSMIT    0x000b
303 #define CMD_DEALLOCATETX 0x000c
304 #define NOP             0x0010
305 #define CMD_WORKAROUND  0x0011
306 #define CMD_ALLOCATEAUX 0x0020
307 #define CMD_ACCESS      0x0021
308 #define CMD_PCIBAP      0x0022
309 #define CMD_PCIAUX      0x0023
310 #define CMD_ALLOCBUF    0x0028
311 #define CMD_GETTLV      0x0029
312 #define CMD_PUTTLV      0x002a
313 #define CMD_DELTLV      0x002b
314 #define CMD_FINDNEXTTLV 0x002c
315 #define CMD_PSPNODES    0x0030
316 #define CMD_SETCW       0x0031    
317 #define CMD_SETPCF      0x0032    
318 #define CMD_SETPHYREG   0x003e
319 #define CMD_TXTEST      0x003f
320 #define MAC_ENABLETX    0x0101
321 #define CMD_LISTBSS     0x0103
322 #define CMD_SAVECFG     0x0108
323 #define CMD_ENABLEAUX   0x0111
324 #define CMD_WRITERID    0x0121
325 #define CMD_USEPSPNODES 0x0130
326 #define MAC_ENABLERX    0x0201
327
328 /* Command errors */
329 #define ERROR_QUALIF 0x00
330 #define ERROR_ILLCMD 0x01
331 #define ERROR_ILLFMT 0x02
332 #define ERROR_INVFID 0x03
333 #define ERROR_INVRID 0x04
334 #define ERROR_LARGE 0x05
335 #define ERROR_NDISABL 0x06
336 #define ERROR_ALLOCBSY 0x07
337 #define ERROR_NORD 0x0B
338 #define ERROR_NOWR 0x0C
339 #define ERROR_INVFIDTX 0x0D
340 #define ERROR_TESTACT 0x0E
341 #define ERROR_TAGNFND 0x12
342 #define ERROR_DECODE 0x20
343 #define ERROR_DESCUNAV 0x21
344 #define ERROR_BADLEN 0x22
345 #define ERROR_MODE 0x80
346 #define ERROR_HOP 0x81
347 #define ERROR_BINTER 0x82
348 #define ERROR_RXMODE 0x83
349 #define ERROR_MACADDR 0x84
350 #define ERROR_RATES 0x85
351 #define ERROR_ORDER 0x86
352 #define ERROR_SCAN 0x87
353 #define ERROR_AUTH 0x88
354 #define ERROR_PSMODE 0x89
355 #define ERROR_RTYPE 0x8A
356 #define ERROR_DIVER 0x8B
357 #define ERROR_SSID 0x8C
358 #define ERROR_APLIST 0x8D
359 #define ERROR_AUTOWAKE 0x8E
360 #define ERROR_LEAP 0x8F
361
362 /* Registers */
363 #define COMMAND 0x00
364 #define PARAM0 0x02
365 #define PARAM1 0x04
366 #define PARAM2 0x06
367 #define STATUS 0x08
368 #define RESP0 0x0a
369 #define RESP1 0x0c
370 #define RESP2 0x0e
371 #define LINKSTAT 0x10
372 #define SELECT0 0x18
373 #define OFFSET0 0x1c
374 #define RXFID 0x20
375 #define TXALLOCFID 0x22
376 #define TXCOMPLFID 0x24
377 #define DATA0 0x36
378 #define EVSTAT 0x30
379 #define EVINTEN 0x32
380 #define EVACK 0x34
381 #define SWS0 0x28
382 #define SWS1 0x2a
383 #define SWS2 0x2c
384 #define SWS3 0x2e
385 #define AUXPAGE 0x3A
386 #define AUXOFF 0x3C
387 #define AUXDATA 0x3E
388
389 #define FID_TX 1
390 #define FID_RX 2
391 /* Offset into aux memory for descriptors */
392 #define AUX_OFFSET 0x800
393 /* Size of allocated packets */
394 #define PKTSIZE 1840
395 #define RIDSIZE 2048
396 /* Size of the transmit queue */
397 #define MAXTXQ 64
398
399 /* BAP selectors */
400 #define BAP0 0 // Used for receiving packets
401 #define BAP1 2 // Used for xmiting packets and working with RIDS
402
403 /* Flags */
404 #define COMMAND_BUSY 0x8000
405
406 #define BAP_BUSY 0x8000
407 #define BAP_ERR 0x4000
408 #define BAP_DONE 0x2000
409
410 #define PROMISC 0xffff
411 #define NOPROMISC 0x0000
412
413 #define EV_CMD 0x10
414 #define EV_CLEARCOMMANDBUSY 0x4000
415 #define EV_RX 0x01
416 #define EV_TX 0x02
417 #define EV_TXEXC 0x04
418 #define EV_ALLOC 0x08
419 #define EV_LINK 0x80
420 #define EV_AWAKE 0x100
421 #define EV_TXCPY 0x400
422 #define EV_UNKNOWN 0x800
423 #define EV_MIC 0x1000 /* Message Integrity Check Interrupt */
424 #define EV_AWAKEN 0x2000
425 #define STATUS_INTS (EV_AWAKE|EV_LINK|EV_TXEXC|EV_TX|EV_TXCPY|EV_RX|EV_MIC)
426
427 #ifdef CHECK_UNKNOWN_INTS
428 #define IGNORE_INTS ( EV_CMD | EV_UNKNOWN)
429 #else
430 #define IGNORE_INTS (~STATUS_INTS)
431 #endif
432
433 /* RID TYPES */
434 #define RID_RW 0x20
435
436 /* The RIDs */
437 #define RID_CAPABILITIES 0xFF00
438 #define RID_APINFO     0xFF01
439 #define RID_RADIOINFO  0xFF02
440 #define RID_UNKNOWN3   0xFF03
441 #define RID_RSSI       0xFF04
442 #define RID_CONFIG     0xFF10
443 #define RID_SSID       0xFF11
444 #define RID_APLIST     0xFF12
445 #define RID_DRVNAME    0xFF13
446 #define RID_ETHERENCAP 0xFF14
447 #define RID_WEP_TEMP   0xFF15
448 #define RID_WEP_PERM   0xFF16
449 #define RID_MODULATION 0xFF17
450 #define RID_OPTIONS    0xFF18
451 #define RID_ACTUALCONFIG 0xFF20 /*readonly*/
452 #define RID_FACTORYCONFIG 0xFF21
453 #define RID_UNKNOWN22  0xFF22
454 #define RID_LEAPUSERNAME 0xFF23
455 #define RID_LEAPPASSWORD 0xFF24
456 #define RID_STATUS     0xFF50
457 #define RID_BEACON_HST 0xFF51
458 #define RID_BUSY_HST   0xFF52
459 #define RID_RETRIES_HST 0xFF53
460 #define RID_UNKNOWN54  0xFF54
461 #define RID_UNKNOWN55  0xFF55
462 #define RID_UNKNOWN56  0xFF56
463 #define RID_MIC        0xFF57
464 #define RID_STATS16    0xFF60
465 #define RID_STATS16DELTA 0xFF61
466 #define RID_STATS16DELTACLEAR 0xFF62
467 #define RID_STATS      0xFF68
468 #define RID_STATSDELTA 0xFF69
469 #define RID_STATSDELTACLEAR 0xFF6A
470 #define RID_ECHOTEST_RID 0xFF70
471 #define RID_ECHOTEST_RESULTS 0xFF71
472 #define RID_BSSLISTFIRST 0xFF72
473 #define RID_BSSLISTNEXT  0xFF73
474
475 typedef struct {
476         u16 cmd;
477         u16 parm0;
478         u16 parm1;
479         u16 parm2;
480 } Cmd;
481
482 typedef struct {
483         u16 status;
484         u16 rsp0;
485         u16 rsp1;
486         u16 rsp2;
487 } Resp;
488
489 /*
490  * Rids and endian-ness:  The Rids will always be in cpu endian, since
491  * this all the patches from the big-endian guys end up doing that.
492  * so all rid access should use the read/writeXXXRid routines.
493  */
494
495 /* This is redundant for x86 archs, but it seems necessary for ARM */
496 #pragma pack(1)
497
498 /* This structure came from an email sent to me from an engineer at
499    aironet for inclusion into this driver */
500 typedef struct {
501         u16 len;
502         u16 kindex;
503         u8 mac[ETH_ALEN];
504         u16 klen;
505         u8 key[16];
506 } WepKeyRid;
507
508 /* These structures are from the Aironet's PC4500 Developers Manual */
509 typedef struct {
510         u16 len;
511         u8 ssid[32];
512 } Ssid;
513
514 typedef struct {
515         u16 len;
516         Ssid ssids[3];
517 } SsidRid;
518
519 typedef struct {
520         u16 len;
521         u16 modulation;
522 #define MOD_DEFAULT 0
523 #define MOD_CCK 1
524 #define MOD_MOK 2
525 } ModulationRid;
526
527 typedef struct {
528         u16 len; /* sizeof(ConfigRid) */
529         u16 opmode; /* operating mode */
530 #define MODE_STA_IBSS 0
531 #define MODE_STA_ESS 1
532 #define MODE_AP 2
533 #define MODE_AP_RPTR 3
534 #define MODE_ETHERNET_HOST (0<<8) /* rx payloads converted */
535 #define MODE_LLC_HOST (1<<8) /* rx payloads left as is */
536 #define MODE_AIRONET_EXTEND (1<<9) /* enable Aironet extenstions */
537 #define MODE_AP_INTERFACE (1<<10) /* enable ap interface extensions */
538 #define MODE_ANTENNA_ALIGN (1<<11) /* enable antenna alignment */
539 #define MODE_ETHER_LLC (1<<12) /* enable ethernet LLC */
540 #define MODE_LEAF_NODE (1<<13) /* enable leaf node bridge */
541 #define MODE_CF_POLLABLE (1<<14) /* enable CF pollable */
542 #define MODE_MIC (1<<15) /* enable MIC */
543         u16 rmode; /* receive mode */
544 #define RXMODE_BC_MC_ADDR 0
545 #define RXMODE_BC_ADDR 1 /* ignore multicasts */
546 #define RXMODE_ADDR 2 /* ignore multicast and broadcast */
547 #define RXMODE_RFMON 3 /* wireless monitor mode */
548 #define RXMODE_RFMON_ANYBSS 4
549 #define RXMODE_LANMON 5 /* lan style monitor -- data packets only */
550 #define RXMODE_DISABLE_802_3_HEADER (1<<8) /* disables 802.3 header on rx */
551 #define RXMODE_NORMALIZED_RSSI (1<<9) /* return normalized RSSI */
552         u16 fragThresh;
553         u16 rtsThres;
554         u8 macAddr[ETH_ALEN];
555         u8 rates[8];
556         u16 shortRetryLimit;
557         u16 longRetryLimit;
558         u16 txLifetime; /* in kusec */
559         u16 rxLifetime; /* in kusec */
560         u16 stationary;
561         u16 ordering;
562         u16 u16deviceType; /* for overriding device type */
563         u16 cfpRate;
564         u16 cfpDuration;
565         u16 _reserved1[3];
566         /*---------- Scanning/Associating ----------*/
567         u16 scanMode;
568 #define SCANMODE_ACTIVE 0
569 #define SCANMODE_PASSIVE 1
570 #define SCANMODE_AIROSCAN 2
571         u16 probeDelay; /* in kusec */
572         u16 probeEnergyTimeout; /* in kusec */
573         u16 probeResponseTimeout;
574         u16 beaconListenTimeout;
575         u16 joinNetTimeout;
576         u16 authTimeout;
577         u16 authType;
578 #define AUTH_OPEN 0x1
579 #define AUTH_ENCRYPT 0x101
580 #define AUTH_SHAREDKEY 0x102
581 #define AUTH_ALLOW_UNENCRYPTED 0x200
582         u16 associationTimeout;
583         u16 specifiedApTimeout;
584         u16 offlineScanInterval;
585         u16 offlineScanDuration;
586         u16 linkLossDelay;
587         u16 maxBeaconLostTime;
588         u16 refreshInterval;
589 #define DISABLE_REFRESH 0xFFFF
590         u16 _reserved1a[1];
591         /*---------- Power save operation ----------*/
592         u16 powerSaveMode;
593 #define POWERSAVE_CAM 0
594 #define POWERSAVE_PSP 1
595 #define POWERSAVE_PSPCAM 2
596         u16 sleepForDtims;
597         u16 listenInterval;
598         u16 fastListenInterval;
599         u16 listenDecay;
600         u16 fastListenDelay;
601         u16 _reserved2[2];
602         /*---------- Ap/Ibss config items ----------*/
603         u16 beaconPeriod;
604         u16 atimDuration;
605         u16 hopPeriod;
606         u16 channelSet;
607         u16 channel;
608         u16 dtimPeriod;
609         u16 bridgeDistance;
610         u16 radioID;
611         /*---------- Radio configuration ----------*/
612         u16 radioType;
613 #define RADIOTYPE_DEFAULT 0
614 #define RADIOTYPE_802_11 1
615 #define RADIOTYPE_LEGACY 2
616         u8 rxDiversity;
617         u8 txDiversity;
618         u16 txPower;
619 #define TXPOWER_DEFAULT 0
620         u16 rssiThreshold;
621 #define RSSI_DEFAULT 0
622         u16 modulation;
623 #define PREAMBLE_AUTO 0
624 #define PREAMBLE_LONG 1
625 #define PREAMBLE_SHORT 2
626         u16 preamble;
627         u16 homeProduct;
628         u16 radioSpecific;
629         /*---------- Aironet Extensions ----------*/
630         u8 nodeName[16];
631         u16 arlThreshold;
632         u16 arlDecay;
633         u16 arlDelay;
634         u16 _reserved4[1];
635         /*---------- Aironet Extensions ----------*/
636         u8 magicAction;
637 #define MAGIC_ACTION_STSCHG 1
638 #define MAGIC_ACTION_RESUME 2
639 #define MAGIC_IGNORE_MCAST (1<<8)
640 #define MAGIC_IGNORE_BCAST (1<<9)
641 #define MAGIC_SWITCH_TO_PSP (0<<10)
642 #define MAGIC_STAY_IN_CAM (1<<10)
643         u8 magicControl;
644         u16 autoWake;
645 } ConfigRid;
646
647 typedef struct {
648         u16 len;
649         u8 mac[ETH_ALEN];
650         u16 mode;
651         u16 errorCode;
652         u16 sigQuality;
653         u16 SSIDlen;
654         char SSID[32];
655         char apName[16];
656         u8 bssid[4][ETH_ALEN];
657         u16 beaconPeriod;
658         u16 dimPeriod;
659         u16 atimDuration;
660         u16 hopPeriod;
661         u16 channelSet;
662         u16 channel;
663         u16 hopsToBackbone;
664         u16 apTotalLoad;
665         u16 generatedLoad;
666         u16 accumulatedArl;
667         u16 signalQuality;
668         u16 currentXmitRate;
669         u16 apDevExtensions;
670         u16 normalizedSignalStrength;
671         u16 shortPreamble;
672         u8 apIP[4];
673         u8 noisePercent; /* Noise percent in last second */
674         u8 noisedBm; /* Noise dBm in last second */
675         u8 noiseAvePercent; /* Noise percent in last minute */
676         u8 noiseAvedBm; /* Noise dBm in last minute */
677         u8 noiseMaxPercent; /* Highest noise percent in last minute */
678         u8 noiseMaxdBm; /* Highest noise dbm in last minute */
679         u16 load;
680         u8 carrier[4];
681         u16 assocStatus;
682 #define STAT_NOPACKETS 0
683 #define STAT_NOCARRIERSET 10
684 #define STAT_GOTCARRIERSET 11
685 #define STAT_WRONGSSID 20
686 #define STAT_BADCHANNEL 25
687 #define STAT_BADBITRATES 30
688 #define STAT_BADPRIVACY 35
689 #define STAT_APFOUND 40
690 #define STAT_APREJECTED 50
691 #define STAT_AUTHENTICATING 60
692 #define STAT_DEAUTHENTICATED 61
693 #define STAT_AUTHTIMEOUT 62
694 #define STAT_ASSOCIATING 70
695 #define STAT_DEASSOCIATED 71
696 #define STAT_ASSOCTIMEOUT 72
697 #define STAT_NOTAIROAP 73
698 #define STAT_ASSOCIATED 80
699 #define STAT_LEAPING 90
700 #define STAT_LEAPFAILED 91
701 #define STAT_LEAPTIMEDOUT 92
702 #define STAT_LEAPCOMPLETE 93
703 } StatusRid;
704
705 typedef struct {
706         u16 len;
707         u16 spacer;
708         u32 vals[100];
709 } StatsRid;
710
711
712 typedef struct {
713         u16 len;
714         u8 ap[4][ETH_ALEN];
715 } APListRid;
716
717 typedef struct {
718         u16 len;
719         char oui[3];
720         char zero;
721         u16 prodNum;
722         char manName[32];
723         char prodName[16];
724         char prodVer[8];
725         char factoryAddr[ETH_ALEN];
726         char aironetAddr[ETH_ALEN];
727         u16 radioType;
728         u16 country;
729         char callid[ETH_ALEN];
730         char supportedRates[8];
731         char rxDiversity;
732         char txDiversity;
733         u16 txPowerLevels[8];
734         u16 hardVer;
735         u16 hardCap;
736         u16 tempRange;
737         u16 softVer;
738         u16 softSubVer;
739         u16 interfaceVer;
740         u16 softCap;
741         u16 bootBlockVer;
742         u16 requiredHard;
743         u16 extSoftCap;
744 } CapabilityRid;
745
746 typedef struct {
747   u16 len;
748   u16 index; /* First is 0 and 0xffff means end of list */
749 #define RADIO_FH 1 /* Frequency hopping radio type */
750 #define RADIO_DS 2 /* Direct sequence radio type */
751 #define RADIO_TMA 4 /* Proprietary radio used in old cards (2500) */
752   u16 radioType;
753   u8 bssid[ETH_ALEN]; /* Mac address of the BSS */
754   u8 zero;
755   u8 ssidLen;
756   u8 ssid[32];
757   u16 dBm;
758 #define CAP_ESS (1<<0)
759 #define CAP_IBSS (1<<1)
760 #define CAP_PRIVACY (1<<4)
761 #define CAP_SHORTHDR (1<<5)
762   u16 cap;
763   u16 beaconInterval;
764   u8 rates[8]; /* Same as rates for config rid */
765   struct { /* For frequency hopping only */
766     u16 dwell;
767     u8 hopSet;
768     u8 hopPattern;
769     u8 hopIndex;
770     u8 fill;
771   } fh;
772   u16 dsChannel;
773   u16 atimWindow;
774 } BSSListRid;
775
776 typedef struct {
777   u8 rssipct;
778   u8 rssidBm;
779 } tdsRssiEntry;
780
781 typedef struct {
782   u16 len;
783   tdsRssiEntry x[256];
784 } tdsRssiRid;
785
786 typedef struct {
787         u16 len;
788         u16 state;
789         u16 multicastValid;
790         u8  multicast[16];
791         u16 unicastValid;
792         u8  unicast[16];
793 } MICRid;
794
795 typedef struct {
796         u16 typelen;
797
798         union {
799             u8 snap[8];
800             struct {
801                 u8 dsap;
802                 u8 ssap;
803                 u8 control;
804                 u8 orgcode[3];
805                 u8 fieldtype[2];
806             } llc;
807         } u;
808         u32 mic;
809         u32 seq;
810 } MICBuffer;
811
812 typedef struct {
813         u8 da[ETH_ALEN];
814         u8 sa[ETH_ALEN];
815 } etherHead;
816
817 #pragma pack()
818
819 #define TXCTL_TXOK (1<<1) /* report if tx is ok */
820 #define TXCTL_TXEX (1<<2) /* report if tx fails */
821 #define TXCTL_802_3 (0<<3) /* 802.3 packet */
822 #define TXCTL_802_11 (1<<3) /* 802.11 mac packet */
823 #define TXCTL_ETHERNET (0<<4) /* payload has ethertype */
824 #define TXCTL_LLC (1<<4) /* payload is llc */
825 #define TXCTL_RELEASE (0<<5) /* release after completion */
826 #define TXCTL_NORELEASE (1<<5) /* on completion returns to host */
827
828 #define BUSY_FID 0x10000
829
830 #ifdef CISCO_EXT
831 #define AIROMAGIC       0xa55a
832 /* Warning : SIOCDEVPRIVATE may disapear during 2.5.X - Jean II */
833 #ifdef SIOCIWFIRSTPRIV
834 #ifdef SIOCDEVPRIVATE
835 #define AIROOLDIOCTL    SIOCDEVPRIVATE
836 #define AIROOLDIDIFC    AIROOLDIOCTL + 1
837 #endif /* SIOCDEVPRIVATE */
838 #else /* SIOCIWFIRSTPRIV */
839 #define SIOCIWFIRSTPRIV SIOCDEVPRIVATE
840 #endif /* SIOCIWFIRSTPRIV */
841 /* This may be wrong. When using the new SIOCIWFIRSTPRIV range, we probably
842  * should use only "GET" ioctls (last bit set to 1). "SET" ioctls are root
843  * only and don't return the modified struct ifreq to the application which
844  * is usually a problem. - Jean II */
845 #define AIROIOCTL       SIOCIWFIRSTPRIV
846 #define AIROIDIFC       AIROIOCTL + 1
847
848 /* Ioctl constants to be used in airo_ioctl.command */
849
850 #define AIROGCAP                0       // Capability rid
851 #define AIROGCFG                1       // USED A LOT
852 #define AIROGSLIST              2       // System ID list
853 #define AIROGVLIST              3       // List of specified AP's
854 #define AIROGDRVNAM             4       //  NOTUSED
855 #define AIROGEHTENC             5       // NOTUSED
856 #define AIROGWEPKTMP            6
857 #define AIROGWEPKNV             7
858 #define AIROGSTAT               8
859 #define AIROGSTATSC32           9
860 #define AIROGSTATSD32           10
861 #define AIROGMICRID             11
862 #define AIROGMICSTATS           12
863 #define AIROGFLAGS              13
864 #define AIROGID                 14
865 #define AIRORRID                15
866 #define AIRORSWVERSION          17
867
868 /* Leave gap of 40 commands after AIROGSTATSD32 for future */
869
870 #define AIROPCAP                AIROGSTATSD32 + 40
871 #define AIROPVLIST              AIROPCAP      + 1
872 #define AIROPSLIST              AIROPVLIST    + 1
873 #define AIROPCFG                AIROPSLIST    + 1
874 #define AIROPSIDS               AIROPCFG      + 1
875 #define AIROPAPLIST             AIROPSIDS     + 1
876 #define AIROPMACON              AIROPAPLIST   + 1       /* Enable mac  */
877 #define AIROPMACOFF             AIROPMACON    + 1       /* Disable mac */
878 #define AIROPSTCLR              AIROPMACOFF   + 1
879 #define AIROPWEPKEY             AIROPSTCLR    + 1
880 #define AIROPWEPKEYNV           AIROPWEPKEY   + 1
881 #define AIROPLEAPPWD            AIROPWEPKEYNV + 1
882 #define AIROPLEAPUSR            AIROPLEAPPWD  + 1
883
884 /* Flash codes */
885
886 #define AIROFLSHRST            AIROPWEPKEYNV  + 40
887 #define AIROFLSHGCHR           AIROFLSHRST    + 1
888 #define AIROFLSHSTFL           AIROFLSHGCHR   + 1
889 #define AIROFLSHPCHR           AIROFLSHSTFL   + 1
890 #define AIROFLPUTBUF           AIROFLSHPCHR   + 1
891 #define AIRORESTART            AIROFLPUTBUF   + 1
892
893 #define FLASHSIZE       32768
894 #define AUXMEMSIZE      (256 * 1024)
895
896 typedef struct aironet_ioctl {
897         unsigned short command;         // What to do
898         unsigned short len;             // Len of data
899         unsigned short ridnum;          // rid number
900         unsigned char __user *data;     // d-data
901 } aironet_ioctl;
902
903 static char swversion[] = "2.1";
904 #endif /* CISCO_EXT */
905
906 #define NUM_MODULES       2
907 #define MIC_MSGLEN_MAX    2400
908 #define EMMH32_MSGLEN_MAX MIC_MSGLEN_MAX
909
910 typedef struct {
911         u32   size;            // size
912         u8    enabled;         // MIC enabled or not
913         u32   rxSuccess;       // successful packets received
914         u32   rxIncorrectMIC;  // pkts dropped due to incorrect MIC comparison
915         u32   rxNotMICed;      // pkts dropped due to not being MIC'd
916         u32   rxMICPlummed;    // pkts dropped due to not having a MIC plummed
917         u32   rxWrongSequence; // pkts dropped due to sequence number violation
918         u32   reserve[32];
919 } mic_statistics;
920
921 typedef struct {
922         u32 coeff[((EMMH32_MSGLEN_MAX)+3)>>2];
923         u64 accum;      // accumulated mic, reduced to u32 in final()
924         int position;   // current position (byte offset) in message
925         union {
926                 u8  d8[4];
927                 u32 d32;
928         } part; // saves partial message word across update() calls
929 } emmh32_context;
930
931 typedef struct {
932         emmh32_context seed;        // Context - the seed
933         u32              rx;        // Received sequence number
934         u32              tx;        // Tx sequence number
935         u32              window;    // Start of window
936         u8               valid;     // Flag to say if context is valid or not
937         u8               key[16];
938 } miccntx;
939
940 typedef struct {
941         miccntx mCtx;           // Multicast context
942         miccntx uCtx;           // Unicast context
943 } mic_module;
944
945 typedef struct {
946         unsigned int  rid: 16;
947         unsigned int  len: 15;
948         unsigned int  valid: 1;
949         dma_addr_t host_addr;
950 } Rid;
951
952 typedef struct {
953         unsigned int  offset: 15;
954         unsigned int  eoc: 1;
955         unsigned int  len: 15;
956         unsigned int  valid: 1;
957         dma_addr_t host_addr;
958 } TxFid;
959
960 typedef struct {
961         unsigned int  ctl: 15;
962         unsigned int  rdy: 1;
963         unsigned int  len: 15;
964         unsigned int  valid: 1;
965         dma_addr_t host_addr;
966 } RxFid;
967
968 /*
969  * Host receive descriptor
970  */
971 typedef struct {
972         unsigned char __iomem *card_ram_off; /* offset into card memory of the
973                                                 desc */
974         RxFid         rx_desc;               /* card receive descriptor */
975         char          *virtual_host_addr;    /* virtual address of host receive
976                                                 buffer */
977         int           pending;
978 } HostRxDesc;
979
980 /*
981  * Host transmit descriptor
982  */
983 typedef struct {
984         unsigned char __iomem *card_ram_off;         /* offset into card memory of the
985                                                 desc */
986         TxFid         tx_desc;               /* card transmit descriptor */
987         char          *virtual_host_addr;    /* virtual address of host receive
988                                                 buffer */
989         int           pending;
990 } HostTxDesc;
991
992 /*
993  * Host RID descriptor
994  */
995 typedef struct {
996         unsigned char __iomem *card_ram_off;      /* offset into card memory of the
997                                              descriptor */
998         Rid           rid_desc;           /* card RID descriptor */
999         char          *virtual_host_addr; /* virtual address of host receive
1000                                              buffer */
1001 } HostRidDesc;
1002
1003 typedef struct {
1004         u16 sw0;
1005         u16 sw1;
1006         u16 status;
1007         u16 len;
1008 #define HOST_SET (1 << 0)
1009 #define HOST_INT_TX (1 << 1) /* Interrupt on successful TX */
1010 #define HOST_INT_TXERR (1 << 2) /* Interrupt on unseccessful TX */
1011 #define HOST_LCC_PAYLOAD (1 << 4) /* LLC payload, 0 = Ethertype */
1012 #define HOST_DONT_RLSE (1 << 5) /* Don't release buffer when done */
1013 #define HOST_DONT_RETRY (1 << 6) /* Don't retry trasmit */
1014 #define HOST_CLR_AID (1 << 7) /* clear AID failure */
1015 #define HOST_RTS (1 << 9) /* Force RTS use */
1016 #define HOST_SHORT (1 << 10) /* Do short preamble */
1017         u16 ctl;
1018         u16 aid;
1019         u16 retries;
1020         u16 fill;
1021 } TxCtlHdr;
1022
1023 typedef struct {
1024         u16 ctl;
1025         u16 duration;
1026         char addr1[6];
1027         char addr2[6];
1028         char addr3[6];
1029         u16 seq;
1030         char addr4[6];
1031 } WifiHdr;
1032
1033
1034 typedef struct {
1035         TxCtlHdr ctlhdr;
1036         u16 fill1;
1037         u16 fill2;
1038         WifiHdr wifihdr;
1039         u16 gaplen;
1040         u16 status;
1041 } WifiCtlHdr;
1042
1043 WifiCtlHdr wifictlhdr8023 = {
1044         .ctlhdr = {
1045                 .ctl    = HOST_DONT_RLSE,
1046         }
1047 };
1048
1049 #ifdef WIRELESS_EXT
1050 // Frequency list (map channels to frequencies)
1051 static const long frequency_list[] = { 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1052                                 2447, 2452, 2457, 2462, 2467, 2472, 2484 };
1053
1054 // A few details needed for WEP (Wireless Equivalent Privacy)
1055 #define MAX_KEY_SIZE 13                 // 128 (?) bits
1056 #define MIN_KEY_SIZE  5                 // 40 bits RC4 - WEP
1057 typedef struct wep_key_t {
1058         u16     len;
1059         u8      key[16];        /* 40-bit and 104-bit keys */
1060 } wep_key_t;
1061
1062 /* Backward compatibility */
1063 #ifndef IW_ENCODE_NOKEY
1064 #define IW_ENCODE_NOKEY         0x0800  /* Key is write only, so not present */
1065 #define IW_ENCODE_MODE  (IW_ENCODE_DISABLED | IW_ENCODE_RESTRICTED | IW_ENCODE_OPEN)
1066 #endif /* IW_ENCODE_NOKEY */
1067
1068 /* List of Wireless Handlers (new API) */
1069 static const struct iw_handler_def      airo_handler_def;
1070 #endif /* WIRELESS_EXT */
1071
1072 static const char version[] = "airo.c 0.6 (Ben Reed & Javier Achirica)";
1073
1074 struct airo_info;
1075
1076 static int get_dec_u16( char *buffer, int *start, int limit );
1077 static void OUT4500( struct airo_info *, u16 register, u16 value );
1078 static unsigned short IN4500( struct airo_info *, u16 register );
1079 static u16 setup_card(struct airo_info*, u8 *mac, int lock);
1080 static int enable_MAC( struct airo_info *ai, Resp *rsp, int lock );
1081 static void disable_MAC(struct airo_info *ai, int lock);
1082 static void enable_interrupts(struct airo_info*);
1083 static void disable_interrupts(struct airo_info*);
1084 static u16 issuecommand(struct airo_info*, Cmd *pCmd, Resp *pRsp);
1085 static int bap_setup(struct airo_info*, u16 rid, u16 offset, int whichbap);
1086 static int aux_bap_read(struct airo_info*, u16 *pu16Dst, int bytelen,
1087                         int whichbap);
1088 static int fast_bap_read(struct airo_info*, u16 *pu16Dst, int bytelen,
1089                          int whichbap);
1090 static int bap_write(struct airo_info*, const u16 *pu16Src, int bytelen,
1091                      int whichbap);
1092 static int PC4500_accessrid(struct airo_info*, u16 rid, u16 accmd);
1093 static int PC4500_readrid(struct airo_info*, u16 rid, void *pBuf, int len, int lock);
1094 static int PC4500_writerid(struct airo_info*, u16 rid, const void
1095                            *pBuf, int len, int lock);
1096 static int do_writerid( struct airo_info*, u16 rid, const void *rid_data,
1097                         int len, int dummy );
1098 static u16 transmit_allocate(struct airo_info*, int lenPayload, int raw);
1099 static int transmit_802_3_packet(struct airo_info*, int len, char *pPacket);
1100 static int transmit_802_11_packet(struct airo_info*, int len, char *pPacket);
1101
1102 static int mpi_send_packet (struct net_device *dev);
1103 static void mpi_unmap_card(struct pci_dev *pci);
1104 static void mpi_receive_802_3(struct airo_info *ai);
1105 static void mpi_receive_802_11(struct airo_info *ai);
1106 static int waitbusy (struct airo_info *ai);
1107
1108 static irqreturn_t airo_interrupt( int irq, void* dev_id, struct pt_regs
1109                             *regs);
1110 static int airo_thread(void *data);
1111 static void timer_func( struct net_device *dev );
1112 static int airo_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
1113 #ifdef WIRELESS_EXT
1114 struct iw_statistics *airo_get_wireless_stats (struct net_device *dev);
1115 static void airo_read_wireless_stats (struct airo_info *local);
1116 #endif /* WIRELESS_EXT */
1117 #ifdef CISCO_EXT
1118 static int readrids(struct net_device *dev, aironet_ioctl *comp);
1119 static int writerids(struct net_device *dev, aironet_ioctl *comp);
1120 int flashcard(struct net_device *dev, aironet_ioctl *comp);
1121 #endif /* CISCO_EXT */
1122 #ifdef MICSUPPORT
1123 static void micinit(struct airo_info *ai);
1124 static int micsetup(struct airo_info *ai);
1125 static int encapsulate(struct airo_info *ai, etherHead *pPacket, MICBuffer *buffer, int len);
1126 static int decapsulate(struct airo_info *ai, MICBuffer *mic, etherHead *pPacket, u16 payLen);
1127
1128 static u8 airo_rssi_to_dbm (tdsRssiEntry *rssi_rid, u8 rssi);
1129 static u8 airo_dbm_to_pct (tdsRssiEntry *rssi_rid, u8 dbm);
1130
1131 #include <linux/crypto.h>
1132 #endif
1133
1134 struct airo_info {
1135         struct net_device_stats stats;
1136         struct net_device             *dev;
1137         /* Note, we can have MAX_FIDS outstanding.  FIDs are 16-bits, so we
1138            use the high bit to mark whether it is in use. */
1139 #define MAX_FIDS 6
1140 #define MPI_MAX_FIDS 1
1141         int                           fids[MAX_FIDS];
1142         ConfigRid config;
1143         char keyindex; // Used with auto wep
1144         char defindex; // Used with auto wep
1145         struct proc_dir_entry *proc_entry;
1146         spinlock_t aux_lock;
1147         unsigned long flags;
1148 #define FLAG_PROMISC    8       /* IFF_PROMISC 0x100 - include/linux/if.h */
1149 #define FLAG_RADIO_OFF  0       /* User disabling of MAC */
1150 #define FLAG_RADIO_DOWN 1       /* ifup/ifdown disabling of MAC */
1151 #define FLAG_RADIO_MASK 0x03
1152 #define FLAG_ENABLED    2
1153 #define FLAG_ADHOC      3       /* Needed by MIC */
1154 #define FLAG_MIC_CAPABLE 4
1155 #define FLAG_UPDATE_MULTI 5
1156 #define FLAG_UPDATE_UNI 6
1157 #define FLAG_802_11     7
1158 #define FLAG_PENDING_XMIT 9
1159 #define FLAG_PENDING_XMIT11 10
1160 #define FLAG_MPI        11
1161 #define FLAG_REGISTERED 12
1162 #define FLAG_COMMIT     13
1163 #define FLAG_RESET      14
1164 #define FLAG_FLASHING   15
1165 #define JOB_MASK        0x1ff0000
1166 #define JOB_DIE         16
1167 #define JOB_XMIT        17
1168 #define JOB_XMIT11      18
1169 #define JOB_STATS       19
1170 #define JOB_PROMISC     20
1171 #define JOB_MIC         21
1172 #define JOB_EVENT       22
1173 #define JOB_AUTOWEP     23
1174 #define JOB_WSTATS      24
1175         int (*bap_read)(struct airo_info*, u16 *pu16Dst, int bytelen,
1176                         int whichbap);
1177         unsigned short *flash;
1178         tdsRssiEntry *rssi;
1179         struct task_struct *task;
1180         struct semaphore sem;
1181         pid_t thr_pid;
1182         wait_queue_head_t thr_wait;
1183         struct completion thr_exited;
1184         unsigned long expires;
1185         struct {
1186                 struct sk_buff *skb;
1187                 int fid;
1188         } xmit, xmit11;
1189         struct net_device *wifidev;
1190 #ifdef WIRELESS_EXT
1191         struct iw_statistics    wstats;         // wireless stats
1192         unsigned long           scan_timestamp; /* Time started to scan */
1193         struct iw_spy_data      spy_data;
1194         struct iw_public_data   wireless_data;
1195 #endif /* WIRELESS_EXT */
1196 #ifdef MICSUPPORT
1197         /* MIC stuff */
1198         struct crypto_tfm       *tfm;
1199         mic_module              mod[2];
1200         mic_statistics          micstats;
1201 #endif
1202         HostRxDesc rxfids[MPI_MAX_FIDS]; // rx/tx/config MPI350 descriptors
1203         HostTxDesc txfids[MPI_MAX_FIDS];
1204         HostRidDesc config_desc;
1205         unsigned long ridbus; // phys addr of config_desc
1206         struct sk_buff_head txq;// tx queue used by mpi350 code
1207         struct pci_dev          *pci;
1208         unsigned char           __iomem *pcimem;
1209         unsigned char           __iomem *pciaux;
1210         unsigned char           *shared;
1211         dma_addr_t              shared_dma;
1212         pm_message_t            power;
1213         SsidRid                 *SSID;
1214         APListRid               *APList;
1215 #define PCI_SHARED_LEN          2*MPI_MAX_FIDS*PKTSIZE+RIDSIZE
1216         char                    proc_name[IFNAMSIZ];
1217 };
1218
1219 static inline int bap_read(struct airo_info *ai, u16 *pu16Dst, int bytelen,
1220                            int whichbap) {
1221         return ai->bap_read(ai, pu16Dst, bytelen, whichbap);
1222 }
1223
1224 static int setup_proc_entry( struct net_device *dev,
1225                              struct airo_info *apriv );
1226 static int takedown_proc_entry( struct net_device *dev,
1227                                 struct airo_info *apriv );
1228
1229 #ifdef MICSUPPORT
1230 /***********************************************************************
1231  *                              MIC ROUTINES                           *
1232  ***********************************************************************
1233  */
1234
1235 static int RxSeqValid (struct airo_info *ai,miccntx *context,int mcast,u32 micSeq);
1236 static void MoveWindow(miccntx *context, u32 micSeq);
1237 void emmh32_setseed(emmh32_context *context, u8 *pkey, int keylen, struct crypto_tfm *);
1238 void emmh32_init(emmh32_context *context);
1239 void emmh32_update(emmh32_context *context, u8 *pOctets, int len);
1240 void emmh32_final(emmh32_context *context, u8 digest[4]);
1241
1242 /* micinit - Initialize mic seed */
1243
1244 static void micinit(struct airo_info *ai)
1245 {
1246         MICRid mic_rid;
1247
1248         clear_bit(JOB_MIC, &ai->flags);
1249         PC4500_readrid(ai, RID_MIC, &mic_rid, sizeof(mic_rid), 0);
1250         up(&ai->sem);
1251
1252         ai->micstats.enabled = (mic_rid.state & 0x00FF) ? 1 : 0;
1253
1254         if (ai->micstats.enabled) {
1255                 /* Key must be valid and different */
1256                 if (mic_rid.multicastValid && (!ai->mod[0].mCtx.valid ||
1257                     (memcmp (ai->mod[0].mCtx.key, mic_rid.multicast,
1258                              sizeof(ai->mod[0].mCtx.key)) != 0))) {
1259                         /* Age current mic Context */
1260                         memcpy(&ai->mod[1].mCtx,&ai->mod[0].mCtx,sizeof(miccntx));
1261                         /* Initialize new context */
1262                         memcpy(&ai->mod[0].mCtx.key,mic_rid.multicast,sizeof(mic_rid.multicast));
1263                         ai->mod[0].mCtx.window  = 33; //Window always points to the middle
1264                         ai->mod[0].mCtx.rx      = 0;  //Rx Sequence numbers
1265                         ai->mod[0].mCtx.tx      = 0;  //Tx sequence numbers
1266                         ai->mod[0].mCtx.valid   = 1;  //Key is now valid
1267   
1268                         /* Give key to mic seed */
1269                         emmh32_setseed(&ai->mod[0].mCtx.seed,mic_rid.multicast,sizeof(mic_rid.multicast), ai->tfm);
1270                 }
1271
1272                 /* Key must be valid and different */
1273                 if (mic_rid.unicastValid && (!ai->mod[0].uCtx.valid || 
1274                     (memcmp(ai->mod[0].uCtx.key, mic_rid.unicast,
1275                             sizeof(ai->mod[0].uCtx.key)) != 0))) {
1276                         /* Age current mic Context */
1277                         memcpy(&ai->mod[1].uCtx,&ai->mod[0].uCtx,sizeof(miccntx));
1278                         /* Initialize new context */
1279                         memcpy(&ai->mod[0].uCtx.key,mic_rid.unicast,sizeof(mic_rid.unicast));
1280         
1281                         ai->mod[0].uCtx.window  = 33; //Window always points to the middle
1282                         ai->mod[0].uCtx.rx      = 0;  //Rx Sequence numbers
1283                         ai->mod[0].uCtx.tx      = 0;  //Tx sequence numbers
1284                         ai->mod[0].uCtx.valid   = 1;  //Key is now valid
1285         
1286                         //Give key to mic seed
1287                         emmh32_setseed(&ai->mod[0].uCtx.seed, mic_rid.unicast, sizeof(mic_rid.unicast), ai->tfm);
1288                 }
1289         } else {
1290       /* So next time we have a valid key and mic is enabled, we will update
1291        * the sequence number if the key is the same as before.
1292        */
1293                 ai->mod[0].uCtx.valid = 0;
1294                 ai->mod[0].mCtx.valid = 0;
1295         }
1296 }
1297
1298 /* micsetup - Get ready for business */
1299
1300 static int micsetup(struct airo_info *ai) {
1301         int i;
1302
1303         if (ai->tfm == NULL)
1304                 ai->tfm = crypto_alloc_tfm("aes", CRYPTO_TFM_REQ_MAY_SLEEP);
1305
1306         if (ai->tfm == NULL) {
1307                 printk(KERN_ERR "airo: failed to load transform for AES\n");
1308                 return ERROR;
1309         }
1310
1311         for (i=0; i < NUM_MODULES; i++) {
1312                 memset(&ai->mod[i].mCtx,0,sizeof(miccntx));
1313                 memset(&ai->mod[i].uCtx,0,sizeof(miccntx));
1314         }
1315         return SUCCESS;
1316 }
1317
1318 char micsnap[]= {0xAA,0xAA,0x03,0x00,0x40,0x96,0x00,0x02};
1319
1320 /*===========================================================================
1321  * Description: Mic a packet
1322  *    
1323  *      Inputs: etherHead * pointer to an 802.3 frame
1324  *    
1325  *     Returns: BOOLEAN if successful, otherwise false.
1326  *             PacketTxLen will be updated with the mic'd packets size.
1327  *
1328  *    Caveats: It is assumed that the frame buffer will already
1329  *             be big enough to hold the largets mic message possible.
1330  *            (No memory allocation is done here).
1331  *  
1332  *    Author: sbraneky (10/15/01)
1333  *    Merciless hacks by rwilcher (1/14/02)
1334  */
1335
1336 static int encapsulate(struct airo_info *ai ,etherHead *frame, MICBuffer *mic, int payLen)
1337 {
1338         miccntx   *context;
1339
1340         // Determine correct context
1341         // If not adhoc, always use unicast key
1342
1343         if (test_bit(FLAG_ADHOC, &ai->flags) && (frame->da[0] & 0x1))
1344                 context = &ai->mod[0].mCtx;
1345         else
1346                 context = &ai->mod[0].uCtx;
1347   
1348         if (!context->valid)
1349                 return ERROR;
1350
1351         mic->typelen = htons(payLen + 16); //Length of Mic'd packet
1352
1353         memcpy(&mic->u.snap, micsnap, sizeof(micsnap)); // Add Snap
1354
1355         // Add Tx sequence
1356         mic->seq = htonl(context->tx);
1357         context->tx += 2;
1358
1359         emmh32_init(&context->seed); // Mic the packet
1360         emmh32_update(&context->seed,frame->da,ETH_ALEN * 2); // DA,SA
1361         emmh32_update(&context->seed,(u8*)&mic->typelen,10); // Type/Length and Snap
1362         emmh32_update(&context->seed,(u8*)&mic->seq,sizeof(mic->seq)); //SEQ
1363         emmh32_update(&context->seed,frame->da + ETH_ALEN * 2,payLen); //payload
1364         emmh32_final(&context->seed, (u8*)&mic->mic);
1365
1366         /*    New Type/length ?????????? */
1367         mic->typelen = 0; //Let NIC know it could be an oversized packet
1368         return SUCCESS;
1369 }
1370
1371 typedef enum {
1372     NONE,
1373     NOMIC,
1374     NOMICPLUMMED,
1375     SEQUENCE,
1376     INCORRECTMIC,
1377 } mic_error;
1378
1379 /*===========================================================================
1380  *  Description: Decapsulates a MIC'd packet and returns the 802.3 packet
1381  *               (removes the MIC stuff) if packet is a valid packet.
1382  *      
1383  *       Inputs: etherHead  pointer to the 802.3 packet             
1384  *     
1385  *      Returns: BOOLEAN - TRUE if packet should be dropped otherwise FALSE
1386  *     
1387  *      Author: sbraneky (10/15/01)
1388  *    Merciless hacks by rwilcher (1/14/02)
1389  *---------------------------------------------------------------------------
1390  */
1391
1392 static int decapsulate(struct airo_info *ai, MICBuffer *mic, etherHead *eth, u16 payLen)
1393 {
1394         int      i;
1395         u32      micSEQ;
1396         miccntx  *context;
1397         u8       digest[4];
1398         mic_error micError = NONE;
1399
1400         // Check if the packet is a Mic'd packet
1401
1402         if (!ai->micstats.enabled) {
1403                 //No Mic set or Mic OFF but we received a MIC'd packet.
1404                 if (memcmp ((u8*)eth + 14, micsnap, sizeof(micsnap)) == 0) {
1405                         ai->micstats.rxMICPlummed++;
1406                         return ERROR;
1407                 }
1408                 return SUCCESS;
1409         }
1410
1411         if (ntohs(mic->typelen) == 0x888E)
1412                 return SUCCESS;
1413
1414         if (memcmp (mic->u.snap, micsnap, sizeof(micsnap)) != 0) {
1415             // Mic enabled but packet isn't Mic'd
1416                 ai->micstats.rxMICPlummed++;
1417                 return ERROR;
1418         }
1419
1420         micSEQ = ntohl(mic->seq);            //store SEQ as CPU order
1421
1422         //At this point we a have a mic'd packet and mic is enabled
1423         //Now do the mic error checking.
1424
1425         //Receive seq must be odd
1426         if ( (micSEQ & 1) == 0 ) {
1427                 ai->micstats.rxWrongSequence++;
1428                 return ERROR;
1429         }
1430
1431         for (i = 0; i < NUM_MODULES; i++) {
1432                 int mcast = eth->da[0] & 1;
1433                 //Determine proper context 
1434                 context = mcast ? &ai->mod[i].mCtx : &ai->mod[i].uCtx;
1435         
1436                 //Make sure context is valid
1437                 if (!context->valid) {
1438                         if (i == 0)
1439                                 micError = NOMICPLUMMED;
1440                         continue;                
1441                 }
1442                 //DeMic it 
1443
1444                 if (!mic->typelen)
1445                         mic->typelen = htons(payLen + sizeof(MICBuffer) - 2);
1446         
1447                 emmh32_init(&context->seed);
1448                 emmh32_update(&context->seed, eth->da, ETH_ALEN*2); 
1449                 emmh32_update(&context->seed, (u8 *)&mic->typelen, sizeof(mic->typelen)+sizeof(mic->u.snap)); 
1450                 emmh32_update(&context->seed, (u8 *)&mic->seq,sizeof(mic->seq));        
1451                 emmh32_update(&context->seed, eth->da + ETH_ALEN*2,payLen);     
1452                 //Calculate MIC
1453                 emmh32_final(&context->seed, digest);
1454         
1455                 if (memcmp(digest, &mic->mic, 4)) { //Make sure the mics match
1456                   //Invalid Mic
1457                         if (i == 0)
1458                                 micError = INCORRECTMIC;
1459                         continue;
1460                 }
1461
1462                 //Check Sequence number if mics pass
1463                 if (RxSeqValid(ai, context, mcast, micSEQ) == SUCCESS) {
1464                         ai->micstats.rxSuccess++;
1465                         return SUCCESS;
1466                 }
1467                 if (i == 0)
1468                         micError = SEQUENCE;
1469         }
1470
1471         // Update statistics
1472         switch (micError) {
1473                 case NOMICPLUMMED: ai->micstats.rxMICPlummed++;   break;
1474                 case SEQUENCE:    ai->micstats.rxWrongSequence++; break;
1475                 case INCORRECTMIC: ai->micstats.rxIncorrectMIC++; break;
1476                 case NONE:  break;
1477                 case NOMIC: break;
1478         }
1479         return ERROR;
1480 }
1481
1482 /*===========================================================================
1483  * Description:  Checks the Rx Seq number to make sure it is valid
1484  *               and hasn't already been received
1485  *   
1486  *     Inputs: miccntx - mic context to check seq against
1487  *             micSeq  - the Mic seq number
1488  *   
1489  *    Returns: TRUE if valid otherwise FALSE. 
1490  *
1491  *    Author: sbraneky (10/15/01)
1492  *    Merciless hacks by rwilcher (1/14/02)
1493  *---------------------------------------------------------------------------
1494  */
1495
1496 static int RxSeqValid (struct airo_info *ai,miccntx *context,int mcast,u32 micSeq)
1497 {
1498         u32 seq,index;
1499
1500         //Allow for the ap being rebooted - if it is then use the next 
1501         //sequence number of the current sequence number - might go backwards
1502
1503         if (mcast) {
1504                 if (test_bit(FLAG_UPDATE_MULTI, &ai->flags)) {
1505                         clear_bit (FLAG_UPDATE_MULTI, &ai->flags);
1506                         context->window = (micSeq > 33) ? micSeq : 33;
1507                         context->rx     = 0;        // Reset rx
1508                 }
1509         } else if (test_bit(FLAG_UPDATE_UNI, &ai->flags)) {
1510                 clear_bit (FLAG_UPDATE_UNI, &ai->flags);
1511                 context->window = (micSeq > 33) ? micSeq : 33; // Move window
1512                 context->rx     = 0;        // Reset rx
1513         }
1514
1515         //Make sequence number relative to START of window
1516         seq = micSeq - (context->window - 33);
1517
1518         //Too old of a SEQ number to check.
1519         if ((s32)seq < 0)
1520                 return ERROR;
1521     
1522         if ( seq > 64 ) {
1523                 //Window is infinite forward
1524                 MoveWindow(context,micSeq);
1525                 return SUCCESS;
1526         }
1527
1528         // We are in the window. Now check the context rx bit to see if it was already sent
1529         seq >>= 1;         //divide by 2 because we only have odd numbers
1530         index = 1 << seq;  //Get an index number
1531
1532         if (!(context->rx & index)) {
1533                 //micSEQ falls inside the window.
1534                 //Add seqence number to the list of received numbers.
1535                 context->rx |= index;
1536
1537                 MoveWindow(context,micSeq);
1538
1539                 return SUCCESS;
1540         }
1541         return ERROR;
1542 }
1543
1544 static void MoveWindow(miccntx *context, u32 micSeq)
1545 {
1546         u32 shift;
1547
1548         //Move window if seq greater than the middle of the window
1549         if (micSeq > context->window) {
1550                 shift = (micSeq - context->window) >> 1;
1551     
1552                     //Shift out old
1553                 if (shift < 32)
1554                         context->rx >>= shift;
1555                 else
1556                         context->rx = 0;
1557
1558                 context->window = micSeq;      //Move window
1559         }
1560 }
1561
1562 /*==============================================*/
1563 /*========== EMMH ROUTINES  ====================*/
1564 /*==============================================*/
1565
1566 /* mic accumulate */
1567 #define MIC_ACCUM(val)  \
1568         context->accum += (u64)(val) * context->coeff[coeff_position++];
1569
1570 static unsigned char aes_counter[16];
1571
1572 /* expand the key to fill the MMH coefficient array */
1573 void emmh32_setseed(emmh32_context *context, u8 *pkey, int keylen, struct crypto_tfm *tfm)
1574 {
1575   /* take the keying material, expand if necessary, truncate at 16-bytes */
1576   /* run through AES counter mode to generate context->coeff[] */
1577   
1578         int i,j;
1579         u32 counter;
1580         u8 *cipher, plain[16];
1581         struct scatterlist sg[1];
1582
1583         crypto_cipher_setkey(tfm, pkey, 16);
1584         counter = 0;
1585         for (i = 0; i < (sizeof(context->coeff)/sizeof(context->coeff[0])); ) {
1586                 aes_counter[15] = (u8)(counter >> 0);
1587                 aes_counter[14] = (u8)(counter >> 8);
1588                 aes_counter[13] = (u8)(counter >> 16);
1589                 aes_counter[12] = (u8)(counter >> 24);
1590                 counter++;
1591                 memcpy (plain, aes_counter, 16);
1592                 sg[0].page = virt_to_page(plain);
1593                 sg[0].offset = ((long) plain & ~PAGE_MASK);
1594                 sg[0].length = 16;
1595                 crypto_cipher_encrypt(tfm, sg, sg, 16);
1596                 cipher = kmap(sg[0].page) + sg[0].offset;
1597                 for (j=0; (j<16) && (i< (sizeof(context->coeff)/sizeof(context->coeff[0]))); ) {
1598                         context->coeff[i++] = ntohl(*(u32 *)&cipher[j]);
1599                         j += 4;
1600                 }
1601         }
1602 }
1603
1604 /* prepare for calculation of a new mic */
1605 void emmh32_init(emmh32_context *context)
1606 {
1607         /* prepare for new mic calculation */
1608         context->accum = 0;
1609         context->position = 0;
1610 }
1611
1612 /* add some bytes to the mic calculation */
1613 void emmh32_update(emmh32_context *context, u8 *pOctets, int len)
1614 {
1615         int     coeff_position, byte_position;
1616   
1617         if (len == 0) return;
1618   
1619         coeff_position = context->position >> 2;
1620   
1621         /* deal with partial 32-bit word left over from last update */
1622         byte_position = context->position & 3;
1623         if (byte_position) {
1624                 /* have a partial word in part to deal with */
1625                 do {
1626                         if (len == 0) return;
1627                         context->part.d8[byte_position++] = *pOctets++;
1628                         context->position++;
1629                         len--;
1630                 } while (byte_position < 4);
1631                 MIC_ACCUM(htonl(context->part.d32));
1632         }
1633
1634         /* deal with full 32-bit words */
1635         while (len >= 4) {
1636                 MIC_ACCUM(htonl(*(u32 *)pOctets));
1637                 context->position += 4;
1638                 pOctets += 4;
1639                 len -= 4;
1640         }
1641
1642         /* deal with partial 32-bit word that will be left over from this update */
1643         byte_position = 0;
1644         while (len > 0) {
1645                 context->part.d8[byte_position++] = *pOctets++;
1646                 context->position++;
1647                 len--;
1648         }
1649 }
1650
1651 /* mask used to zero empty bytes for final partial word */
1652 static u32 mask32[4] = { 0x00000000L, 0xFF000000L, 0xFFFF0000L, 0xFFFFFF00L };
1653
1654 /* calculate the mic */
1655 void emmh32_final(emmh32_context *context, u8 digest[4])
1656 {
1657         int     coeff_position, byte_position;
1658         u32     val;
1659   
1660         u64 sum, utmp;
1661         s64 stmp;
1662
1663         coeff_position = context->position >> 2;
1664   
1665         /* deal with partial 32-bit word left over from last update */
1666         byte_position = context->position & 3;
1667         if (byte_position) {
1668                 /* have a partial word in part to deal with */
1669                 val = htonl(context->part.d32);
1670                 MIC_ACCUM(val & mask32[byte_position]); /* zero empty bytes */
1671         }
1672
1673         /* reduce the accumulated u64 to a 32-bit MIC */
1674         sum = context->accum;
1675         stmp = (sum  & 0xffffffffLL) - ((sum >> 32)  * 15);
1676         utmp = (stmp & 0xffffffffLL) - ((stmp >> 32) * 15);
1677         sum = utmp & 0xffffffffLL;
1678         if (utmp > 0x10000000fLL)
1679                 sum -= 15;
1680
1681         val = (u32)sum;
1682         digest[0] = (val>>24) & 0xFF;
1683         digest[1] = (val>>16) & 0xFF;
1684         digest[2] = (val>>8) & 0xFF;
1685         digest[3] = val & 0xFF;
1686 }
1687 #endif
1688
1689 static int readBSSListRid(struct airo_info *ai, int first,
1690                       BSSListRid *list) {
1691         int rc;
1692                         Cmd cmd;
1693                         Resp rsp;
1694
1695         if (first == 1) {
1696                         if (ai->flags & FLAG_RADIO_MASK) return -ENETDOWN;
1697                         memset(&cmd, 0, sizeof(cmd));
1698                         cmd.cmd=CMD_LISTBSS;
1699                         if (down_interruptible(&ai->sem))
1700                                 return -ERESTARTSYS;
1701                         issuecommand(ai, &cmd, &rsp);
1702                         up(&ai->sem);
1703                         /* Let the command take effect */
1704                         ai->task = current;
1705                         ssleep(3);
1706                         ai->task = NULL;
1707                 }
1708         rc = PC4500_readrid(ai, first ? RID_BSSLISTFIRST : RID_BSSLISTNEXT,
1709                             list, sizeof(*list), 1);
1710
1711         list->len = le16_to_cpu(list->len);
1712         list->index = le16_to_cpu(list->index);
1713         list->radioType = le16_to_cpu(list->radioType);
1714         list->cap = le16_to_cpu(list->cap);
1715         list->beaconInterval = le16_to_cpu(list->beaconInterval);
1716         list->fh.dwell = le16_to_cpu(list->fh.dwell);
1717         list->dsChannel = le16_to_cpu(list->dsChannel);
1718         list->atimWindow = le16_to_cpu(list->atimWindow);
1719         list->dBm = le16_to_cpu(list->dBm);
1720         return rc;
1721 }
1722
1723 static int readWepKeyRid(struct airo_info*ai, WepKeyRid *wkr, int temp, int lock) {
1724         int rc = PC4500_readrid(ai, temp ? RID_WEP_TEMP : RID_WEP_PERM,
1725                                 wkr, sizeof(*wkr), lock);
1726
1727         wkr->len = le16_to_cpu(wkr->len);
1728         wkr->kindex = le16_to_cpu(wkr->kindex);
1729         wkr->klen = le16_to_cpu(wkr->klen);
1730         return rc;
1731 }
1732 /* In the writeXXXRid routines we copy the rids so that we don't screwup
1733  * the originals when we endian them... */
1734 static int writeWepKeyRid(struct airo_info*ai, WepKeyRid *pwkr, int perm, int lock) {
1735         int rc;
1736         WepKeyRid wkr = *pwkr;
1737
1738         wkr.len = cpu_to_le16(wkr.len);
1739         wkr.kindex = cpu_to_le16(wkr.kindex);
1740         wkr.klen = cpu_to_le16(wkr.klen);
1741         rc = PC4500_writerid(ai, RID_WEP_TEMP, &wkr, sizeof(wkr), lock);
1742         if (rc!=SUCCESS) printk(KERN_ERR "airo:  WEP_TEMP set %x\n", rc);
1743         if (perm) {
1744                 rc = PC4500_writerid(ai, RID_WEP_PERM, &wkr, sizeof(wkr), lock);
1745                 if (rc!=SUCCESS) {
1746                         printk(KERN_ERR "airo:  WEP_PERM set %x\n", rc);
1747                 }
1748         }
1749         return rc;
1750 }
1751
1752 static int readSsidRid(struct airo_info*ai, SsidRid *ssidr) {
1753         int i;
1754         int rc = PC4500_readrid(ai, RID_SSID, ssidr, sizeof(*ssidr), 1);
1755
1756         ssidr->len = le16_to_cpu(ssidr->len);
1757         for(i = 0; i < 3; i++) {
1758                 ssidr->ssids[i].len = le16_to_cpu(ssidr->ssids[i].len);
1759         }
1760         return rc;
1761 }
1762 static int writeSsidRid(struct airo_info*ai, SsidRid *pssidr, int lock) {
1763         int rc;
1764         int i;
1765         SsidRid ssidr = *pssidr;
1766
1767         ssidr.len = cpu_to_le16(ssidr.len);
1768         for(i = 0; i < 3; i++) {
1769                 ssidr.ssids[i].len = cpu_to_le16(ssidr.ssids[i].len);
1770         }
1771         rc = PC4500_writerid(ai, RID_SSID, &ssidr, sizeof(ssidr), lock);
1772         return rc;
1773 }
1774 static int readConfigRid(struct airo_info*ai, int lock) {
1775         int rc;
1776         u16 *s;
1777         ConfigRid cfg;
1778
1779         if (ai->config.len)
1780                 return SUCCESS;
1781
1782         rc = PC4500_readrid(ai, RID_ACTUALCONFIG, &cfg, sizeof(cfg), lock);
1783         if (rc != SUCCESS)
1784                 return rc;
1785
1786         for(s = &cfg.len; s <= &cfg.rtsThres; s++) *s = le16_to_cpu(*s);
1787
1788         for(s = &cfg.shortRetryLimit; s <= &cfg.radioType; s++)
1789                 *s = le16_to_cpu(*s);
1790
1791         for(s = &cfg.txPower; s <= &cfg.radioSpecific; s++)
1792                 *s = le16_to_cpu(*s);
1793
1794         for(s = &cfg.arlThreshold; s <= &cfg._reserved4[0]; s++)
1795                 *s = cpu_to_le16(*s);
1796
1797         for(s = &cfg.autoWake; s <= &cfg.autoWake; s++)
1798                 *s = cpu_to_le16(*s);
1799
1800         ai->config = cfg;
1801         return SUCCESS;
1802 }
1803 static inline void checkThrottle(struct airo_info *ai) {
1804         int i;
1805 /* Old hardware had a limit on encryption speed */
1806         if (ai->config.authType != AUTH_OPEN && maxencrypt) {
1807                 for(i=0; i<8; i++) {
1808                         if (ai->config.rates[i] > maxencrypt) {
1809                                 ai->config.rates[i] = 0;
1810                         }
1811                 }
1812         }
1813 }
1814 static int writeConfigRid(struct airo_info*ai, int lock) {
1815         u16 *s;
1816         ConfigRid cfgr;
1817
1818         if (!test_bit (FLAG_COMMIT, &ai->flags))
1819                 return SUCCESS;
1820
1821         clear_bit (FLAG_COMMIT, &ai->flags);
1822         clear_bit (FLAG_RESET, &ai->flags);
1823         checkThrottle(ai);
1824         cfgr = ai->config;
1825
1826         if ((cfgr.opmode & 0xFF) == MODE_STA_IBSS)
1827                 set_bit(FLAG_ADHOC, &ai->flags);
1828         else
1829                 clear_bit(FLAG_ADHOC, &ai->flags);
1830
1831         for(s = &cfgr.len; s <= &cfgr.rtsThres; s++) *s = cpu_to_le16(*s);
1832
1833         for(s = &cfgr.shortRetryLimit; s <= &cfgr.radioType; s++)
1834                 *s = cpu_to_le16(*s);
1835
1836         for(s = &cfgr.txPower; s <= &cfgr.radioSpecific; s++)
1837                 *s = cpu_to_le16(*s);
1838
1839         for(s = &cfgr.arlThreshold; s <= &cfgr._reserved4[0]; s++)
1840                 *s = cpu_to_le16(*s);
1841
1842         for(s = &cfgr.autoWake; s <= &cfgr.autoWake; s++)
1843                 *s = cpu_to_le16(*s);
1844
1845         return PC4500_writerid( ai, RID_CONFIG, &cfgr, sizeof(cfgr), lock);
1846 }
1847 static int readStatusRid(struct airo_info*ai, StatusRid *statr, int lock) {
1848         int rc = PC4500_readrid(ai, RID_STATUS, statr, sizeof(*statr), lock);
1849         u16 *s;
1850
1851         statr->len = le16_to_cpu(statr->len);
1852         for(s = &statr->mode; s <= &statr->SSIDlen; s++) *s = le16_to_cpu(*s);
1853
1854         for(s = &statr->beaconPeriod; s <= &statr->shortPreamble; s++)
1855                 *s = le16_to_cpu(*s);
1856         statr->load = le16_to_cpu(statr->load);
1857         statr->assocStatus = le16_to_cpu(statr->assocStatus);
1858         return rc;
1859 }
1860 static int readAPListRid(struct airo_info*ai, APListRid *aplr) {
1861         int rc =  PC4500_readrid(ai, RID_APLIST, aplr, sizeof(*aplr), 1);
1862         aplr->len = le16_to_cpu(aplr->len);
1863         return rc;
1864 }
1865 static int writeAPListRid(struct airo_info*ai, APListRid *aplr, int lock) {
1866         int rc;
1867         aplr->len = cpu_to_le16(aplr->len);
1868         rc = PC4500_writerid(ai, RID_APLIST, aplr, sizeof(*aplr), lock);
1869         return rc;
1870 }
1871 static int readCapabilityRid(struct airo_info*ai, CapabilityRid *capr, int lock) {
1872         int rc = PC4500_readrid(ai, RID_CAPABILITIES, capr, sizeof(*capr), lock);
1873         u16 *s;
1874
1875         capr->len = le16_to_cpu(capr->len);
1876         capr->prodNum = le16_to_cpu(capr->prodNum);
1877         capr->radioType = le16_to_cpu(capr->radioType);
1878         capr->country = le16_to_cpu(capr->country);
1879         for(s = &capr->txPowerLevels[0]; s <= &capr->requiredHard; s++)
1880                 *s = le16_to_cpu(*s);
1881         return rc;
1882 }
1883 static int readStatsRid(struct airo_info*ai, StatsRid *sr, int rid, int lock) {
1884         int rc = PC4500_readrid(ai, rid, sr, sizeof(*sr), lock);
1885         u32 *i;
1886
1887         sr->len = le16_to_cpu(sr->len);
1888         for(i = &sr->vals[0]; i <= &sr->vals[99]; i++) *i = le32_to_cpu(*i);
1889         return rc;
1890 }
1891
1892 static int airo_open(struct net_device *dev) {
1893         struct airo_info *info = dev->priv;
1894         Resp rsp;
1895
1896         if (test_bit(FLAG_FLASHING, &info->flags))
1897                 return -EIO;
1898
1899         /* Make sure the card is configured.
1900          * Wireless Extensions may postpone config changes until the card
1901          * is open (to pipeline changes and speed-up card setup). If
1902          * those changes are not yet commited, do it now - Jean II */
1903         if (test_bit (FLAG_COMMIT, &info->flags)) {
1904                 disable_MAC(info, 1);
1905                 writeConfigRid(info, 1);
1906         }
1907
1908         if (info->wifidev != dev) {
1909                 /* Power on the MAC controller (which may have been disabled) */
1910                 clear_bit(FLAG_RADIO_DOWN, &info->flags);
1911                 enable_interrupts(info);
1912         }
1913         enable_MAC(info, &rsp, 1);
1914
1915         netif_start_queue(dev);
1916         return 0;
1917 }
1918
1919 static int mpi_start_xmit(struct sk_buff *skb, struct net_device *dev) {
1920         int npacks, pending;
1921         unsigned long flags;
1922         struct airo_info *ai = dev->priv;
1923
1924         if (!skb) {
1925                 printk(KERN_ERR "airo: %s: skb==NULL\n",__FUNCTION__);
1926                 return 0;
1927         }
1928         npacks = skb_queue_len (&ai->txq);
1929
1930         if (npacks >= MAXTXQ - 1) {
1931                 netif_stop_queue (dev);
1932                 if (npacks > MAXTXQ) {
1933                         ai->stats.tx_fifo_errors++;
1934                         return 1;
1935                 }
1936                 skb_queue_tail (&ai->txq, skb);
1937                 return 0;
1938         }
1939
1940         spin_lock_irqsave(&ai->aux_lock, flags);
1941         skb_queue_tail (&ai->txq, skb);
1942         pending = test_bit(FLAG_PENDING_XMIT, &ai->flags);
1943         spin_unlock_irqrestore(&ai->aux_lock,flags);
1944         netif_wake_queue (dev);
1945
1946         if (pending == 0) {
1947                 set_bit(FLAG_PENDING_XMIT, &ai->flags);
1948                 mpi_send_packet (dev);
1949         }
1950         return 0;
1951 }
1952
1953 /*
1954  * @mpi_send_packet
1955  *
1956  * Attempt to transmit a packet. Can be called from interrupt
1957  * or transmit . return number of packets we tried to send
1958  */
1959
1960 static int mpi_send_packet (struct net_device *dev)
1961 {
1962         struct sk_buff *skb;
1963         unsigned char *buffer;
1964         s16 len, *payloadLen;
1965         struct airo_info *ai = dev->priv;
1966         u8 *sendbuf;
1967
1968         /* get a packet to send */
1969
1970         if ((skb = skb_dequeue(&ai->txq)) == 0) {
1971                 printk (KERN_ERR
1972                         "airo: %s: Dequeue'd zero in send_packet()\n",
1973                         __FUNCTION__);
1974                 return 0;
1975         }
1976
1977         /* check min length*/
1978         len = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
1979         buffer = skb->data;
1980
1981         ai->txfids[0].tx_desc.offset = 0;
1982         ai->txfids[0].tx_desc.valid = 1;
1983         ai->txfids[0].tx_desc.eoc = 1;
1984         ai->txfids[0].tx_desc.len =len+sizeof(WifiHdr);
1985
1986 /*
1987  * Magic, the cards firmware needs a length count (2 bytes) in the host buffer
1988  * right after  TXFID_HDR.The TXFID_HDR contains the status short so payloadlen
1989  * is immediatly after it. ------------------------------------------------
1990  *                         |TXFIDHDR+STATUS|PAYLOADLEN|802.3HDR|PACKETDATA|
1991  *                         ------------------------------------------------
1992  */
1993
1994         memcpy((char *)ai->txfids[0].virtual_host_addr,
1995                 (char *)&wifictlhdr8023, sizeof(wifictlhdr8023));
1996
1997         payloadLen = (s16 *)(ai->txfids[0].virtual_host_addr +
1998                 sizeof(wifictlhdr8023));
1999         sendbuf = ai->txfids[0].virtual_host_addr +
2000                 sizeof(wifictlhdr8023) + 2 ;
2001
2002         /*
2003          * Firmware automaticly puts 802 header on so
2004          * we don't need to account for it in the length
2005          */
2006 #ifdef MICSUPPORT
2007         if (test_bit(FLAG_MIC_CAPABLE, &ai->flags) && ai->micstats.enabled &&
2008                 (ntohs(((u16 *)buffer)[6]) != 0x888E)) {
2009                 MICBuffer pMic;
2010
2011                 if (encapsulate(ai, (etherHead *)buffer, &pMic, len - sizeof(etherHead)) != SUCCESS)
2012                         return ERROR;
2013
2014                 *payloadLen = cpu_to_le16(len-sizeof(etherHead)+sizeof(pMic));
2015                 ai->txfids[0].tx_desc.len += sizeof(pMic);
2016                 /* copy data into airo dma buffer */
2017                 memcpy (sendbuf, buffer, sizeof(etherHead));
2018                 buffer += sizeof(etherHead);
2019                 sendbuf += sizeof(etherHead);
2020                 memcpy (sendbuf, &pMic, sizeof(pMic));
2021                 sendbuf += sizeof(pMic);
2022                 memcpy (sendbuf, buffer, len - sizeof(etherHead));
2023         } else
2024 #endif
2025         {
2026                 *payloadLen = cpu_to_le16(len - sizeof(etherHead));
2027
2028                 dev->trans_start = jiffies;
2029
2030                 /* copy data into airo dma buffer */
2031                 memcpy(sendbuf, buffer, len);
2032         }
2033
2034         memcpy_toio(ai->txfids[0].card_ram_off,
2035                 &ai->txfids[0].tx_desc, sizeof(TxFid));
2036
2037         OUT4500(ai, EVACK, 8);
2038
2039         dev_kfree_skb_any(skb);
2040         return 1;
2041 }
2042
2043 static void get_tx_error(struct airo_info *ai, u32 fid)
2044 {
2045         u16 status;
2046
2047         if (fid < 0)
2048                 status = ((WifiCtlHdr *)ai->txfids[0].virtual_host_addr)->ctlhdr.status;
2049         else {
2050                 if (bap_setup(ai, ai->fids[fid] & 0xffff, 4, BAP0) != SUCCESS)
2051                         return;
2052                 bap_read(ai, &status, 2, BAP0);
2053         }
2054         if (le16_to_cpu(status) & 2) /* Too many retries */
2055                 ai->stats.tx_aborted_errors++;
2056         if (le16_to_cpu(status) & 4) /* Transmit lifetime exceeded */
2057                 ai->stats.tx_heartbeat_errors++;
2058         if (le16_to_cpu(status) & 8) /* Aid fail */
2059                 { }
2060         if (le16_to_cpu(status) & 0x10) /* MAC disabled */
2061                 ai->stats.tx_carrier_errors++;
2062         if (le16_to_cpu(status) & 0x20) /* Association lost */
2063                 { }
2064         /* We produce a TXDROP event only for retry or lifetime
2065          * exceeded, because that's the only status that really mean
2066          * that this particular node went away.
2067          * Other errors means that *we* screwed up. - Jean II */
2068         if ((le16_to_cpu(status) & 2) ||
2069              (le16_to_cpu(status) & 4)) {
2070                 union iwreq_data        wrqu;
2071                 char junk[0x18];
2072
2073                 /* Faster to skip over useless data than to do
2074                  * another bap_setup(). We are at offset 0x6 and
2075                  * need to go to 0x18 and read 6 bytes - Jean II */
2076                 bap_read(ai, (u16 *) junk, 0x18, BAP0);
2077
2078                 /* Copy 802.11 dest address.
2079                  * We use the 802.11 header because the frame may
2080                  * not be 802.3 or may be mangled...
2081                  * In Ad-Hoc mode, it will be the node address.
2082                  * In managed mode, it will be most likely the AP addr
2083                  * User space will figure out how to convert it to
2084                  * whatever it needs (IP address or else).
2085                  * - Jean II */
2086                 memcpy(wrqu.addr.sa_data, junk + 0x12, ETH_ALEN);
2087                 wrqu.addr.sa_family = ARPHRD_ETHER;
2088
2089                 /* Send event to user space */
2090                 wireless_send_event(ai->dev, IWEVTXDROP, &wrqu, NULL);
2091         }
2092 }
2093
2094 static void airo_end_xmit(struct net_device *dev) {
2095         u16 status;
2096         int i;
2097         struct airo_info *priv = dev->priv;
2098         struct sk_buff *skb = priv->xmit.skb;
2099         int fid = priv->xmit.fid;
2100         u32 *fids = priv->fids;
2101
2102         clear_bit(JOB_XMIT, &priv->flags);
2103         clear_bit(FLAG_PENDING_XMIT, &priv->flags);
2104         status = transmit_802_3_packet (priv, fids[fid], skb->data);
2105         up(&priv->sem);
2106
2107         i = 0;
2108         if ( status == SUCCESS ) {
2109                 dev->trans_start = jiffies;
2110                 for (; i < MAX_FIDS / 2 && (priv->fids[i] & 0xffff0000); i++);
2111         } else {
2112                 priv->fids[fid] &= 0xffff;
2113                 priv->stats.tx_window_errors++;
2114         }
2115         if (i < MAX_FIDS / 2)
2116                 netif_wake_queue(dev);
2117         dev_kfree_skb(skb);
2118 }
2119
2120 static int airo_start_xmit(struct sk_buff *skb, struct net_device *dev) {
2121         s16 len;
2122         int i, j;
2123         struct airo_info *priv = dev->priv;
2124         u32 *fids = priv->fids;
2125
2126         if ( skb == NULL ) {
2127                 printk( KERN_ERR "airo:  skb == NULL!!!\n" );
2128                 return 0;
2129         }
2130
2131         /* Find a vacant FID */
2132         for( i = 0; i < MAX_FIDS / 2 && (fids[i] & 0xffff0000); i++ );
2133         for( j = i + 1; j < MAX_FIDS / 2 && (fids[j] & 0xffff0000); j++ );
2134
2135         if ( j >= MAX_FIDS / 2 ) {
2136                 netif_stop_queue(dev);
2137
2138                 if (i == MAX_FIDS / 2) {
2139                         priv->stats.tx_fifo_errors++;
2140                         return 1;
2141                 }
2142         }
2143         /* check min length*/
2144         len = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
2145         /* Mark fid as used & save length for later */
2146         fids[i] |= (len << 16);
2147         priv->xmit.skb = skb;
2148         priv->xmit.fid = i;
2149         if (down_trylock(&priv->sem) != 0) {
2150                 set_bit(FLAG_PENDING_XMIT, &priv->flags);
2151                 netif_stop_queue(dev);
2152                 set_bit(JOB_XMIT, &priv->flags);
2153                 wake_up_interruptible(&priv->thr_wait);
2154         } else
2155                 airo_end_xmit(dev);
2156         return 0;
2157 }
2158
2159 static void airo_end_xmit11(struct net_device *dev) {
2160         u16 status;
2161         int i;
2162         struct airo_info *priv = dev->priv;
2163         struct sk_buff *skb = priv->xmit11.skb;
2164         int fid = priv->xmit11.fid;
2165         u32 *fids = priv->fids;
2166
2167         clear_bit(JOB_XMIT11, &priv->flags);
2168         clear_bit(FLAG_PENDING_XMIT11, &priv->flags);
2169         status = transmit_802_11_packet (priv, fids[fid], skb->data);
2170         up(&priv->sem);
2171
2172         i = MAX_FIDS / 2;
2173         if ( status == SUCCESS ) {
2174                 dev->trans_start = jiffies;
2175                 for (; i < MAX_FIDS && (priv->fids[i] & 0xffff0000); i++);
2176         } else {
2177                 priv->fids[fid] &= 0xffff;
2178                 priv->stats.tx_window_errors++;
2179         }
2180         if (i < MAX_FIDS)
2181                 netif_wake_queue(dev);
2182         dev_kfree_skb(skb);
2183 }
2184
2185 static int airo_start_xmit11(struct sk_buff *skb, struct net_device *dev) {
2186         s16 len;
2187         int i, j;
2188         struct airo_info *priv = dev->priv;
2189         u32 *fids = priv->fids;
2190
2191         if (test_bit(FLAG_MPI, &priv->flags)) {
2192                 /* Not implemented yet for MPI350 */
2193                 netif_stop_queue(dev);
2194                 return -ENETDOWN;
2195         }
2196
2197         if ( skb == NULL ) {
2198                 printk( KERN_ERR "airo:  skb == NULL!!!\n" );
2199                 return 0;
2200         }
2201
2202         /* Find a vacant FID */
2203         for( i = MAX_FIDS / 2; i < MAX_FIDS && (fids[i] & 0xffff0000); i++ );
2204         for( j = i + 1; j < MAX_FIDS && (fids[j] & 0xffff0000); j++ );
2205
2206         if ( j >= MAX_FIDS ) {
2207                 netif_stop_queue(dev);
2208
2209                 if (i == MAX_FIDS) {
2210                         priv->stats.tx_fifo_errors++;
2211                         return 1;
2212                 }
2213         }
2214         /* check min length*/
2215         len = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
2216         /* Mark fid as used & save length for later */
2217         fids[i] |= (len << 16);
2218         priv->xmit11.skb = skb;
2219         priv->xmit11.fid = i;
2220         if (down_trylock(&priv->sem) != 0) {
2221                 set_bit(FLAG_PENDING_XMIT11, &priv->flags);
2222                 netif_stop_queue(dev);
2223                 set_bit(JOB_XMIT11, &priv->flags);
2224                 wake_up_interruptible(&priv->thr_wait);
2225         } else
2226                 airo_end_xmit11(dev);
2227         return 0;
2228 }
2229
2230 static void airo_read_stats(struct airo_info *ai) {
2231         StatsRid stats_rid;
2232         u32 *vals = stats_rid.vals;
2233
2234         clear_bit(JOB_STATS, &ai->flags);
2235         if (ai->power) {
2236                 up(&ai->sem);
2237                 return;
2238         }
2239         readStatsRid(ai, &stats_rid, RID_STATS, 0);
2240         up(&ai->sem);
2241
2242         ai->stats.rx_packets = vals[43] + vals[44] + vals[45];
2243         ai->stats.tx_packets = vals[39] + vals[40] + vals[41];
2244         ai->stats.rx_bytes = vals[92];
2245         ai->stats.tx_bytes = vals[91];
2246         ai->stats.rx_errors = vals[0] + vals[2] + vals[3] + vals[4];
2247         ai->stats.tx_errors = vals[42] + ai->stats.tx_fifo_errors;
2248         ai->stats.multicast = vals[43];
2249         ai->stats.collisions = vals[89];
2250
2251         /* detailed rx_errors: */
2252         ai->stats.rx_length_errors = vals[3];
2253         ai->stats.rx_crc_errors = vals[4];
2254         ai->stats.rx_frame_errors = vals[2];
2255         ai->stats.rx_fifo_errors = vals[0];
2256 }
2257
2258 struct net_device_stats *airo_get_stats(struct net_device *dev)
2259 {
2260         struct airo_info *local =  dev->priv;
2261
2262         if (!test_bit(JOB_STATS, &local->flags)) {
2263                 /* Get stats out of the card if available */
2264                 if (down_trylock(&local->sem) != 0) {
2265                         set_bit(JOB_STATS, &local->flags);
2266                         wake_up_interruptible(&local->thr_wait);
2267                 } else
2268                         airo_read_stats(local);
2269         }
2270
2271         return &local->stats;
2272 }
2273
2274 static void airo_set_promisc(struct airo_info *ai) {
2275         Cmd cmd;
2276         Resp rsp;
2277
2278         memset(&cmd, 0, sizeof(cmd));
2279         cmd.cmd=CMD_SETMODE;
2280         clear_bit(JOB_PROMISC, &ai->flags);
2281         cmd.parm0=(ai->flags&IFF_PROMISC) ? PROMISC : NOPROMISC;
2282         issuecommand(ai, &cmd, &rsp);
2283         up(&ai->sem);
2284 }
2285
2286 static void airo_set_multicast_list(struct net_device *dev) {
2287         struct airo_info *ai = dev->priv;
2288
2289         if ((dev->flags ^ ai->flags) & IFF_PROMISC) {
2290                 change_bit(FLAG_PROMISC, &ai->flags);
2291                 if (down_trylock(&ai->sem) != 0) {
2292                         set_bit(JOB_PROMISC, &ai->flags);
2293                         wake_up_interruptible(&ai->thr_wait);
2294                 } else
2295                         airo_set_promisc(ai);
2296         }
2297
2298         if ((dev->flags&IFF_ALLMULTI)||dev->mc_count>0) {
2299                 /* Turn on multicast.  (Should be already setup...) */
2300         }
2301 }
2302
2303 static int airo_set_mac_address(struct net_device *dev, void *p)
2304 {
2305         struct airo_info *ai = dev->priv;
2306         struct sockaddr *addr = p;
2307         Resp rsp;
2308
2309         readConfigRid(ai, 1);
2310         memcpy (ai->config.macAddr, addr->sa_data, dev->addr_len);
2311         set_bit (FLAG_COMMIT, &ai->flags);
2312         disable_MAC(ai, 1);
2313         writeConfigRid (ai, 1);
2314         enable_MAC(ai, &rsp, 1);
2315         memcpy (ai->dev->dev_addr, addr->sa_data, dev->addr_len);
2316         if (ai->wifidev)
2317                 memcpy (ai->wifidev->dev_addr, addr->sa_data, dev->addr_len);
2318         return 0;
2319 }
2320
2321 static int airo_change_mtu(struct net_device *dev, int new_mtu)
2322 {
2323         if ((new_mtu < 68) || (new_mtu > 2400))
2324                 return -EINVAL;
2325         dev->mtu = new_mtu;
2326         return 0;
2327 }
2328
2329
2330 static int airo_close(struct net_device *dev) {
2331         struct airo_info *ai = dev->priv;
2332
2333         netif_stop_queue(dev);
2334
2335         if (ai->wifidev != dev) {
2336 #ifdef POWER_ON_DOWN
2337                 /* Shut power to the card. The idea is that the user can save
2338                  * power when he doesn't need the card with "ifconfig down".
2339                  * That's the method that is most friendly towards the network
2340                  * stack (i.e. the network stack won't try to broadcast
2341                  * anything on the interface and routes are gone. Jean II */
2342                 set_bit(FLAG_RADIO_DOWN, &ai->flags);
2343                 disable_MAC(ai, 1);
2344 #endif
2345                 disable_interrupts( ai );
2346         }
2347         return 0;
2348 }
2349
2350 static void del_airo_dev( struct net_device *dev );
2351
2352 void stop_airo_card( struct net_device *dev, int freeres )
2353 {
2354         struct airo_info *ai = dev->priv;
2355
2356         set_bit(FLAG_RADIO_DOWN, &ai->flags);
2357         disable_MAC(ai, 1);
2358         disable_interrupts(ai);
2359         free_irq( dev->irq, dev );
2360         takedown_proc_entry( dev, ai );
2361         if (test_bit(FLAG_REGISTERED, &ai->flags)) {
2362                 unregister_netdev( dev );
2363                 if (ai->wifidev) {
2364                         unregister_netdev(ai->wifidev);
2365                         free_netdev(ai->wifidev);
2366                         ai->wifidev = NULL;
2367                 }
2368                 clear_bit(FLAG_REGISTERED, &ai->flags);
2369         }
2370         set_bit(JOB_DIE, &ai->flags);
2371         kill_proc(ai->thr_pid, SIGTERM, 1);
2372         wait_for_completion(&ai->thr_exited);
2373
2374         /*
2375          * Clean out tx queue
2376          */
2377         if (test_bit(FLAG_MPI, &ai->flags) && !skb_queue_empty(&ai->txq)) {
2378                 struct sk_buff *skb = NULL;
2379                 for (;(skb = skb_dequeue(&ai->txq));)
2380                         dev_kfree_skb(skb);
2381         }
2382
2383         if (ai->flash)
2384                 kfree(ai->flash);
2385         if (ai->rssi)
2386                 kfree(ai->rssi);
2387         if (ai->APList)
2388                 kfree(ai->APList);
2389         if (ai->SSID)
2390                 kfree(ai->SSID);
2391         if (freeres) {
2392                 /* PCMCIA frees this stuff, so only for PCI and ISA */
2393                 release_region( dev->base_addr, 64 );
2394                 if (test_bit(FLAG_MPI, &ai->flags)) {
2395                         if (ai->pci)
2396                                 mpi_unmap_card(ai->pci);
2397                         if (ai->pcimem)
2398                                 iounmap(ai->pcimem);
2399                         if (ai->pciaux)
2400                                 iounmap(ai->pciaux);
2401                         pci_free_consistent(ai->pci, PCI_SHARED_LEN,
2402                                 ai->shared, ai->shared_dma);
2403                 }
2404         }
2405 #ifdef MICSUPPORT
2406         crypto_free_tfm(ai->tfm);
2407 #endif
2408         del_airo_dev( dev );
2409         free_netdev( dev );
2410 }
2411
2412 EXPORT_SYMBOL(stop_airo_card);
2413
2414 static int add_airo_dev( struct net_device *dev );
2415
2416 int wll_header_parse(struct sk_buff *skb, unsigned char *haddr)
2417 {
2418         memcpy(haddr, skb->mac.raw + 10, ETH_ALEN);
2419         return ETH_ALEN;
2420 }
2421
2422 static void mpi_unmap_card(struct pci_dev *pci)
2423 {
2424         unsigned long mem_start = pci_resource_start(pci, 1);
2425         unsigned long mem_len = pci_resource_len(pci, 1);
2426         unsigned long aux_start = pci_resource_start(pci, 2);
2427         unsigned long aux_len = AUXMEMSIZE;
2428
2429         release_mem_region(aux_start, aux_len);
2430         release_mem_region(mem_start, mem_len);
2431 }
2432
2433 /*************************************************************
2434  *  This routine assumes that descriptors have been setup .
2435  *  Run at insmod time or after reset  when the decriptors
2436  *  have been initialized . Returns 0 if all is well nz
2437  *  otherwise . Does not allocate memory but sets up card
2438  *  using previously allocated descriptors.
2439  */
2440 static int mpi_init_descriptors (struct airo_info *ai)
2441 {
2442         Cmd cmd;
2443         Resp rsp;
2444         int i;
2445         int rc = SUCCESS;
2446
2447         /* Alloc  card RX descriptors */
2448         netif_stop_queue(ai->dev);
2449
2450         memset(&rsp,0,sizeof(rsp));
2451         memset(&cmd,0,sizeof(cmd));
2452
2453         cmd.cmd = CMD_ALLOCATEAUX;
2454         cmd.parm0 = FID_RX;
2455         cmd.parm1 = (ai->rxfids[0].card_ram_off - ai->pciaux);
2456         cmd.parm2 = MPI_MAX_FIDS;
2457         rc=issuecommand(ai, &cmd, &rsp);
2458         if (rc != SUCCESS) {
2459                 printk(KERN_ERR "airo:  Couldn't allocate RX FID\n");
2460                 return rc;
2461         }
2462
2463         for (i=0; i<MPI_MAX_FIDS; i++) {
2464                 memcpy_toio(ai->rxfids[i].card_ram_off,
2465                         &ai->rxfids[i].rx_desc, sizeof(RxFid));
2466         }
2467
2468         /* Alloc card TX descriptors */
2469
2470         memset(&rsp,0,sizeof(rsp));
2471         memset(&cmd,0,sizeof(cmd));
2472
2473         cmd.cmd = CMD_ALLOCATEAUX;
2474         cmd.parm0 = FID_TX;
2475         cmd.parm1 = (ai->txfids[0].card_ram_off - ai->pciaux);
2476         cmd.parm2 = MPI_MAX_FIDS;
2477
2478         for (i=0; i<MPI_MAX_FIDS; i++) {
2479                 ai->txfids[i].tx_desc.valid = 1;
2480                 memcpy_toio(ai->txfids[i].card_ram_off,
2481                         &ai->txfids[i].tx_desc, sizeof(TxFid));
2482         }
2483         ai->txfids[i-1].tx_desc.eoc = 1; /* Last descriptor has EOC set */
2484
2485         rc=issuecommand(ai, &cmd, &rsp);
2486         if (rc != SUCCESS) {
2487                 printk(KERN_ERR "airo:  Couldn't allocate TX FID\n");
2488                 return rc;
2489         }
2490
2491         /* Alloc card Rid descriptor */
2492         memset(&rsp,0,sizeof(rsp));
2493         memset(&cmd,0,sizeof(cmd));
2494
2495         cmd.cmd = CMD_ALLOCATEAUX;
2496         cmd.parm0 = RID_RW;
2497         cmd.parm1 = (ai->config_desc.card_ram_off - ai->pciaux);
2498         cmd.parm2 = 1; /* Magic number... */
2499         rc=issuecommand(ai, &cmd, &rsp);
2500         if (rc != SUCCESS) {
2501                 printk(KERN_ERR "airo:  Couldn't allocate RID\n");
2502                 return rc;
2503         }
2504
2505         memcpy_toio(ai->config_desc.card_ram_off,
2506                 &ai->config_desc.rid_desc, sizeof(Rid));
2507
2508         return rc;
2509 }
2510
2511 /*
2512  * We are setting up three things here:
2513  * 1) Map AUX memory for descriptors: Rid, TxFid, or RxFid.
2514  * 2) Map PCI memory for issueing commands.
2515  * 3) Allocate memory (shared) to send and receive ethernet frames.
2516  */
2517 static int mpi_map_card(struct airo_info *ai, struct pci_dev *pci,
2518                     const char *name)
2519 {
2520         unsigned long mem_start, mem_len, aux_start, aux_len;
2521         int rc = -1;
2522         int i;
2523         unsigned char *busaddroff,*vpackoff;
2524         unsigned char __iomem *pciaddroff;
2525
2526         mem_start = pci_resource_start(pci, 1);
2527         mem_len = pci_resource_len(pci, 1);
2528         aux_start = pci_resource_start(pci, 2);
2529         aux_len = AUXMEMSIZE;
2530
2531         if (!request_mem_region(mem_start, mem_len, name)) {
2532                 printk(KERN_ERR "airo: Couldn't get region %x[%x] for %s\n",
2533                        (int)mem_start, (int)mem_len, name);
2534                 goto out;
2535         }
2536         if (!request_mem_region(aux_start, aux_len, name)) {
2537                 printk(KERN_ERR "airo: Couldn't get region %x[%x] for %s\n",
2538                        (int)aux_start, (int)aux_len, name);
2539                 goto free_region1;
2540         }
2541
2542         ai->pcimem = ioremap(mem_start, mem_len);
2543         if (!ai->pcimem) {
2544                 printk(KERN_ERR "airo: Couldn't map region %x[%x] for %s\n",
2545                        (int)mem_start, (int)mem_len, name);
2546                 goto free_region2;
2547         }
2548         ai->pciaux = ioremap(aux_start, aux_len);
2549         if (!ai->pciaux) {
2550                 printk(KERN_ERR "airo: Couldn't map region %x[%x] for %s\n",
2551                        (int)aux_start, (int)aux_len, name);
2552                 goto free_memmap;
2553         }
2554
2555         /* Reserve PKTSIZE for each fid and 2K for the Rids */
2556         ai->shared = pci_alloc_consistent(pci, PCI_SHARED_LEN, &ai->shared_dma);
2557         if (!ai->shared) {
2558                 printk(KERN_ERR "airo: Couldn't alloc_consistent %d\n",
2559                        PCI_SHARED_LEN);
2560                 goto free_auxmap;
2561         }
2562
2563         /*
2564          * Setup descriptor RX, TX, CONFIG
2565          */
2566         busaddroff = (unsigned char *)ai->shared_dma;
2567         pciaddroff = ai->pciaux + AUX_OFFSET;
2568         vpackoff   = ai->shared;
2569
2570         /* RX descriptor setup */
2571         for(i = 0; i < MPI_MAX_FIDS; i++) {
2572                 ai->rxfids[i].pending = 0;
2573                 ai->rxfids[i].card_ram_off = pciaddroff;
2574                 ai->rxfids[i].virtual_host_addr = vpackoff;
2575                 ai->rxfids[i].rx_desc.host_addr = (dma_addr_t) busaddroff;
2576                 ai->rxfids[i].rx_desc.valid = 1;
2577                 ai->rxfids[i].rx_desc.len = PKTSIZE;
2578                 ai->rxfids[i].rx_desc.rdy = 0;
2579
2580                 pciaddroff += sizeof(RxFid);
2581                 busaddroff += PKTSIZE;
2582                 vpackoff   += PKTSIZE;
2583         }
2584
2585         /* TX descriptor setup */
2586         for(i = 0; i < MPI_MAX_FIDS; i++) {
2587                 ai->txfids[i].card_ram_off = pciaddroff;
2588                 ai->txfids[i].virtual_host_addr = vpackoff;
2589                 ai->txfids[i].tx_desc.valid = 1;
2590                 ai->txfids[i].tx_desc.host_addr = (dma_addr_t) busaddroff;
2591                 memcpy(ai->txfids[i].virtual_host_addr,
2592                         &wifictlhdr8023, sizeof(wifictlhdr8023));
2593
2594                 pciaddroff += sizeof(TxFid);
2595                 busaddroff += PKTSIZE;
2596                 vpackoff   += PKTSIZE;
2597         }
2598         ai->txfids[i-1].tx_desc.eoc = 1; /* Last descriptor has EOC set */
2599
2600         /* Rid descriptor setup */
2601         ai->config_desc.card_ram_off = pciaddroff;
2602         ai->config_desc.virtual_host_addr = vpackoff;
2603         ai->config_desc.rid_desc.host_addr = (dma_addr_t) busaddroff;
2604         ai->ridbus = (dma_addr_t)busaddroff;
2605         ai->config_desc.rid_desc.rid = 0;
2606         ai->config_desc.rid_desc.len = RIDSIZE;
2607         ai->config_desc.rid_desc.valid = 1;
2608         pciaddroff += sizeof(Rid);
2609         busaddroff += RIDSIZE;
2610         vpackoff   += RIDSIZE;
2611
2612         /* Tell card about descriptors */
2613         if (mpi_init_descriptors (ai) != SUCCESS)
2614                 goto free_shared;
2615
2616         return 0;
2617  free_shared:
2618         pci_free_consistent(pci, PCI_SHARED_LEN, ai->shared, ai->shared_dma);
2619  free_auxmap:
2620         iounmap(ai->pciaux);
2621  free_memmap:
2622         iounmap(ai->pcimem);
2623  free_region2:
2624         release_mem_region(aux_start, aux_len);
2625  free_region1:
2626         release_mem_region(mem_start, mem_len);
2627  out:
2628         return rc;
2629 }
2630
2631 static void wifi_setup(struct net_device *dev)
2632 {
2633         dev->hard_header        = NULL;
2634         dev->rebuild_header     = NULL;
2635         dev->hard_header_cache  = NULL;
2636         dev->header_cache_update= NULL;
2637
2638         dev->hard_header_parse  = wll_header_parse;
2639         dev->hard_start_xmit = &airo_start_xmit11;
2640         dev->get_stats = &airo_get_stats;
2641         dev->set_mac_address = &airo_set_mac_address;
2642         dev->do_ioctl = &airo_ioctl;
2643 #ifdef WIRELESS_EXT
2644         dev->wireless_handlers = &airo_handler_def;
2645 #endif /* WIRELESS_EXT */
2646         dev->change_mtu = &airo_change_mtu;
2647         dev->open = &airo_open;
2648         dev->stop = &airo_close;
2649
2650         dev->type               = ARPHRD_IEEE80211;
2651         dev->hard_header_len    = ETH_HLEN;
2652         dev->mtu                = 2312;
2653         dev->addr_len           = ETH_ALEN;
2654         dev->tx_queue_len       = 100; 
2655
2656         memset(dev->broadcast,0xFF, ETH_ALEN);
2657
2658         dev->flags              = IFF_BROADCAST|IFF_MULTICAST;
2659 }
2660
2661 static struct net_device *init_wifidev(struct airo_info *ai,
2662                                         struct net_device *ethdev)
2663 {
2664         int err;
2665         struct net_device *dev = alloc_netdev(0, "wifi%d", wifi_setup);
2666         if (!dev)
2667                 return NULL;
2668         dev->priv = ethdev->priv;
2669         dev->irq = ethdev->irq;
2670         dev->base_addr = ethdev->base_addr;
2671 #ifdef WIRELESS_EXT
2672         dev->wireless_data = ethdev->wireless_data;
2673 #endif /* WIRELESS_EXT */
2674         memcpy(dev->dev_addr, ethdev->dev_addr, dev->addr_len);
2675         err = register_netdev(dev);
2676         if (err<0) {
2677                 free_netdev(dev);
2678                 return NULL;
2679         }
2680         return dev;
2681 }
2682
2683 int reset_card( struct net_device *dev , int lock) {
2684         struct airo_info *ai = dev->priv;
2685
2686         if (lock && down_interruptible(&ai->sem))
2687                 return -1;
2688         waitbusy (ai);
2689         OUT4500(ai,COMMAND,CMD_SOFTRESET);
2690         msleep(200);
2691         waitbusy (ai);
2692         msleep(200);
2693         if (lock)
2694                 up(&ai->sem);
2695         return 0;
2696 }
2697
2698 struct net_device *_init_airo_card( unsigned short irq, int port,
2699                                     int is_pcmcia, struct pci_dev *pci,
2700                                     struct device *dmdev )
2701 {
2702         struct net_device *dev;
2703         struct airo_info *ai;
2704         int i, rc;
2705
2706         /* Create the network device object. */
2707         dev = alloc_etherdev(sizeof(*ai));
2708         if (!dev) {
2709                 printk(KERN_ERR "airo:  Couldn't alloc_etherdev\n");
2710                 return NULL;
2711         }
2712         if (dev_alloc_name(dev, dev->name) < 0) {
2713                 printk(KERN_ERR "airo:  Couldn't get name!\n");
2714                 goto err_out_free;
2715         }
2716
2717         ai = dev->priv;
2718         ai->wifidev = NULL;
2719         ai->flags = 0;
2720         if (pci && (pci->device == 0x5000 || pci->device == 0xa504)) {
2721                 printk(KERN_DEBUG "airo: Found an MPI350 card\n");
2722                 set_bit(FLAG_MPI, &ai->flags);
2723         }
2724         ai->dev = dev;
2725         spin_lock_init(&ai->aux_lock);
2726         sema_init(&ai->sem, 1);
2727         ai->config.len = 0;
2728         ai->pci = pci;
2729         init_waitqueue_head (&ai->thr_wait);
2730         init_completion (&ai->thr_exited);
2731         ai->thr_pid = kernel_thread(airo_thread, dev, CLONE_FS | CLONE_FILES);
2732         if (ai->thr_pid < 0)
2733                 goto err_out_free;
2734 #ifdef MICSUPPORT
2735         ai->tfm = NULL;
2736 #endif
2737         rc = add_airo_dev( dev );
2738         if (rc)
2739                 goto err_out_thr;
2740
2741         /* The Airo-specific entries in the device structure. */
2742         if (test_bit(FLAG_MPI,&ai->flags)) {
2743                 skb_queue_head_init (&ai->txq);
2744                 dev->hard_start_xmit = &mpi_start_xmit;
2745         } else
2746                 dev->hard_start_xmit = &airo_start_xmit;
2747         dev->get_stats = &airo_get_stats;
2748         dev->set_multicast_list = &airo_set_multicast_list;
2749         dev->set_mac_address = &airo_set_mac_address;
2750         dev->do_ioctl = &airo_ioctl;
2751 #ifdef WIRELESS_EXT
2752         dev->wireless_handlers = &airo_handler_def;
2753         ai->wireless_data.spy_data = &ai->spy_data;
2754         dev->wireless_data = &ai->wireless_data;
2755 #endif /* WIRELESS_EXT */
2756         dev->change_mtu = &airo_change_mtu;
2757         dev->open = &airo_open;
2758         dev->stop = &airo_close;
2759         dev->irq = irq;
2760         dev->base_addr = port;
2761
2762         SET_NETDEV_DEV(dev, dmdev);
2763
2764
2765         if (test_bit(FLAG_MPI,&ai->flags))
2766                 reset_card (dev, 1);
2767
2768         rc = request_irq( dev->irq, airo_interrupt, SA_SHIRQ, dev->name, dev );
2769         if (rc) {
2770                 printk(KERN_ERR "airo: register interrupt %d failed, rc %d\n", irq, rc );
2771                 goto err_out_unlink;
2772         }
2773         if (!is_pcmcia) {
2774                 if (!request_region( dev->base_addr, 64, dev->name )) {
2775                         rc = -EBUSY;
2776                         printk(KERN_ERR "airo: Couldn't request region\n");
2777                         goto err_out_irq;
2778                 }
2779         }
2780
2781         if (test_bit(FLAG_MPI,&ai->flags)) {
2782                 if (mpi_map_card(ai, pci, dev->name)) {
2783                         printk(KERN_ERR "airo: Could not map memory\n");
2784                         goto err_out_res;
2785                 }
2786         }
2787
2788         if (probe) {
2789                 if ( setup_card( ai, dev->dev_addr, 1 ) != SUCCESS ) {
2790                         printk( KERN_ERR "airo: MAC could not be enabled\n" );
2791                         rc = -EIO;
2792                         goto err_out_map;
2793                 }
2794         } else if (!test_bit(FLAG_MPI,&ai->flags)) {
2795                 ai->bap_read = fast_bap_read;
2796                 set_bit(FLAG_FLASHING, &ai->flags);
2797         }
2798
2799         rc = register_netdev(dev);
2800         if (rc) {
2801                 printk(KERN_ERR "airo: Couldn't register_netdev\n");
2802                 goto err_out_map;
2803         }
2804         ai->wifidev = init_wifidev(ai, dev);
2805
2806         set_bit(FLAG_REGISTERED,&ai->flags);
2807         printk( KERN_INFO "airo: MAC enabled %s %x:%x:%x:%x:%x:%x\n",
2808                 dev->name,
2809                 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
2810                 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5] );
2811
2812         /* Allocate the transmit buffers */
2813         if (probe && !test_bit(FLAG_MPI,&ai->flags))
2814                 for( i = 0; i < MAX_FIDS; i++ )
2815                         ai->fids[i] = transmit_allocate(ai,2312,i>=MAX_FIDS/2);
2816
2817         setup_proc_entry( dev, dev->priv ); /* XXX check for failure */
2818         netif_start_queue(dev);
2819         SET_MODULE_OWNER(dev);
2820         return dev;
2821
2822 err_out_map:
2823         if (test_bit(FLAG_MPI,&ai->flags) && pci) {
2824                 pci_free_consistent(pci, PCI_SHARED_LEN, ai->shared, ai->shared_dma);
2825                 iounmap(ai->pciaux);
2826                 iounmap(ai->pcimem);
2827                 mpi_unmap_card(ai->pci);
2828         }
2829 err_out_res:
2830         if (!is_pcmcia)
2831                 release_region( dev->base_addr, 64 );
2832 err_out_irq:
2833         free_irq(dev->irq, dev);
2834 err_out_unlink:
2835         del_airo_dev(dev);
2836 err_out_thr:
2837         set_bit(JOB_DIE, &ai->flags);
2838         kill_proc(ai->thr_pid, SIGTERM, 1);
2839         wait_for_completion(&ai->thr_exited);
2840 err_out_free:
2841         free_netdev(dev);
2842         return NULL;
2843 }
2844
2845 struct net_device *init_airo_card( unsigned short irq, int port, int is_pcmcia,
2846                                   struct device *dmdev)
2847 {
2848         return _init_airo_card ( irq, port, is_pcmcia, NULL, dmdev);
2849 }
2850
2851 EXPORT_SYMBOL(init_airo_card);
2852
2853 static int waitbusy (struct airo_info *ai) {
2854         int delay = 0;
2855         while ((IN4500 (ai, COMMAND) & COMMAND_BUSY) & (delay < 10000)) {
2856                 udelay (10);
2857                 if ((++delay % 20) == 0)
2858                         OUT4500(ai, EVACK, EV_CLEARCOMMANDBUSY);
2859         }
2860         return delay < 10000;
2861 }
2862
2863 int reset_airo_card( struct net_device *dev )
2864 {
2865         int i;
2866         struct airo_info *ai = dev->priv;
2867
2868         if (reset_card (dev, 1))
2869                 return -1;
2870
2871         if ( setup_card(ai, dev->dev_addr, 1 ) != SUCCESS ) {
2872                 printk( KERN_ERR "airo: MAC could not be enabled\n" );
2873                 return -1;
2874         }
2875         printk( KERN_INFO "airo: MAC enabled %s %x:%x:%x:%x:%x:%x\n", dev->name,
2876                         dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
2877                         dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
2878         /* Allocate the transmit buffers if needed */
2879         if (!test_bit(FLAG_MPI,&ai->flags))
2880                 for( i = 0; i < MAX_FIDS; i++ )
2881                         ai->fids[i] = transmit_allocate (ai,2312,i>=MAX_FIDS/2);
2882
2883         enable_interrupts( ai );
2884         netif_wake_queue(dev);
2885         return 0;
2886 }
2887
2888 EXPORT_SYMBOL(reset_airo_card);
2889
2890 static void airo_send_event(struct net_device *dev) {
2891         struct airo_info *ai = dev->priv;
2892         union iwreq_data wrqu;
2893         StatusRid status_rid;
2894
2895         clear_bit(JOB_EVENT, &ai->flags);
2896         PC4500_readrid(ai, RID_STATUS, &status_rid, sizeof(status_rid), 0);
2897         up(&ai->sem);
2898         wrqu.data.length = 0;
2899         wrqu.data.flags = 0;
2900         memcpy(wrqu.ap_addr.sa_data, status_rid.bssid[0], ETH_ALEN);
2901         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
2902
2903         /* Send event to user space */
2904         wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
2905 }
2906
2907 static int airo_thread(void *data) {
2908         struct net_device *dev = data;
2909         struct airo_info *ai = dev->priv;
2910         int locked;
2911         
2912         daemonize("%s", dev->name);
2913         allow_signal(SIGTERM);
2914
2915         while(1) {
2916                 if (signal_pending(current))
2917                         flush_signals(current);
2918
2919                 /* make swsusp happy with our thread */
2920                 try_to_freeze();
2921
2922                 if (test_bit(JOB_DIE, &ai->flags))
2923                         break;
2924
2925                 if (ai->flags & JOB_MASK) {
2926                         locked = down_interruptible(&ai->sem);
2927                 } else {
2928                         wait_queue_t wait;
2929
2930                         init_waitqueue_entry(&wait, current);
2931                         add_wait_queue(&ai->thr_wait, &wait);
2932                         for (;;) {
2933                                 set_current_state(TASK_INTERRUPTIBLE);
2934                                 if (ai->flags & JOB_MASK)
2935                                         break;
2936                                 if (ai->expires) {
2937                                         if (time_after_eq(jiffies,ai->expires)){
2938                                                 set_bit(JOB_AUTOWEP,&ai->flags);
2939                                                 break;
2940                                         }
2941                                         if (!signal_pending(current)) {
2942                                                 schedule_timeout(ai->expires - jiffies);
2943                                                 continue;
2944                                         }
2945                                 } else if (!signal_pending(current)) {
2946                                         schedule();
2947                                         continue;
2948                                 }
2949                                 break;
2950                         }
2951                         current->state = TASK_RUNNING;
2952                         remove_wait_queue(&ai->thr_wait, &wait);
2953                         locked = 1;
2954                 }
2955
2956                 if (locked)
2957                         continue;
2958
2959                 if (test_bit(JOB_DIE, &ai->flags)) {
2960                         up(&ai->sem);
2961                         break;
2962                 }
2963
2964                 if (ai->power || test_bit(FLAG_FLASHING, &ai->flags)) {
2965                         up(&ai->sem);
2966                         continue;
2967                 }
2968
2969                 if (test_bit(JOB_XMIT, &ai->flags))
2970                         airo_end_xmit(dev);
2971                 else if (test_bit(JOB_XMIT11, &ai->flags))
2972                         airo_end_xmit11(dev);
2973                 else if (test_bit(JOB_STATS, &ai->flags))
2974                         airo_read_stats(ai);
2975                 else if (test_bit(JOB_WSTATS, &ai->flags))
2976                         airo_read_wireless_stats(ai);
2977                 else if (test_bit(JOB_PROMISC, &ai->flags))
2978                         airo_set_promisc(ai);
2979 #ifdef MICSUPPORT
2980                 else if (test_bit(JOB_MIC, &ai->flags))
2981                         micinit(ai);
2982 #endif
2983                 else if (test_bit(JOB_EVENT, &ai->flags))
2984                         airo_send_event(dev);
2985                 else if (test_bit(JOB_AUTOWEP, &ai->flags))
2986                         timer_func(dev);
2987         }
2988         complete_and_exit (&ai->thr_exited, 0);
2989 }
2990
2991 static irqreturn_t airo_interrupt ( int irq, void* dev_id, struct pt_regs *regs) {
2992         struct net_device *dev = (struct net_device *)dev_id;
2993         u16 status;
2994         u16 fid;
2995         struct airo_info *apriv = dev->priv;
2996         u16 savedInterrupts = 0;
2997         int handled = 0;
2998
2999         if (!netif_device_present(dev))
3000                 return IRQ_NONE;
3001
3002         for (;;) {
3003                 status = IN4500( apriv, EVSTAT );
3004                 if ( !(status & STATUS_INTS) || status == 0xffff ) break;
3005
3006                 handled = 1;
3007
3008                 if ( status & EV_AWAKE ) {
3009                         OUT4500( apriv, EVACK, EV_AWAKE );
3010                         OUT4500( apriv, EVACK, EV_AWAKE );
3011                 }
3012
3013                 if (!savedInterrupts) {
3014                         savedInterrupts = IN4500( apriv, EVINTEN );
3015                         OUT4500( apriv, EVINTEN, 0 );
3016                 }
3017
3018                 if ( status & EV_MIC ) {
3019                         OUT4500( apriv, EVACK, EV_MIC );
3020 #ifdef MICSUPPORT
3021                         if (test_bit(FLAG_MIC_CAPABLE, &apriv->flags)) {
3022                                 set_bit(JOB_MIC, &apriv->flags);
3023                                 wake_up_interruptible(&apriv->thr_wait);
3024                         }
3025 #endif
3026                 }
3027                 if ( status & EV_LINK ) {
3028                         union iwreq_data        wrqu;
3029                         /* The link status has changed, if you want to put a
3030                            monitor hook in, do it here.  (Remember that
3031                            interrupts are still disabled!)
3032                         */
3033                         u16 newStatus = IN4500(apriv, LINKSTAT);
3034                         OUT4500( apriv, EVACK, EV_LINK);
3035                         /* Here is what newStatus means: */
3036 #define NOBEACON 0x8000 /* Loss of sync - missed beacons */
3037 #define MAXRETRIES 0x8001 /* Loss of sync - max retries */
3038 #define MAXARL 0x8002 /* Loss of sync - average retry level exceeded*/
3039 #define FORCELOSS 0x8003 /* Loss of sync - host request */
3040 #define TSFSYNC 0x8004 /* Loss of sync - TSF synchronization */
3041 #define DEAUTH 0x8100 /* Deauthentication (low byte is reason code) */
3042 #define DISASS 0x8200 /* Disassociation (low byte is reason code) */
3043 #define ASSFAIL 0x8400 /* Association failure (low byte is reason
3044                           code) */
3045 #define AUTHFAIL 0x0300 /* Authentication failure (low byte is reason
3046                            code) */
3047 #define ASSOCIATED 0x0400 /* Assocatied */
3048 #define RC_RESERVED 0 /* Reserved return code */
3049 #define RC_NOREASON 1 /* Unspecified reason */
3050 #define RC_AUTHINV 2 /* Previous authentication invalid */
3051 #define RC_DEAUTH 3 /* Deauthenticated because sending station is
3052                        leaving */
3053 #define RC_NOACT 4 /* Disassociated due to inactivity */
3054 #define RC_MAXLOAD 5 /* Disassociated because AP is unable to handle
3055                         all currently associated stations */
3056 #define RC_BADCLASS2 6 /* Class 2 frame received from
3057                           non-Authenticated station */
3058 #define RC_BADCLASS3 7 /* Class 3 frame received from
3059                           non-Associated station */
3060 #define RC_STATLEAVE 8 /* Disassociated because sending station is
3061                           leaving BSS */
3062 #define RC_NOAUTH 9 /* Station requesting (Re)Association is not
3063                        Authenticated with the responding station */
3064                         if (newStatus != ASSOCIATED) {
3065                                 if (auto_wep && !apriv->expires) {
3066                                         apriv->expires = RUN_AT(3*HZ);
3067                                         wake_up_interruptible(&apriv->thr_wait);
3068                                 }
3069                         } else {
3070                                 struct task_struct *task = apriv->task;
3071                                 if (auto_wep)
3072                                         apriv->expires = 0;
3073                                 if (task)
3074                                         wake_up_process (task);
3075                                 set_bit(FLAG_UPDATE_UNI, &apriv->flags);
3076                                 set_bit(FLAG_UPDATE_MULTI, &apriv->flags);
3077                         }
3078                         /* Question : is ASSOCIATED the only status
3079                          * that is valid ? We want to catch handover
3080                          * and reassociations as valid status
3081                          * Jean II */
3082                         if(newStatus == ASSOCIATED) {
3083                                 if (apriv->scan_timestamp) {
3084                                         /* Send an empty event to user space.
3085                                          * We don't send the received data on
3086                                          * the event because it would require
3087                                          * us to do complex transcoding, and
3088                                          * we want to minimise the work done in
3089                                          * the irq handler. Use a request to
3090                                          * extract the data - Jean II */
3091                                         wrqu.data.length = 0;
3092                                         wrqu.data.flags = 0;
3093                                         wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
3094                                         apriv->scan_timestamp = 0;
3095                                 }
3096                                 if (down_trylock(&apriv->sem) != 0) {
3097                                         set_bit(JOB_EVENT, &apriv->flags);
3098                                         wake_up_interruptible(&apriv->thr_wait);
3099                                 } else
3100                                         airo_send_event(dev);
3101                         } else {
3102                                 memset(wrqu.ap_addr.sa_data, '\0', ETH_ALEN);
3103                                 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
3104
3105                                 /* Send event to user space */
3106                                 wireless_send_event(dev, SIOCGIWAP, &wrqu,NULL);
3107                         }
3108                 }
3109
3110                 /* Check to see if there is something to receive */
3111                 if ( status & EV_RX  ) {
3112                         struct sk_buff *skb = NULL;
3113                         u16 fc, len, hdrlen = 0;
3114 #pragma pack(1)
3115                         struct {
3116                                 u16 status, len;
3117                                 u8 rssi[2];
3118                                 u8 rate;
3119                                 u8 freq;
3120                                 u16 tmp[4];
3121                         } hdr;
3122 #pragma pack()
3123                         u16 gap;
3124                         u16 tmpbuf[4];
3125                         u16 *buffer;
3126
3127                         if (test_bit(FLAG_MPI,&apriv->flags)) {
3128                                 if (test_bit(FLAG_802_11, &apriv->flags))
3129                                         mpi_receive_802_11(apriv);
3130                                 else
3131                                         mpi_receive_802_3(apriv);
3132                                 OUT4500(apriv, EVACK, EV_RX);
3133                                 goto exitrx;
3134                         }
3135
3136                         fid = IN4500( apriv, RXFID );
3137
3138                         /* Get the packet length */
3139                         if (test_bit(FLAG_802_11, &apriv->flags)) {
3140                                 bap_setup (apriv, fid, 4, BAP0);
3141                                 bap_read (apriv, (u16*)&hdr, sizeof(hdr), BAP0);
3142                                 /* Bad CRC. Ignore packet */
3143                                 if (le16_to_cpu(hdr.status) & 2)
3144                                         hdr.len = 0;
3145                                 if (apriv->wifidev == NULL)
3146                                         hdr.len = 0;
3147                         } else {
3148                                 bap_setup (apriv, fid, 0x36, BAP0);
3149                                 bap_read (apriv, (u16*)&hdr.len, 2, BAP0);
3150                         }
3151                         len = le16_to_cpu(hdr.len);
3152
3153                         if (len > 2312) {
3154                                 printk( KERN_ERR "airo: Bad size %d\n", len );
3155                                 goto badrx;
3156                         }
3157                         if (len == 0)
3158                                 goto badrx;
3159
3160                         if (test_bit(FLAG_802_11, &apriv->flags)) {
3161                                 bap_read (apriv, (u16*)&fc, sizeof(fc), BAP0);
3162                                 fc = le16_to_cpu(fc);
3163                                 switch (fc & 0xc) {
3164                                         case 4:
3165                                                 if ((fc & 0xe0) == 0xc0)
3166                                                         hdrlen = 10;
3167                                                 else
3168                                                         hdrlen = 16;
3169                                                 break;
3170                                         case 8:
3171                                                 if ((fc&0x300)==0x300){
3172                                                         hdrlen = 30;
3173                                                         break;
3174                                                 }
3175                                         default:
3176                                                 hdrlen = 24;
3177                                 }
3178                         } else
3179                                 hdrlen = ETH_ALEN * 2;
3180
3181                         skb = dev_alloc_skb( len + hdrlen + 2 + 2 );
3182                         if ( !skb ) {
3183                                 apriv->stats.rx_dropped++;
3184                                 goto badrx;
3185                         }
3186                         skb_reserve(skb, 2); /* This way the IP header is aligned */
3187                         buffer = (u16*)skb_put (skb, len + hdrlen);
3188                         if (test_bit(FLAG_802_11, &apriv->flags)) {
3189                                 buffer[0] = fc;
3190                                 bap_read (apriv, buffer + 1, hdrlen - 2, BAP0);
3191                                 if (hdrlen == 24)
3192                                         bap_read (apriv, tmpbuf, 6, BAP0);
3193
3194                                 bap_read (apriv, &gap, sizeof(gap), BAP0);
3195                                 gap = le16_to_cpu(gap);
3196                                 if (gap) {
3197                                         if (gap <= 8)
3198                                                 bap_read (apriv, tmpbuf, gap, BAP0);
3199                                         else
3200                                                 printk(KERN_ERR "airo: gaplen too big. Problems will follow...\n");
3201                                 }
3202                                 bap_read (apriv, buffer + hdrlen/2, len, BAP0);
3203                         } else {
3204 #ifdef MICSUPPORT
3205                                 MICBuffer micbuf;
3206 #endif
3207                                 bap_read (apriv, buffer, ETH_ALEN*2, BAP0);
3208 #ifdef MICSUPPORT
3209                                 if (apriv->micstats.enabled) {
3210                                         bap_read (apriv,(u16*)&micbuf,sizeof(micbuf),BAP0);
3211                                         if (ntohs(micbuf.typelen) > 0x05DC)
3212                                                 bap_setup (apriv, fid, 0x44, BAP0);
3213                                         else {
3214                                                 if (len <= sizeof(micbuf))
3215                                                         goto badmic;
3216
3217                                                 len -= sizeof(micbuf);
3218                                                 skb_trim (skb, len + hdrlen);
3219                                         }
3220                                 }
3221 #endif
3222                                 bap_read(apriv,buffer+ETH_ALEN,len,BAP0);
3223 #ifdef MICSUPPORT
3224                                 if (decapsulate(apriv,&micbuf,(etherHead*)buffer,len)) {
3225 badmic:
3226                                         dev_kfree_skb_irq (skb);
3227 #else
3228                                 if (0) {
3229 #endif
3230 badrx:
3231                                         OUT4500( apriv, EVACK, EV_RX);
3232                                         goto exitrx;
3233                                 }
3234                         }
3235 #ifdef WIRELESS_SPY
3236                         if (apriv->spy_data.spy_number > 0) {
3237                                 char *sa;
3238                                 struct iw_quality wstats;
3239                                 /* Prepare spy data : addr + qual */
3240                                 if (!test_bit(FLAG_802_11, &apriv->flags)) {
3241                                         sa = (char*)buffer + 6;
3242                                         bap_setup (apriv, fid, 8, BAP0);
3243                                         bap_read (apriv, (u16*)hdr.rssi, 2, BAP0);
3244                                 } else
3245                                         sa = (char*)buffer + 10;
3246                                 wstats.qual = hdr.rssi[0];
3247                                 if (apriv->rssi)
3248                                         wstats.level = 0x100 - apriv->rssi[hdr.rssi[1]].rssidBm;
3249                                 else
3250                                         wstats.level = (hdr.rssi[1] + 321) / 2;
3251                                 wstats.noise = apriv->wstats.qual.noise;
3252                                 wstats.updated = IW_QUAL_LEVEL_UPDATED
3253                                         | IW_QUAL_QUAL_UPDATED
3254                                         | IW_QUAL_NOISE_UPDATED;
3255                                 /* Update spy records */
3256                                 wireless_spy_update(dev, sa, &wstats);
3257                         }
3258 #endif /* WIRELESS_SPY */
3259                         OUT4500( apriv, EVACK, EV_RX);
3260
3261                         if (test_bit(FLAG_802_11, &apriv->flags)) {
3262                                 skb->mac.raw = skb->data;
3263                                 skb->pkt_type = PACKET_OTHERHOST;
3264                                 skb->dev = apriv->wifidev;
3265                                 skb->protocol = htons(ETH_P_802_2);
3266                         } else {
3267                                 skb->dev = dev;
3268                                 skb->protocol = eth_type_trans(skb,dev);
3269                         }
3270                         skb->dev->last_rx = jiffies;
3271                         skb->ip_summed = CHECKSUM_NONE;
3272
3273                         netif_rx( skb );
3274                 }
3275 exitrx:
3276
3277                 /* Check to see if a packet has been transmitted */
3278                 if (  status & ( EV_TX|EV_TXCPY|EV_TXEXC ) ) {
3279                         int i;
3280                         int len = 0;
3281                         int index = -1;
3282
3283                         if (test_bit(FLAG_MPI,&apriv->flags)) {
3284                                 unsigned long flags;
3285
3286                                 if (status & EV_TXEXC)
3287                                         get_tx_error(apriv, -1);
3288                                 spin_lock_irqsave(&apriv->aux_lock, flags);
3289                                 if (!skb_queue_empty(&apriv->txq)) {
3290                                         spin_unlock_irqrestore(&apriv->aux_lock,flags);
3291                                         mpi_send_packet (dev);
3292                                 } else {
3293                                         clear_bit(FLAG_PENDING_XMIT, &apriv->flags);
3294                                         spin_unlock_irqrestore(&apriv->aux_lock,flags);
3295                                         netif_wake_queue (dev);
3296                                 }
3297                                 OUT4500( apriv, EVACK,
3298                                         status & (EV_TX|EV_TXCPY|EV_TXEXC));
3299                                 goto exittx;
3300                         }
3301
3302                         fid = IN4500(apriv, TXCOMPLFID);
3303
3304                         for( i = 0; i < MAX_FIDS; i++ ) {
3305                                 if ( ( apriv->fids[i] & 0xffff ) == fid ) {
3306                                         len = apriv->fids[i] >> 16;
3307                                         index = i;
3308                                 }
3309                         }
3310                         if (index != -1) {
3311                                 if (status & EV_TXEXC)
3312                                         get_tx_error(apriv, index);
3313                                 OUT4500( apriv, EVACK, status & (EV_TX | EV_TXEXC));
3314                                 /* Set up to be used again */
3315                                 apriv->fids[index] &= 0xffff;
3316                                 if (index < MAX_FIDS / 2) {
3317                                         if (!test_bit(FLAG_PENDING_XMIT, &apriv->flags))
3318                                                 netif_wake_queue(dev);
3319                                 } else {
3320                                         if (!test_bit(FLAG_PENDING_XMIT11, &apriv->flags))
3321                                                 netif_wake_queue(apriv->wifidev);
3322                                 }
3323                         } else {
3324                                 OUT4500( apriv, EVACK, status & (EV_TX | EV_TXCPY | EV_TXEXC));
3325                                 printk( KERN_ERR "airo: Unallocated FID was used to xmit\n" );
3326                         }
3327                 }
3328 exittx:
3329                 if ( status & ~STATUS_INTS & ~IGNORE_INTS )
3330                         printk( KERN_WARNING "airo: Got weird status %x\n",
3331                                 status & ~STATUS_INTS & ~IGNORE_INTS );
3332         }
3333
3334         if (savedInterrupts)
3335                 OUT4500( apriv, EVINTEN, savedInterrupts );
3336
3337         /* done.. */
3338         return IRQ_RETVAL(handled);
3339 }
3340
3341 /*
3342  *  Routines to talk to the card
3343  */
3344
3345 /*
3346  *  This was originally written for the 4500, hence the name
3347  *  NOTE:  If use with 8bit mode and SMP bad things will happen!
3348  *         Why would some one do 8 bit IO in an SMP machine?!?
3349  */
3350 static void OUT4500( struct airo_info *ai, u16 reg, u16 val ) {
3351         if (test_bit(FLAG_MPI,&ai->flags))
3352                 reg <<= 1;
3353         if ( !do8bitIO )
3354                 outw( val, ai->dev->base_addr + reg );
3355         else {
3356                 outb( val & 0xff, ai->dev->base_addr + reg );
3357                 outb( val >> 8, ai->dev->base_addr + reg + 1 );
3358         }
3359 }
3360
3361 static u16 IN4500( struct airo_info *ai, u16 reg ) {
3362         unsigned short rc;
3363
3364         if (test_bit(FLAG_MPI,&ai->flags))
3365                 reg <<= 1;
3366         if ( !do8bitIO )
3367                 rc = inw( ai->dev->base_addr + reg );
3368         else {
3369                 rc = inb( ai->dev->base_addr + reg );
3370                 rc += ((int)inb( ai->dev->base_addr + reg + 1 )) << 8;
3371         }
3372         return rc;
3373 }
3374
3375 static int enable_MAC( struct airo_info *ai, Resp *rsp, int lock ) {
3376         int rc;
3377         Cmd cmd;
3378
3379         /* FLAG_RADIO_OFF : Radio disabled via /proc or Wireless Extensions
3380          * FLAG_RADIO_DOWN : Radio disabled via "ifconfig ethX down"
3381          * Note : we could try to use !netif_running(dev) in enable_MAC()
3382          * instead of this flag, but I don't trust it *within* the
3383          * open/close functions, and testing both flags together is
3384          * "cheaper" - Jean II */
3385         if (ai->flags & FLAG_RADIO_MASK) return SUCCESS;
3386
3387         if (lock && down_interruptible(&ai->sem))
3388                 return -ERESTARTSYS;
3389
3390         if (!test_bit(FLAG_ENABLED, &ai->flags)) {
3391                 memset(&cmd, 0, sizeof(cmd));
3392                 cmd.cmd = MAC_ENABLE;
3393                 rc = issuecommand(ai, &cmd, rsp);
3394                 if (rc == SUCCESS)
3395                         set_bit(FLAG_ENABLED, &ai->flags);
3396         } else
3397                 rc = SUCCESS;
3398
3399         if (lock)
3400             up(&ai->sem);
3401
3402         if (rc)
3403                 printk(KERN_ERR "%s: Cannot enable MAC, err=%d\n",
3404                         __FUNCTION__,rc);
3405         return rc;
3406 }
3407
3408 static void disable_MAC( struct airo_info *ai, int lock ) {
3409         Cmd cmd;
3410         Resp rsp;
3411
3412         if (lock && down_interruptible(&ai->sem))
3413                 return;
3414
3415         if (test_bit(FLAG_ENABLED, &ai->flags)) {
3416                 memset(&cmd, 0, sizeof(cmd));
3417                 cmd.cmd = MAC_DISABLE; // disable in case already enabled
3418                 issuecommand(ai, &cmd, &rsp);
3419                 clear_bit(FLAG_ENABLED, &ai->flags);
3420         }
3421         if (lock)
3422                 up(&ai->sem);
3423 }
3424
3425 static void enable_interrupts( struct airo_info *ai ) {
3426         /* Enable the interrupts */
3427         OUT4500( ai, EVINTEN, STATUS_INTS );
3428 }
3429
3430 static void disable_interrupts( struct airo_info *ai ) {
3431         OUT4500( ai, EVINTEN, 0 );
3432 }
3433
3434 static void mpi_receive_802_3(struct airo_info *ai)
3435 {
3436         RxFid rxd;
3437         int len = 0;
3438         struct sk_buff *skb;
3439         char *buffer;
3440 #ifdef MICSUPPORT
3441         int off = 0;
3442         MICBuffer micbuf;
3443 #endif
3444
3445         memcpy_fromio(&rxd, ai->rxfids[0].card_ram_off, sizeof(rxd));
3446         /* Make sure we got something */
3447         if (rxd.rdy && rxd.valid == 0) {
3448                 len = rxd.len + 12;
3449                 if (len < 12 || len > 2048)
3450                         goto badrx;
3451
3452                 skb = dev_alloc_skb(len);
3453                 if (!skb) {
3454                         ai->stats.rx_dropped++;
3455                         goto badrx;
3456                 }
3457                 buffer = skb_put(skb,len);
3458 #ifdef MICSUPPORT
3459                 memcpy(buffer, ai->rxfids[0].virtual_host_addr, ETH_ALEN * 2);
3460                 if (ai->micstats.enabled) {
3461                         memcpy(&micbuf,
3462                                 ai->rxfids[0].virtual_host_addr + ETH_ALEN * 2,
3463                                 sizeof(micbuf));
3464                         if (ntohs(micbuf.typelen) <= 0x05DC) {
3465                                 if (len <= sizeof(micbuf) + ETH_ALEN * 2)
3466                                         goto badmic;
3467
3468                                 off = sizeof(micbuf);
3469                                 skb_trim (skb, len - off);
3470                         }
3471                 }
3472                 memcpy(buffer + ETH_ALEN * 2,
3473                         ai->rxfids[0].virtual_host_addr + ETH_ALEN * 2 + off,
3474                         len - ETH_ALEN * 2 - off);
3475                 if (decapsulate (ai, &micbuf, (etherHead*)buffer, len - off - ETH_ALEN * 2)) {
3476 badmic:
3477                         dev_kfree_skb_irq (skb);
3478                         goto badrx;
3479                 }
3480 #else
3481                 memcpy(buffer, ai->rxfids[0].virtual_host_addr, len);
3482 #endif
3483 #ifdef WIRELESS_SPY
3484                 if (ai->spy_data.spy_number > 0) {
3485                         char *sa;
3486                         struct iw_quality wstats;
3487                         /* Prepare spy data : addr + qual */
3488                         sa = buffer + ETH_ALEN;
3489                         wstats.qual = 0; /* XXX Where do I get that info from ??? */
3490                         wstats.level = 0;
3491                         wstats.updated = 0;
3492                         /* Update spy records */
3493                         wireless_spy_update(ai->dev, sa, &wstats);
3494                 }
3495 #endif /* WIRELESS_SPY */
3496
3497                 skb->dev = ai->dev;
3498                 skb->ip_summed = CHECKSUM_NONE;
3499                 skb->protocol = eth_type_trans(skb, ai->dev);
3500                 skb->dev->last_rx = jiffies;
3501                 netif_rx(skb);
3502         }
3503 badrx:
3504         if (rxd.valid == 0) {
3505                 rxd.valid = 1;
3506                 rxd.rdy = 0;
3507                 rxd.len = PKTSIZE;
3508                 memcpy_toio(ai->rxfids[0].card_ram_off, &rxd, sizeof(rxd));
3509         }
3510 }
3511
3512 void mpi_receive_802_11 (struct airo_info *ai)
3513 {
3514         RxFid rxd;
3515         struct sk_buff *skb = NULL;
3516         u16 fc, len, hdrlen = 0;
3517 #pragma pack(1)
3518         struct {
3519                 u16 status, len;
3520                 u8 rssi[2];
3521                 u8 rate;
3522                 u8 freq;
3523                 u16 tmp[4];
3524         } hdr;
3525 #pragma pack()
3526         u16 gap;
3527         u16 *buffer;
3528         char *ptr = ai->rxfids[0].virtual_host_addr+4;
3529
3530         memcpy_fromio(&rxd, ai->rxfids[0].card_ram_off, sizeof(rxd));
3531         memcpy ((char *)&hdr, ptr, sizeof(hdr));
3532         ptr += sizeof(hdr);
3533         /* Bad CRC. Ignore packet */
3534         if (le16_to_cpu(hdr.status) & 2)
3535                 hdr.len = 0;
3536         if (ai->wifidev == NULL)
3537                 hdr.len = 0;
3538         len = le16_to_cpu(hdr.len);
3539         if (len > 2312) {
3540                 printk( KERN_ERR "airo: Bad size %d\n", len );
3541                 goto badrx;
3542         }
3543         if (len == 0)
3544                 goto badrx;
3545
3546         memcpy ((char *)&fc, ptr, sizeof(fc));
3547         fc = le16_to_cpu(fc);
3548         switch (fc & 0xc) {
3549                 case 4:
3550                         if ((fc & 0xe0) == 0xc0)
3551                                 hdrlen = 10;
3552                         else
3553                                 hdrlen = 16;
3554                         break;
3555                 case 8:
3556                         if ((fc&0x300)==0x300){
3557                                 hdrlen = 30;
3558                                 break;
3559                         }
3560                 default:
3561                         hdrlen = 24;
3562         }
3563
3564         skb = dev_alloc_skb( len + hdrlen + 2 );
3565         if ( !skb ) {
3566                 ai->stats.rx_dropped++;
3567                 goto badrx;
3568         }
3569         buffer = (u16*)skb_put (skb, len + hdrlen);
3570         memcpy ((char *)buffer, ptr, hdrlen);
3571         ptr += hdrlen;
3572         if (hdrlen == 24)
3573                 ptr += 6;
3574         memcpy ((char *)&gap, ptr, sizeof(gap));
3575         ptr += sizeof(gap);
3576         gap = le16_to_cpu(gap);
3577         if (gap) {
3578                 if (gap <= 8)
3579                         ptr += gap;
3580                 else
3581                         printk(KERN_ERR
3582                             "airo: gaplen too big. Problems will follow...\n");
3583         }
3584         memcpy ((char *)buffer + hdrlen, ptr, len);
3585         ptr += len;
3586 #ifdef IW_WIRELESS_SPY    /* defined in iw_handler.h */
3587         if (ai->spy_data.spy_number > 0) {
3588                 char *sa;
3589                 struct iw_quality wstats;
3590                 /* Prepare spy data : addr + qual */
3591                 sa = (char*)buffer + 10;
3592                 wstats.qual = hdr.rssi[0];
3593                 if (ai->rssi)
3594                         wstats.level = 0x100 - ai->rssi[hdr.rssi[1]].rssidBm;
3595                 else
3596                         wstats.level = (hdr.rssi[1] + 321) / 2;
3597                 wstats.noise = ai->wstats.qual.noise;
3598                 wstats.updated = IW_QUAL_QUAL_UPDATED
3599                         | IW_QUAL_LEVEL_UPDATED
3600                         | IW_QUAL_NOISE_UPDATED;
3601                 /* Update spy records */
3602                 wireless_spy_update(ai->dev, sa, &wstats);
3603         }
3604 #endif /* IW_WIRELESS_SPY */
3605         skb->mac.raw = skb->data;
3606         skb->pkt_type = PACKET_OTHERHOST;
3607         skb->dev = ai->wifidev;
3608         skb->protocol = htons(ETH_P_802_2);
3609         skb->dev->last_rx = jiffies;
3610         skb->ip_summed = CHECKSUM_NONE;
3611         netif_rx( skb );
3612 badrx:
3613         if (rxd.valid == 0) {
3614                 rxd.valid = 1;
3615                 rxd.rdy = 0;
3616                 rxd.len = PKTSIZE;
3617                 memcpy_toio(ai->rxfids[0].card_ram_off, &rxd, sizeof(rxd));
3618         }
3619 }
3620
3621 static u16 setup_card(struct airo_info *ai, u8 *mac, int lock)
3622 {
3623         Cmd cmd;
3624         Resp rsp;
3625         int status;
3626         int i;
3627         SsidRid mySsid;
3628         u16 lastindex;
3629         WepKeyRid wkr;
3630         int rc;
3631
3632         memset( &mySsid, 0, sizeof( mySsid ) );
3633         if (ai->flash) {
3634                 kfree (ai->flash);
3635                 ai->flash = NULL;
3636         }
3637
3638         /* The NOP is the first step in getting the card going */
3639         cmd.cmd = NOP;
3640         cmd.parm0 = cmd.parm1 = cmd.parm2 = 0;
3641         if (lock && down_interruptible(&ai->sem))
3642                 return ERROR;
3643         if ( issuecommand( ai, &cmd, &rsp ) != SUCCESS ) {
3644                 if (lock)
3645                         up(&ai->sem);
3646                 return ERROR;
3647         }
3648         disable_MAC( ai, 0);
3649
3650         // Let's figure out if we need to use the AUX port
3651         if (!test_bit(FLAG_MPI,&ai->flags)) {
3652                 cmd.cmd = CMD_ENABLEAUX;
3653                 if (issuecommand(ai, &cmd, &rsp) != SUCCESS) {
3654                         if (lock)
3655                                 up(&ai->sem);
3656                         printk(KERN_ERR "airo: Error checking for AUX port\n");
3657                         return ERROR;
3658                 }
3659                 if (!aux_bap || rsp.status & 0xff00) {
3660                         ai->bap_read = fast_bap_read;
3661                         printk(KERN_DEBUG "airo: Doing fast bap_reads\n");
3662                 } else {
3663                         ai->bap_read = aux_bap_read;
3664                         printk(KERN_DEBUG "airo: Doing AUX bap_reads\n");
3665                 }
3666         }
3667         if (lock)
3668                 up(&ai->sem);
3669         if (ai->config.len == 0) {
3670                 tdsRssiRid rssi_rid;
3671                 CapabilityRid cap_rid;
3672
3673                 if (ai->APList) {
3674                         kfree(ai->APList);
3675                         ai->APList = NULL;
3676                 }
3677                 if (ai->SSID) {
3678                         kfree(ai->SSID);
3679                         ai->SSID = NULL;
3680                 }
3681                 // general configuration (read/modify/write)
3682                 status = readConfigRid(ai, lock);
3683                 if ( status != SUCCESS ) return ERROR;
3684
3685                 status = readCapabilityRid(ai, &cap_rid, lock);
3686                 if ( status != SUCCESS ) return ERROR;
3687
3688                 status = PC4500_readrid(ai,RID_RSSI,&rssi_rid,sizeof(rssi_rid),lock);
3689                 if ( status == SUCCESS ) {
3690                         if (ai->rssi || (ai->rssi = kmalloc(512, GFP_KERNEL)) != NULL)
3691                                 memcpy(ai->rssi, (u8*)&rssi_rid + 2, 512); /* Skip RID length member */
3692                 }
3693                 else {
3694                         if (ai->rssi) {
3695                                 kfree(ai->rssi);
3696                                 ai->rssi = NULL;
3697                         }
3698                         if (cap_rid.softCap & 8)
3699                                 ai->config.rmode |= RXMODE_NORMALIZED_RSSI;
3700                         else
3701                                 printk(KERN_WARNING "airo: unknown received signal level scale\n");
3702                 }
3703                 ai->config.opmode = adhoc ? MODE_STA_IBSS : MODE_STA_ESS;
3704                 ai->config.authType = AUTH_OPEN;
3705                 ai->config.modulation = MOD_CCK;
3706
3707 #ifdef MICSUPPORT
3708                 if ((cap_rid.len>=sizeof(cap_rid)) && (cap_rid.extSoftCap&1) &&
3709                     (micsetup(ai) == SUCCESS)) {
3710                         ai->config.opmode |= MODE_MIC;
3711                         set_bit(FLAG_MIC_CAPABLE, &ai->flags);
3712                 }
3713 #endif
3714
3715                 /* Save off the MAC */
3716                 for( i = 0; i < ETH_ALEN; i++ ) {
3717                         mac[i] = ai->config.macAddr[i];
3718                 }
3719
3720                 /* Check to see if there are any insmod configured
3721                    rates to add */
3722                 if ( rates[0] ) {
3723                         int i = 0;
3724                         memset(ai->config.rates,0,sizeof(ai->config.rates));
3725                         for( i = 0; i < 8 && rates[i]; i++ ) {
3726                                 ai->config.rates[i] = rates[i];
3727                         }
3728                 }
3729                 if ( basic_rate > 0 ) {
3730                         int i;
3731                         for( i = 0; i < 8; i++ ) {
3732                                 if ( ai->config.rates[i] == basic_rate ||
3733                                      !ai->config.rates ) {
3734                                         ai->config.rates[i] = basic_rate | 0x80;
3735                                         break;
3736                                 }
3737                         }
3738                 }
3739                 set_bit (FLAG_COMMIT, &ai->flags);
3740         }
3741
3742         /* Setup the SSIDs if present */
3743         if ( ssids[0] ) {
3744                 int i;
3745                 for( i = 0; i < 3 && ssids[i]; i++ ) {
3746                         mySsid.ssids[i].len = strlen(ssids[i]);
3747                         if ( mySsid.ssids[i].len > 32 )
3748                                 mySsid.ssids[i].len = 32;
3749                         memcpy(mySsid.ssids[i].ssid, ssids[i],
3750                                mySsid.ssids[i].len);
3751                 }
3752                 mySsid.len = sizeof(mySsid);
3753         }
3754
3755         status = writeConfigRid(ai, lock);
3756         if ( status != SUCCESS ) return ERROR;
3757
3758         /* Set up the SSID list */
3759         if ( ssids[0] ) {
3760                 status = writeSsidRid(ai, &mySsid, lock);
3761                 if ( status != SUCCESS ) return ERROR;
3762         }
3763
3764         status = enable_MAC(ai, &rsp, lock);
3765         if ( status != SUCCESS || (rsp.status & 0xFF00) != 0) {
3766                 printk( KERN_ERR "airo: Bad MAC enable reason = %x, rid = %x, offset = %d\n", rsp.rsp0, rsp.rsp1, rsp.rsp2 );
3767                 return ERROR;
3768         }
3769
3770         /* Grab the initial wep key, we gotta save it for auto_wep */
3771         rc = readWepKeyRid(ai, &wkr, 1, lock);
3772         if (rc == SUCCESS) do {
3773                 lastindex = wkr.kindex;
3774                 if (wkr.kindex == 0xffff) {
3775                         ai->defindex = wkr.mac[0];
3776                 }
3777                 rc = readWepKeyRid(ai, &wkr, 0, lock);
3778         } while(lastindex != wkr.kindex);
3779
3780         if (auto_wep) {
3781                 ai->expires = RUN_AT(3*HZ);
3782                 wake_up_interruptible(&ai->thr_wait);
3783         }
3784
3785         return SUCCESS;
3786 }
3787
3788 static u16 issuecommand(struct airo_info *ai, Cmd *pCmd, Resp *pRsp) {
3789         // Im really paranoid about letting it run forever!
3790         int max_tries = 600000;
3791
3792         if (IN4500(ai, EVSTAT) & EV_CMD)
3793                 OUT4500(ai, EVACK, EV_CMD);
3794
3795         OUT4500(ai, PARAM0, pCmd->parm0);
3796         OUT4500(ai, PARAM1, pCmd->parm1);
3797         OUT4500(ai, PARAM2, pCmd->parm2);
3798         OUT4500(ai, COMMAND, pCmd->cmd);
3799
3800         while (max_tries-- && (IN4500(ai, EVSTAT) & EV_CMD) == 0) {
3801                 if ((IN4500(ai, COMMAND)) == pCmd->cmd)
3802                         // PC4500 didn't notice command, try again
3803                         OUT4500(ai, COMMAND, pCmd->cmd);
3804                 if (!in_atomic() && (max_tries & 255) == 0)
3805                         schedule();
3806         }
3807
3808         if ( max_tries == -1 ) {
3809                 printk( KERN_ERR
3810                         "airo: Max tries exceeded when issueing command\n" );
3811                 if (IN4500(ai, COMMAND) & COMMAND_BUSY)
3812                         OUT4500(ai, EVACK, EV_CLEARCOMMANDBUSY);
3813                 return ERROR;
3814         }
3815
3816         // command completed
3817         pRsp->status = IN4500(ai, STATUS);
3818         pRsp->rsp0 = IN4500(ai, RESP0);
3819         pRsp->rsp1 = IN4500(ai, RESP1);
3820         pRsp->rsp2 = IN4500(ai, RESP2);
3821         if ((pRsp->status & 0xff00)!=0 && pCmd->cmd != CMD_SOFTRESET) {
3822                 printk (KERN_ERR "airo: cmd= %x\n", pCmd->cmd);
3823                 printk (KERN_ERR "airo: status= %x\n", pRsp->status);
3824                 printk (KERN_ERR "airo: Rsp0= %x\n", pRsp->rsp0);
3825                 printk (KERN_ERR "airo: Rsp1= %x\n", pRsp->rsp1);
3826                 printk (KERN_ERR "airo: Rsp2= %x\n", pRsp->rsp2);
3827         }
3828
3829         // clear stuck command busy if necessary
3830         if (IN4500(ai, COMMAND) & COMMAND_BUSY) {
3831                 OUT4500(ai, EVACK, EV_CLEARCOMMANDBUSY);
3832         }
3833         // acknowledge processing the status/response
3834         OUT4500(ai, EVACK, EV_CMD);
3835
3836         return SUCCESS;
3837 }
3838
3839 /* Sets up the bap to start exchange data.  whichbap should
3840  * be one of the BAP0 or BAP1 defines.  Locks should be held before
3841  * calling! */
3842 static int bap_setup(struct airo_info *ai, u16 rid, u16 offset, int whichbap )
3843 {
3844         int timeout = 50;
3845         int max_tries = 3;
3846
3847         OUT4500(ai, SELECT0+whichbap, rid);
3848         OUT4500(ai, OFFSET0+whichbap, offset);
3849         while (1) {
3850                 int status = IN4500(ai, OFFSET0+whichbap);
3851                 if (status & BAP_BUSY) {
3852                         /* This isn't really a timeout, but its kinda
3853                            close */
3854                         if (timeout--) {
3855                                 continue;
3856                         }
3857                 } else if ( status & BAP_ERR ) {
3858                         /* invalid rid or offset */
3859                         printk( KERN_ERR "airo: BAP error %x %d\n",
3860                                 status, whichbap );
3861                         return ERROR;
3862                 } else if (status & BAP_DONE) { // success
3863                         return SUCCESS;
3864                 }
3865                 if ( !(max_tries--) ) {
3866                         printk( KERN_ERR
3867                                 "airo: BAP setup error too many retries\n" );
3868                         return ERROR;
3869                 }
3870                 // -- PC4500 missed it, try again
3871                 OUT4500(ai, SELECT0+whichbap, rid);
3872                 OUT4500(ai, OFFSET0+whichbap, offset);
3873                 timeout = 50;
3874         }
3875 }
3876
3877 /* should only be called by aux_bap_read.  This aux function and the
3878    following use concepts not documented in the developers guide.  I
3879    got them from a patch given to my by Aironet */
3880 static u16 aux_setup(struct airo_info *ai, u16 page,
3881                      u16 offset, u16 *len)
3882 {
3883         u16 next;
3884
3885         OUT4500(ai, AUXPAGE, page);
3886         OUT4500(ai, AUXOFF, 0);
3887         next = IN4500(ai, AUXDATA);
3888         *len = IN4500(ai, AUXDATA)&0xff;
3889         if (offset != 4) OUT4500(ai, AUXOFF, offset);
3890         return next;
3891 }
3892
3893 /* requires call to bap_setup() first */
3894 static int aux_bap_read(struct airo_info *ai, u16 *pu16Dst,
3895                         int bytelen, int whichbap)
3896 {
3897         u16 len;
3898         u16 page;
3899         u16 offset;
3900         u16 next;
3901         int words;
3902         int i;
3903         unsigned long flags;
3904
3905         spin_lock_irqsave(&ai->aux_lock, flags);
3906         page = IN4500(ai, SWS0+whichbap);
3907         offset = IN4500(ai, SWS2+whichbap);
3908         next = aux_setup(ai, page, offset, &len);
3909         words = (bytelen+1)>>1;
3910
3911         for (i=0; i<words;) {
3912                 int count;
3913                 count = (len>>1) < (words-i) ? (len>>1) : (words-i);
3914                 if ( !do8bitIO )
3915                         insw( ai->dev->base_addr+DATA0+whichbap,
3916                               pu16Dst+i,count );
3917                 else
3918                         insb( ai->dev->base_addr+DATA0+whichbap,
3919                               pu16Dst+i, count << 1 );
3920                 i += count;
3921                 if (i<words) {
3922                         next = aux_setup(ai, next, 4, &len);
3923                 }
3924         }
3925         spin_unlock_irqrestore(&ai->aux_lock, flags);
3926         return SUCCESS;
3927 }
3928
3929
3930 /* requires call to bap_setup() first */
3931 static int fast_bap_read(struct airo_info *ai, u16 *pu16Dst,
3932                          int bytelen, int whichbap)
3933 {
3934         bytelen = (bytelen + 1) & (~1); // round up to even value
3935         if ( !do8bitIO )
3936                 insw( ai->dev->base_addr+DATA0+whichbap, pu16Dst, bytelen>>1 );
3937         else
3938                 insb( ai->dev->base_addr+DATA0+whichbap, pu16Dst, bytelen );
3939         return SUCCESS;
3940 }
3941
3942 /* requires call to bap_setup() first */
3943 static int bap_write(struct airo_info *ai, const u16 *pu16Src,
3944                      int bytelen, int whichbap)
3945 {
3946         bytelen = (bytelen + 1) & (~1); // round up to even value
3947         if ( !do8bitIO )
3948                 outsw( ai->dev->base_addr+DATA0+whichbap,
3949                        pu16Src, bytelen>>1 );
3950         else
3951                 outsb( ai->dev->base_addr+DATA0+whichbap, pu16Src, bytelen );
3952         return SUCCESS;
3953 }
3954
3955 static int PC4500_accessrid(struct airo_info *ai, u16 rid, u16 accmd)
3956 {
3957         Cmd cmd; /* for issuing commands */
3958         Resp rsp; /* response from commands */
3959         u16 status;
3960
3961         memset(&cmd, 0, sizeof(cmd));
3962         cmd.cmd = accmd;
3963         cmd.parm0 = rid;
3964         status = issuecommand(ai, &cmd, &rsp);
3965         if (status != 0) return status;
3966         if ( (rsp.status & 0x7F00) != 0) {
3967                 return (accmd << 8) + (rsp.rsp0 & 0xFF);
3968         }
3969         return 0;
3970 }
3971
3972 /*  Note, that we are using BAP1 which is also used by transmit, so
3973  *  we must get a lock. */
3974 static int PC4500_readrid(struct airo_info *ai, u16 rid, void *pBuf, int len, int lock)
3975 {
3976         u16 status;
3977         int rc = SUCCESS;
3978
3979         if (lock) {
3980                 if (down_interruptible(&ai->sem))
3981                         return ERROR;
3982         }
3983         if (test_bit(FLAG_MPI,&ai->flags)) {
3984                 Cmd cmd;
3985                 Resp rsp;
3986
3987                 memset(&cmd, 0, sizeof(cmd));
3988                 memset(&rsp, 0, sizeof(rsp));
3989                 ai->config_desc.rid_desc.valid = 1;
3990                 ai->config_desc.rid_desc.len = RIDSIZE;
3991                 ai->config_desc.rid_desc.rid = 0;
3992                 ai->config_desc.rid_desc.host_addr = ai->ridbus;
3993
3994                 cmd.cmd = CMD_ACCESS;
3995                 cmd.parm0 = rid;
3996
3997                 memcpy_toio(ai->config_desc.card_ram_off,
3998                         &ai->config_desc.rid_desc, sizeof(Rid));
3999
4000                 rc = issuecommand(ai, &cmd, &rsp);
4001
4002                 if (rsp.status & 0x7f00)
4003                         rc = rsp.rsp0;
4004                 if (!rc)
4005                         memcpy(pBuf, ai->config_desc.virtual_host_addr, len);
4006                 goto done;
4007         } else {
4008                 if ((status = PC4500_accessrid(ai, rid, CMD_ACCESS))!=SUCCESS) {
4009                         rc = status;
4010                         goto done;
4011                 }
4012                 if (bap_setup(ai, rid, 0, BAP1) != SUCCESS) {
4013                         rc = ERROR;
4014                         goto done;
4015                 }
4016                 // read the rid length field
4017                 bap_read(ai, pBuf, 2, BAP1);
4018                 // length for remaining part of rid
4019                 len = min(len, (int)le16_to_cpu(*(u16*)pBuf)) - 2;
4020
4021                 if ( len <= 2 ) {
4022                         printk( KERN_ERR
4023                         "airo: Rid %x has a length of %d which is too short\n",
4024                                 (int)rid, (int)len );
4025                         rc = ERROR;
4026                         goto done;
4027                 }
4028                 // read remainder of the rid
4029                 rc = bap_read(ai, ((u16*)pBuf)+1, len, BAP1);
4030         }
4031 done:
4032         if (lock)
4033                 up(&ai->sem);
4034         return rc;
4035 }
4036
4037 /*  Note, that we are using BAP1 which is also used by transmit, so
4038  *  make sure this isnt called when a transmit is happening */
4039 static int PC4500_writerid(struct airo_info *ai, u16 rid,
4040                            const void *pBuf, int len, int lock)
4041 {
4042         u16 status;
4043         int rc = SUCCESS;
4044
4045         *(u16*)pBuf = cpu_to_le16((u16)len);
4046
4047         if (lock) {
4048                 if (down_interruptible(&ai->sem))
4049                         return ERROR;
4050         }
4051         if (test_bit(FLAG_MPI,&ai->flags)) {
4052                 Cmd cmd;
4053                 Resp rsp;
4054
4055                 if (test_bit(FLAG_ENABLED, &ai->flags))
4056                         printk(KERN_ERR
4057                                 "%s: MAC should be disabled (rid=%04x)\n",
4058                                 __FUNCTION__, rid);
4059                 memset(&cmd, 0, sizeof(cmd));
4060                 memset(&rsp, 0, sizeof(rsp));
4061
4062                 ai->config_desc.rid_desc.valid = 1;
4063                 ai->config_desc.rid_desc.len = *((u16 *)pBuf);
4064                 ai->config_desc.rid_desc.rid = 0;
4065
4066                 cmd.cmd = CMD_WRITERID;
4067                 cmd.parm0 = rid;
4068
4069                 memcpy_toio(ai->config_desc.card_ram_off,
4070                         &ai->config_desc.rid_desc, sizeof(Rid));
4071
4072                 if (len < 4 || len > 2047) {
4073                         printk(KERN_ERR "%s: len=%d\n",__FUNCTION__,len);
4074                         rc = -1;
4075                 } else {
4076                         memcpy((char *)ai->config_desc.virtual_host_addr,
4077                                 pBuf, len);
4078
4079                         rc = issuecommand(ai, &cmd, &rsp);
4080                         if ((rc & 0xff00) != 0) {
4081                                 printk(KERN_ERR "%s: Write rid Error %d\n",
4082                                         __FUNCTION__,rc);
4083                                 printk(KERN_ERR "%s: Cmd=%04x\n",
4084                                                 __FUNCTION__,cmd.cmd);
4085                         }
4086
4087                         if ((rsp.status & 0x7f00))
4088                                 rc = rsp.rsp0;
4089                 }
4090         } else {
4091                 // --- first access so that we can write the rid data
4092                 if ( (status = PC4500_accessrid(ai, rid, CMD_ACCESS)) != 0) {
4093                         rc = status;
4094                         goto done;
4095                 }
4096                 // --- now write the rid data
4097                 if (bap_setup(ai, rid, 0, BAP1) != SUCCESS) {
4098                         rc = ERROR;
4099                         goto done;
4100                 }
4101                 bap_write(ai, pBuf, len, BAP1);
4102                 // ---now commit the rid data
4103                 rc = PC4500_accessrid(ai, rid, 0x100|CMD_ACCESS);
4104         }
4105 done:
4106         if (lock)
4107                 up(&ai->sem);
4108         return rc;
4109 }
4110
4111 /* Allocates a FID to be used for transmitting packets.  We only use
4112    one for now. */
4113 static u16 transmit_allocate(struct airo_info *ai, int lenPayload, int raw)
4114 {
4115         unsigned int loop = 3000;
4116         Cmd cmd;
4117         Resp rsp;
4118         u16 txFid;
4119         u16 txControl;
4120
4121         cmd.cmd = CMD_ALLOCATETX;
4122         cmd.parm0 = lenPayload;
4123         if (down_interruptible(&ai->sem))
4124                 return ERROR;
4125         if (issuecommand(ai, &cmd, &rsp) != SUCCESS) {
4126                 txFid = ERROR;
4127                 goto done;
4128         }
4129         if ( (rsp.status & 0xFF00) != 0) {
4130                 txFid = ERROR;
4131                 goto done;
4132         }
4133         /* wait for the allocate event/indication
4134          * It makes me kind of nervous that this can just sit here and spin,
4135          * but in practice it only loops like four times. */
4136         while (((IN4500(ai, EVSTAT) & EV_ALLOC) == 0) && --loop);
4137         if (!loop) {
4138                 txFid = ERROR;
4139                 goto done;
4140         }
4141
4142         // get the allocated fid and acknowledge
4143         txFid = IN4500(ai, TXALLOCFID);
4144         OUT4500(ai, EVACK, EV_ALLOC);
4145
4146         /*  The CARD is pretty cool since it converts the ethernet packet
4147          *  into 802.11.  Also note that we don't release the FID since we
4148          *  will be using the same one over and over again. */
4149         /*  We only have to setup the control once since we are not
4150          *  releasing the fid. */
4151         if (raw)
4152                 txControl = cpu_to_le16(TXCTL_TXOK | TXCTL_TXEX | TXCTL_802_11
4153                         | TXCTL_ETHERNET | TXCTL_NORELEASE);
4154         else
4155                 txControl = cpu_to_le16(TXCTL_TXOK | TXCTL_TXEX | TXCTL_802_3
4156                         | TXCTL_ETHERNET | TXCTL_NORELEASE);
4157         if (bap_setup(ai, txFid, 0x0008, BAP1) != SUCCESS)
4158                 txFid = ERROR;
4159         else
4160                 bap_write(ai, &txControl, sizeof(txControl), BAP1);
4161
4162 done:
4163         up(&ai->sem);
4164
4165         return txFid;
4166 }
4167
4168 /* In general BAP1 is dedicated to transmiting packets.  However,
4169    since we need a BAP when accessing RIDs, we also use BAP1 for that.
4170    Make sure the BAP1 spinlock is held when this is called. */
4171 static int transmit_802_3_packet(struct airo_info *ai, int len, char *pPacket)
4172 {
4173         u16 payloadLen;
4174         Cmd cmd;
4175         Resp rsp;
4176         int miclen = 0;
4177         u16 txFid = len;
4178         MICBuffer pMic;
4179
4180         len >>= 16;
4181
4182         if (len <= ETH_ALEN * 2) {
4183                 printk( KERN_WARNING "Short packet %d\n", len );
4184                 return ERROR;
4185         }
4186         len -= ETH_ALEN * 2;
4187
4188 #ifdef MICSUPPORT
4189         if (test_bit(FLAG_MIC_CAPABLE, &ai->flags) && ai->micstats.enabled && 
4190             (ntohs(((u16 *)pPacket)[6]) != 0x888E)) {
4191                 if (encapsulate(ai,(etherHead *)pPacket,&pMic,len) != SUCCESS)
4192                         return ERROR;
4193                 miclen = sizeof(pMic);
4194         }
4195 #endif
4196
4197         // packet is destination[6], source[6], payload[len-12]
4198         // write the payload length and dst/src/payload
4199         if (bap_setup(ai, txFid, 0x0036, BAP1) != SUCCESS) return ERROR;
4200         /* The hardware addresses aren't counted as part of the payload, so
4201          * we have to subtract the 12 bytes for the addresses off */
4202         payloadLen = cpu_to_le16(len + miclen);
4203         bap_write(ai, &payloadLen, sizeof(payloadLen),BAP1);
4204         bap_write(ai, (const u16*)pPacket, sizeof(etherHead), BAP1);
4205         if (miclen)
4206                 bap_write(ai, (const u16*)&pMic, miclen, BAP1);
4207         bap_write(ai, (const u16*)(pPacket + sizeof(etherHead)), len, BAP1);
4208         // issue the transmit command
4209         memset( &cmd, 0, sizeof( cmd ) );
4210         cmd.cmd = CMD_TRANSMIT;
4211         cmd.parm0 = txFid;
4212         if (issuecommand(ai, &cmd, &rsp) != SUCCESS) return ERROR;
4213         if ( (rsp.status & 0xFF00) != 0) return ERROR;
4214         return SUCCESS;
4215 }
4216
4217 static int transmit_802_11_packet(struct airo_info *ai, int len, char *pPacket)
4218 {
4219         u16 fc, payloadLen;
4220         Cmd cmd;
4221         Resp rsp;
4222         int hdrlen;
4223         struct {
4224                 u8 addr4[ETH_ALEN];
4225                 u16 gaplen;
4226                 u8 gap[6];
4227         } gap;
4228         u16 txFid = len;
4229         len >>= 16;
4230         gap.gaplen = 6;
4231
4232         fc = le16_to_cpu(*(const u16*)pPacket);
4233         switch (fc & 0xc) {
4234                 case 4:
4235                         if ((fc & 0xe0) == 0xc0)
4236                                 hdrlen = 10;
4237                         else
4238                                 hdrlen = 16;
4239                         break;
4240                 case 8:
4241                         if ((fc&0x300)==0x300){
4242                                 hdrlen = 30;
4243                                 break;
4244                         }
4245                 default:
4246                         hdrlen = 24;
4247         }
4248
4249         if (len < hdrlen) {
4250                 printk( KERN_WARNING "Short packet %d\n", len );
4251                 return ERROR;
4252         }
4253
4254         /* packet is 802.11 header +  payload
4255          * write the payload length and dst/src/payload */
4256         if (bap_setup(ai, txFid, 6, BAP1) != SUCCESS) return ERROR;
4257         /* The 802.11 header aren't counted as part of the payload, so
4258          * we have to subtract the header bytes off */
4259         payloadLen = cpu_to_le16(len-hdrlen);
4260         bap_write(ai, &payloadLen, sizeof(payloadLen),BAP1);
4261         if (bap_setup(ai, txFid, 0x0014, BAP1) != SUCCESS) return ERROR;
4262         bap_write(ai, (const u16*)pPacket, hdrlen, BAP1);
4263         bap_write(ai, hdrlen == 30 ?
4264                 (const u16*)&gap.gaplen : (const u16*)&gap, 38 - hdrlen, BAP1);
4265
4266         bap_write(ai, (const u16*)(pPacket + hdrlen), len - hdrlen, BAP1);
4267         // issue the transmit command
4268         memset( &cmd, 0, sizeof( cmd ) );
4269         cmd.cmd = CMD_TRANSMIT;
4270         cmd.parm0 = txFid;
4271         if (issuecommand(ai, &cmd, &rsp) != SUCCESS) return ERROR;
4272         if ( (rsp.status & 0xFF00) != 0) return ERROR;
4273         return SUCCESS;
4274 }
4275
4276 /*
4277  *  This is the proc_fs routines.  It is a bit messier than I would
4278  *  like!  Feel free to clean it up!
4279  */
4280
4281 static ssize_t proc_read( struct file *file,
4282                           char __user *buffer,
4283                           size_t len,
4284                           loff_t *offset);
4285
4286 static ssize_t proc_write( struct file *file,
4287                            const char __user *buffer,
4288                            size_t len,
4289                            loff_t *offset );
4290 static int proc_close( struct inode *inode, struct file *file );
4291
4292 static int proc_stats_open( struct inode *inode, struct file *file );
4293 static int proc_statsdelta_open( struct inode *inode, struct file *file );
4294 static int proc_status_open( struct inode *inode, struct file *file );
4295 static int proc_SSID_open( struct inode *inode, struct file *file );
4296 static int proc_APList_open( struct inode *inode, struct file *file );
4297 static int proc_BSSList_open( struct inode *inode, struct file *file );
4298 static int proc_config_open( struct inode *inode, struct file *file );
4299 static int proc_wepkey_open( struct inode *inode, struct file *file );
4300
4301 static struct file_operations proc_statsdelta_ops = {
4302         .read           = proc_read,
4303         .open           = proc_statsdelta_open,
4304         .release        = proc_close
4305 };
4306
4307 static struct file_operations proc_stats_ops = {
4308         .read           = proc_read,
4309         .open           = proc_stats_open,
4310         .release        = proc_close
4311 };
4312
4313 static struct file_operations proc_status_ops = {
4314         .read           = proc_read,
4315         .open           = proc_status_open,
4316         .release        = proc_close
4317 };
4318
4319 static struct file_operations proc_SSID_ops = {
4320         .read           = proc_read,
4321         .write          = proc_write,
4322         .open           = proc_SSID_open,
4323         .release        = proc_close
4324 };
4325
4326 static struct file_operations proc_BSSList_ops = {
4327         .read           = proc_read,
4328         .write          = proc_write,
4329         .open           = proc_BSSList_open,
4330         .release        = proc_close
4331 };
4332
4333 static struct file_operations proc_APList_ops = {
4334         .read           = proc_read,
4335         .write          = proc_write,
4336         .open           = proc_APList_open,
4337         .release        = proc_close
4338 };
4339
4340 static struct file_operations proc_config_ops = {
4341         .read           = proc_read,
4342         .write          = proc_write,
4343         .open           = proc_config_open,
4344         .release        = proc_close
4345 };
4346
4347 static struct file_operations proc_wepkey_ops = {
4348         .read           = proc_read,
4349         .write          = proc_write,
4350         .open           = proc_wepkey_open,
4351         .release        = proc_close
4352 };
4353
4354 static struct proc_dir_entry *airo_entry;
4355
4356 struct proc_data {
4357         int release_buffer;
4358         int readlen;
4359         char *rbuffer;
4360         int writelen;
4361         int maxwritelen;
4362         char *wbuffer;
4363         void (*on_close) (struct inode *, struct file *);
4364 };
4365
4366 #ifndef SETPROC_OPS
4367 #define SETPROC_OPS(entry, ops) (entry)->proc_fops = &(ops)
4368 #endif
4369
4370 static int setup_proc_entry( struct net_device *dev,
4371                              struct airo_info *apriv ) {
4372         struct proc_dir_entry *entry;
4373         /* First setup the device directory */
4374         strcpy(apriv->proc_name,dev->name);
4375         apriv->proc_entry = create_proc_entry(apriv->proc_name,
4376                                               S_IFDIR|airo_perm,
4377                                               airo_entry);
4378         apriv->proc_entry->uid = proc_uid;
4379         apriv->proc_entry->gid = proc_gid;
4380         apriv->proc_entry->owner = THIS_MODULE;
4381
4382         /* Setup the StatsDelta */
4383         entry = create_proc_entry("StatsDelta",
4384                                   S_IFREG | (S_IRUGO&proc_perm),
4385                                   apriv->proc_entry);
4386         entry->uid = proc_uid;
4387         entry->gid = proc_gid;
4388         entry->data = dev;
4389         entry->owner = THIS_MODULE;
4390         SETPROC_OPS(entry, proc_statsdelta_ops);
4391
4392         /* Setup the Stats */
4393         entry = create_proc_entry("Stats",
4394                                   S_IFREG | (S_IRUGO&proc_perm),
4395                                   apriv->proc_entry);
4396         entry->uid = proc_uid;
4397         entry->gid = proc_gid;
4398         entry->data = dev;
4399         entry->owner = THIS_MODULE;
4400         SETPROC_OPS(entry, proc_stats_ops);
4401
4402         /* Setup the Status */
4403         entry = create_proc_entry("Status",
4404                                   S_IFREG | (S_IRUGO&proc_perm),
4405                                   apriv->proc_entry);
4406         entry->uid = proc_uid;
4407         entry->gid = proc_gid;
4408         entry->data = dev;
4409         entry->owner = THIS_MODULE;
4410         SETPROC_OPS(entry, proc_status_ops);
4411
4412         /* Setup the Config */
4413         entry = create_proc_entry("Config",
4414                                   S_IFREG | proc_perm,
4415                                   apriv->proc_entry);
4416         entry->uid = proc_uid;
4417         entry->gid = proc_gid;
4418         entry->data = dev;
4419         entry->owner = THIS_MODULE;
4420         SETPROC_OPS(entry, proc_config_ops);
4421
4422         /* Setup the SSID */
4423         entry = create_proc_entry("SSID",
4424                                   S_IFREG | proc_perm,
4425                                   apriv->proc_entry);
4426         entry->uid = proc_uid;
4427         entry->gid = proc_gid;
4428         entry->data = dev;
4429         entry->owner = THIS_MODULE;
4430         SETPROC_OPS(entry, proc_SSID_ops);
4431
4432         /* Setup the APList */
4433         entry = create_proc_entry("APList",
4434                                   S_IFREG | proc_perm,
4435                                   apriv->proc_entry);
4436         entry->uid = proc_uid;
4437         entry->gid = proc_gid;
4438         entry->data = dev;
4439         entry->owner = THIS_MODULE;
4440         SETPROC_OPS(entry, proc_APList_ops);
4441
4442         /* Setup the BSSList */
4443         entry = create_proc_entry("BSSList",
4444                                   S_IFREG | proc_perm,
4445                                   apriv->proc_entry);
4446         entry->uid = proc_uid;
4447         entry->gid = proc_gid;
4448         entry->data = dev;
4449         entry->owner = THIS_MODULE;
4450         SETPROC_OPS(entry, proc_BSSList_ops);
4451
4452         /* Setup the WepKey */
4453         entry = create_proc_entry("WepKey",
4454                                   S_IFREG | proc_perm,
4455                                   apriv->proc_entry);
4456         entry->uid = proc_uid;
4457         entry->gid = proc_gid;
4458         entry->data = dev;
4459         entry->owner = THIS_MODULE;
4460         SETPROC_OPS(entry, proc_wepkey_ops);
4461
4462         return 0;
4463 }
4464
4465 static int takedown_proc_entry( struct net_device *dev,
4466                                 struct airo_info *apriv ) {
4467         if ( !apriv->proc_entry->namelen ) return 0;
4468         remove_proc_entry("Stats",apriv->proc_entry);
4469         remove_proc_entry("StatsDelta",apriv->proc_entry);
4470         remove_proc_entry("Status",apriv->proc_entry);
4471         remove_proc_entry("Config",apriv->proc_entry);
4472         remove_proc_entry("SSID",apriv->proc_entry);
4473         remove_proc_entry("APList",apriv->proc_entry);
4474         remove_proc_entry("BSSList",apriv->proc_entry);
4475         remove_proc_entry("WepKey",apriv->proc_entry);
4476         remove_proc_entry(apriv->proc_name,airo_entry);
4477         return 0;
4478 }
4479
4480 /*
4481  *  What we want from the proc_fs is to be able to efficiently read
4482  *  and write the configuration.  To do this, we want to read the
4483  *  configuration when the file is opened and write it when the file is
4484  *  closed.  So basically we allocate a read buffer at open and fill it
4485  *  with data, and allocate a write buffer and read it at close.
4486  */
4487
4488 /*
4489  *  The read routine is generic, it relies on the preallocated rbuffer
4490  *  to supply the data.
4491  */
4492 static ssize_t proc_read( struct file *file,
4493                           char __user *buffer,
4494                           size_t len,
4495                           loff_t *offset )
4496 {
4497         loff_t pos = *offset;
4498         struct proc_data *priv = (struct proc_data*)file->private_data;
4499
4500         if (!priv->rbuffer)
4501                 return -EINVAL;
4502
4503         if (pos < 0)
4504                 return -EINVAL;
4505         if (pos >= priv->readlen)
4506                 return 0;
4507         if (len > priv->readlen - pos)
4508                 len = priv->readlen - pos;
4509         if (copy_to_user(buffer, priv->rbuffer + pos, len))
4510                 return -EFAULT;
4511         *offset = pos + len;
4512         return len;
4513 }
4514
4515 /*
4516  *  The write routine is generic, it fills in a preallocated rbuffer
4517  *  to supply the data.
4518  */
4519 static ssize_t proc_write( struct file *file,
4520                            const char __user *buffer,
4521                            size_t len,
4522                            loff_t *offset )
4523 {
4524         loff_t pos = *offset;
4525         struct proc_data *priv = (struct proc_data*)file->private_data;
4526
4527         if (!priv->wbuffer)
4528                 return -EINVAL;
4529
4530         if (pos < 0)
4531                 return -EINVAL;
4532         if (pos >= priv->maxwritelen)
4533                 return 0;
4534         if (len > priv->maxwritelen - pos)
4535                 len = priv->maxwritelen - pos;
4536         if (copy_from_user(priv->wbuffer + pos, buffer, len))
4537                 return -EFAULT;
4538         if ( pos + len > priv->writelen )
4539                 priv->writelen = len + file->f_pos;
4540         *offset = pos + len;
4541         return len;
4542 }
4543
4544 static int proc_status_open( struct inode *inode, struct file *file ) {
4545         struct proc_data *data;
4546         struct proc_dir_entry *dp = PDE(inode);
4547         struct net_device *dev = dp->data;
4548         struct airo_info *apriv = dev->priv;
4549         CapabilityRid cap_rid;
4550         StatusRid status_rid;
4551         int i;
4552
4553         if ((file->private_data = kmalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
4554                 return -ENOMEM;
4555         memset(file->private_data, 0, sizeof(struct proc_data));
4556         data = (struct proc_data *)file->private_data;
4557         if ((data->rbuffer = kmalloc( 2048, GFP_KERNEL )) == NULL) {
4558                 kfree (file->private_data);
4559                 return -ENOMEM;
4560         }
4561
4562         readStatusRid(apriv, &status_rid, 1);
4563         readCapabilityRid(apriv, &cap_rid, 1);
4564
4565         i = sprintf(data->rbuffer, "Status: %s%s%s%s%s%s%s%s%s\n",
4566                     status_rid.mode & 1 ? "CFG ": "",
4567                     status_rid.mode & 2 ? "ACT ": "",
4568                     status_rid.mode & 0x10 ? "SYN ": "",
4569                     status_rid.mode & 0x20 ? "LNK ": "",
4570                     status_rid.mode & 0x40 ? "LEAP ": "",
4571                     status_rid.mode & 0x80 ? "PRIV ": "",
4572                     status_rid.mode & 0x100 ? "KEY ": "",
4573                     status_rid.mode & 0x200 ? "WEP ": "",
4574                     status_rid.mode & 0x8000 ? "ERR ": "");
4575         sprintf( data->rbuffer+i, "Mode: %x\n"
4576                  "Signal Strength: %d\n"
4577                  "Signal Quality: %d\n"
4578                  "SSID: %-.*s\n"
4579                  "AP: %-.16s\n"
4580                  "Freq: %d\n"
4581                  "BitRate: %dmbs\n"
4582                  "Driver Version: %s\n"
4583                  "Device: %s\nManufacturer: %s\nFirmware Version: %s\n"
4584                  "Radio type: %x\nCountry: %x\nHardware Version: %x\n"
4585                  "Software Version: %x\nSoftware Subversion: %x\n"
4586                  "Boot block version: %x\n",
4587                  (int)status_rid.mode,
4588                  (int)status_rid.normalizedSignalStrength,
4589                  (int)status_rid.signalQuality,
4590                  (int)status_rid.SSIDlen,
4591                  status_rid.SSID,
4592                  status_rid.apName,
4593                  (int)status_rid.channel,
4594                  (int)status_rid.currentXmitRate/2,
4595                  version,
4596                  cap_rid.prodName,
4597                  cap_rid.manName,
4598                  cap_rid.prodVer,
4599                  cap_rid.radioType,
4600                  cap_rid.country,
4601                  cap_rid.hardVer,
4602                  (int)cap_rid.softVer,
4603                  (int)cap_rid.softSubVer,
4604                  (int)cap_rid.bootBlockVer );
4605         data->readlen = strlen( data->rbuffer );
4606         return 0;
4607 }
4608
4609 static int proc_stats_rid_open(struct inode*, struct file*, u16);
4610 static int proc_statsdelta_open( struct inode *inode,
4611                                  struct file *file ) {
4612         if (file->f_mode&FMODE_WRITE) {
4613                 return proc_stats_rid_open(inode, file, RID_STATSDELTACLEAR);
4614         }
4615         return proc_stats_rid_open(inode, file, RID_STATSDELTA);
4616 }
4617
4618 static int proc_stats_open( struct inode *inode, struct file *file ) {
4619         return proc_stats_rid_open(inode, file, RID_STATS);
4620 }
4621
4622 static int proc_stats_rid_open( struct inode *inode,
4623                                 struct file *file,
4624                                 u16 rid ) {
4625         struct proc_data *data;
4626         struct proc_dir_entry *dp = PDE(inode);
4627         struct net_device *dev = dp->data;
4628         struct airo_info *apriv = dev->priv;
4629         StatsRid stats;
4630         int i, j;
4631         u32 *vals = stats.vals;
4632
4633         if ((file->private_data = kmalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
4634                 return -ENOMEM;
4635         memset(file->private_data, 0, sizeof(struct proc_data));
4636         data = (struct proc_data *)file->private_data;
4637         if ((data->rbuffer = kmalloc( 4096, GFP_KERNEL )) == NULL) {
4638                 kfree (file->private_data);
4639                 return -ENOMEM;
4640         }
4641
4642         readStatsRid(apriv, &stats, rid, 1);
4643
4644         j = 0;
4645         for(i=0; statsLabels[i]!=(char *)-1 &&
4646                     i*4<stats.len; i++){
4647                 if (!statsLabels[i]) continue;
4648                 if (j+strlen(statsLabels[i])+16>4096) {
4649                         printk(KERN_WARNING
4650                                "airo: Potentially disasterous buffer overflow averted!\n");
4651                         break;
4652                 }
4653                 j+=sprintf(data->rbuffer+j, "%s: %u\n", statsLabels[i], vals[i]);
4654         }
4655         if (i*4>=stats.len){
4656                 printk(KERN_WARNING
4657                        "airo: Got a short rid\n");
4658         }
4659         data->readlen = j;
4660         return 0;
4661 }
4662
4663 static int get_dec_u16( char *buffer, int *start, int limit ) {
4664         u16 value;
4665         int valid = 0;
4666         for( value = 0; buffer[*start] >= '0' &&
4667                      buffer[*start] <= '9' &&
4668                      *start < limit; (*start)++ ) {
4669                 valid = 1;
4670                 value *= 10;
4671                 value += buffer[*start] - '0';
4672         }
4673         if ( !valid ) return -1;
4674         return value;
4675 }
4676
4677 static int airo_config_commit(struct net_device *dev,
4678                               struct iw_request_info *info, void *zwrq,
4679                               char *extra);
4680
4681 static void proc_config_on_close( struct inode *inode, struct file *file ) {
4682         struct proc_data *data = file->private_data;
4683         struct proc_dir_entry *dp = PDE(inode);
4684         struct net_device *dev = dp->data;
4685         struct airo_info *ai = dev->priv;
4686         char *line;
4687
4688         if ( !data->writelen ) return;
4689
4690         readConfigRid(ai, 1);
4691         set_bit (FLAG_COMMIT, &ai->flags);
4692
4693         line = data->wbuffer;
4694         while( line[0] ) {
4695 /*** Mode processing */
4696                 if ( !strncmp( line, "Mode: ", 6 ) ) {
4697                         line += 6;
4698                         if ((ai->config.rmode & 0xff) >= RXMODE_RFMON)
4699                                         set_bit (FLAG_RESET, &ai->flags);
4700                         ai->config.rmode &= 0xfe00;
4701                         clear_bit (FLAG_802_11, &ai->flags);
4702                         ai->config.opmode &= 0xFF00;
4703                         ai->config.scanMode = SCANMODE_ACTIVE;
4704                         if ( line[0] == 'a' ) {
4705                                 ai->config.opmode |= 0;
4706                         } else {
4707                                 ai->config.opmode |= 1;
4708                                 if ( line[0] == 'r' ) {
4709                                         ai->config.rmode |= RXMODE_RFMON | RXMODE_DISABLE_802_3_HEADER;
4710                                         ai->config.scanMode = SCANMODE_PASSIVE;
4711                                         set_bit (FLAG_802_11, &ai->flags);
4712                                 } else if ( line[0] == 'y' ) {
4713                                         ai->config.rmode |= RXMODE_RFMON_ANYBSS | RXMODE_DISABLE_802_3_HEADER;
4714                                         ai->config.scanMode = SCANMODE_PASSIVE;
4715                                         set_bit (FLAG_802_11, &ai->flags);
4716                                 } else if ( line[0] == 'l' )
4717                                         ai->config.rmode |= RXMODE_LANMON;
4718                         }
4719                         set_bit (FLAG_COMMIT, &ai->flags);
4720                 }
4721
4722 /*** Radio status */
4723                 else if (!strncmp(line,"Radio: ", 7)) {
4724                         line += 7;
4725                         if (!strncmp(line,"off",3)) {
4726                                 set_bit (FLAG_RADIO_OFF, &ai->flags);
4727                         } else {
4728                                 clear_bit (FLAG_RADIO_OFF, &ai->flags);
4729                         }
4730                 }
4731 /*** NodeName processing */
4732                 else if ( !strncmp( line, "NodeName: ", 10 ) ) {
4733                         int j;
4734
4735                         line += 10;
4736                         memset( ai->config.nodeName, 0, 16 );
4737 /* Do the name, assume a space between the mode and node name */
4738                         for( j = 0; j < 16 && line[j] != '\n'; j++ ) {
4739                                 ai->config.nodeName[j] = line[j];
4740                         }
4741                         set_bit (FLAG_COMMIT, &ai->flags);
4742                 }
4743
4744 /*** PowerMode processing */
4745                 else if ( !strncmp( line, "PowerMode: ", 11 ) ) {
4746                         line += 11;
4747                         if ( !strncmp( line, "PSPCAM", 6 ) ) {
4748                                 ai->config.powerSaveMode = POWERSAVE_PSPCAM;
4749                                 set_bit (FLAG_COMMIT, &ai->flags);
4750                         } else if ( !strncmp( line, "PSP", 3 ) ) {
4751                                 ai->config.powerSaveMode = POWERSAVE_PSP;
4752                                 set_bit (FLAG_COMMIT, &ai->flags);
4753                         } else {
4754                                 ai->config.powerSaveMode = POWERSAVE_CAM;
4755                                 set_bit (FLAG_COMMIT, &ai->flags);
4756                         }
4757                 } else if ( !strncmp( line, "DataRates: ", 11 ) ) {
4758                         int v, i = 0, k = 0; /* i is index into line,
4759                                                 k is index to rates */
4760
4761                         line += 11;
4762                         while((v = get_dec_u16(line, &i, 3))!=-1) {
4763                                 ai->config.rates[k++] = (u8)v;
4764                                 line += i + 1;
4765                                 i = 0;
4766                         }
4767                         set_bit (FLAG_COMMIT, &ai->flags);
4768                 } else if ( !strncmp( line, "Channel: ", 9 ) ) {
4769                         int v, i = 0;
4770                         line += 9;
4771                         v = get_dec_u16(line, &i, i+3);
4772                         if ( v != -1 ) {
4773                                 ai->config.channelSet = (u16)v;
4774                                 set_bit (FLAG_COMMIT, &ai->flags);
4775                         }
4776                 } else if ( !strncmp( line, "XmitPower: ", 11 ) ) {
4777                         int v, i = 0;
4778                         line += 11;
4779                         v = get_dec_u16(line, &i, i+3);
4780                         if ( v != -1 ) {
4781                                 ai->config.txPower = (u16)v;
4782                                 set_bit (FLAG_COMMIT, &ai->flags);
4783                         }
4784                 } else if ( !strncmp( line, "WEP: ", 5 ) ) {
4785                         line += 5;
4786                         switch( line[0] ) {
4787                         case 's':
4788                                 ai->config.authType = (u16)AUTH_SHAREDKEY;
4789                                 break;
4790                         case 'e':
4791                                 ai->config.authType = (u16)AUTH_ENCRYPT;
4792                                 break;
4793                         default:
4794                                 ai->config.authType = (u16)AUTH_OPEN;
4795                                 break;
4796                         }
4797                         set_bit (FLAG_COMMIT, &ai->flags);
4798                 } else if ( !strncmp( line, "LongRetryLimit: ", 16 ) ) {
4799                         int v, i = 0;
4800
4801                         line += 16;
4802                         v = get_dec_u16(line, &i, 3);
4803                         v = (v<0) ? 0 : ((v>255) ? 255 : v);
4804                         ai->config.longRetryLimit = (u16)v;
4805                         set_bit (FLAG_COMMIT, &ai->flags);
4806                 } else if ( !strncmp( line, "ShortRetryLimit: ", 17 ) ) {
4807                         int v, i = 0;
4808
4809                         line += 17;
4810                         v = get_dec_u16(line, &i, 3);
4811                         v = (v<0) ? 0 : ((v>255) ? 255 : v);
4812                         ai->config.shortRetryLimit = (u16)v;
4813                         set_bit (FLAG_COMMIT, &ai->flags);
4814                 } else if ( !strncmp( line, "RTSThreshold: ", 14 ) ) {
4815                         int v, i = 0;
4816
4817                         line += 14;
4818                         v = get_dec_u16(line, &i, 4);
4819                         v = (v<0) ? 0 : ((v>2312) ? 2312 : v);
4820                         ai->config.rtsThres = (u16)v;
4821                         set_bit (FLAG_COMMIT, &ai->flags);
4822                 } else if ( !strncmp( line, "TXMSDULifetime: ", 16 ) ) {
4823                         int v, i = 0;
4824
4825                         line += 16;
4826                         v = get_dec_u16(line, &i, 5);
4827                         v = (v<0) ? 0 : v;
4828                         ai->config.txLifetime = (u16)v;
4829                         set_bit (FLAG_COMMIT, &ai->flags);
4830                 } else if ( !strncmp( line, "RXMSDULifetime: ", 16 ) ) {
4831                         int v, i = 0;
4832
4833                         line += 16;
4834                         v = get_dec_u16(line, &i, 5);
4835                         v = (v<0) ? 0 : v;
4836                         ai->config.rxLifetime = (u16)v;
4837                         set_bit (FLAG_COMMIT, &ai->flags);
4838                 } else if ( !strncmp( line, "TXDiversity: ", 13 ) ) {
4839                         ai->config.txDiversity =
4840                                 (line[13]=='l') ? 1 :
4841                                 ((line[13]=='r')? 2: 3);
4842                         set_bit (FLAG_COMMIT, &ai->flags);
4843                 } else if ( !strncmp( line, "RXDiversity: ", 13 ) ) {
4844                         ai->config.rxDiversity =
4845                                 (line[13]=='l') ? 1 :
4846                                 ((line[13]=='r')? 2: 3);
4847                         set_bit (FLAG_COMMIT, &ai->flags);
4848                 } else if ( !strncmp( line, "FragThreshold: ", 15 ) ) {
4849                         int v, i = 0;
4850
4851                         line += 15;
4852                         v = get_dec_u16(line, &i, 4);
4853                         v = (v<256) ? 256 : ((v>2312) ? 2312 : v);
4854                         v = v & 0xfffe; /* Make sure its even */
4855                         ai->config.fragThresh = (u16)v;
4856                         set_bit (FLAG_COMMIT, &ai->flags);
4857                 } else if (!strncmp(line, "Modulation: ", 12)) {
4858                         line += 12;
4859                         switch(*line) {
4860                         case 'd':  ai->config.modulation=MOD_DEFAULT; set_bit(FLAG_COMMIT, &ai->flags); break;
4861                         case 'c':  ai->config.modulation=MOD_CCK; set_bit(FLAG_COMMIT, &ai->flags); break;
4862                         case 'm':  ai->config.modulation=MOD_MOK; set_bit(FLAG_COMMIT, &ai->flags); break;
4863                         default:
4864                                 printk( KERN_WARNING "airo: Unknown modulation\n" );
4865                         }
4866                 } else if (!strncmp(line, "Preamble: ", 10)) {
4867                         line += 10;
4868                         switch(*line) {
4869                         case 'a': ai->config.preamble=PREAMBLE_AUTO; set_bit(FLAG_COMMIT, &ai->flags); break;
4870                         case 'l': ai->config.preamble=PREAMBLE_LONG; set_bit(FLAG_COMMIT, &ai->flags); break;
4871                         case 's': ai->config.preamble=PREAMBLE_SHORT; set_bit(FLAG_COMMIT, &ai->flags); break;
4872                         default: printk(KERN_WARNING "airo: Unknown preamble\n");
4873                         }
4874                 } else {
4875                         printk( KERN_WARNING "Couldn't figure out %s\n", line );
4876                 }
4877                 while( line[0] && line[0] != '\n' ) line++;
4878                 if ( line[0] ) line++;
4879         }
4880         airo_config_commit(dev, NULL, NULL, NULL);
4881 }
4882
4883 static char *get_rmode(u16 mode) {
4884         switch(mode&0xff) {
4885         case RXMODE_RFMON:  return "rfmon";
4886         case RXMODE_RFMON_ANYBSS:  return "yna (any) bss rfmon";
4887         case RXMODE_LANMON:  return "lanmon";
4888         }
4889         return "ESS";
4890 }
4891
4892 static int proc_config_open( struct inode *inode, struct file *file ) {
4893         struct proc_data *data;
4894         struct proc_dir_entry *dp = PDE(inode);
4895         struct net_device *dev = dp->data;
4896         struct airo_info *ai = dev->priv;
4897         int i;
4898
4899         if ((file->private_data = kmalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
4900                 return -ENOMEM;
4901         memset(file->private_data, 0, sizeof(struct proc_data));
4902         data = (struct proc_data *)file->private_data;
4903         if ((data->rbuffer = kmalloc( 2048, GFP_KERNEL )) == NULL) {
4904                 kfree (file->private_data);
4905                 return -ENOMEM;
4906         }
4907         if ((data->wbuffer = kmalloc( 2048, GFP_KERNEL )) == NULL) {
4908                 kfree (data->rbuffer);
4909                 kfree (file->private_data);
4910                 return -ENOMEM;
4911         }
4912         memset( data->wbuffer, 0, 2048 );
4913         data->maxwritelen = 2048;
4914         data->on_close = proc_config_on_close;
4915
4916         readConfigRid(ai, 1);
4917
4918         i = sprintf( data->rbuffer,
4919                      "Mode: %s\n"
4920                      "Radio: %s\n"
4921                      "NodeName: %-16s\n"
4922                      "PowerMode: %s\n"
4923                      "DataRates: %d %d %d %d %d %d %d %d\n"
4924                      "Channel: %d\n"
4925                      "XmitPower: %d\n",
4926                      (ai->config.opmode & 0xFF) == 0 ? "adhoc" :
4927                      (ai->config.opmode & 0xFF) == 1 ? get_rmode(ai->config.rmode):
4928                      (ai->config.opmode & 0xFF) == 2 ? "AP" :
4929                      (ai->config.opmode & 0xFF) == 3 ? "AP RPTR" : "Error",
4930                      test_bit(FLAG_RADIO_OFF, &ai->flags) ? "off" : "on",
4931                      ai->config.nodeName,
4932                      ai->config.powerSaveMode == 0 ? "CAM" :
4933                      ai->config.powerSaveMode == 1 ? "PSP" :
4934                      ai->config.powerSaveMode == 2 ? "PSPCAM" : "Error",
4935                      (int)ai->config.rates[0],
4936                      (int)ai->config.rates[1],
4937                      (int)ai->config.rates[2],
4938                      (int)ai->config.rates[3],
4939                      (int)ai->config.rates[4],
4940                      (int)ai->config.rates[5],
4941                      (int)ai->config.rates[6],
4942                      (int)ai->config.rates[7],
4943                      (int)ai->config.channelSet,
4944                      (int)ai->config.txPower
4945                 );
4946         sprintf( data->rbuffer + i,
4947                  "LongRetryLimit: %d\n"
4948                  "ShortRetryLimit: %d\n"
4949                  "RTSThreshold: %d\n"
4950                  "TXMSDULifetime: %d\n"
4951                  "RXMSDULifetime: %d\n"
4952                  "TXDiversity: %s\n"
4953                  "RXDiversity: %s\n"
4954                  "FragThreshold: %d\n"
4955                  "WEP: %s\n"
4956                  "Modulation: %s\n"
4957                  "Preamble: %s\n",
4958                  (int)ai->config.longRetryLimit,
4959                  (int)ai->config.shortRetryLimit,
4960                  (int)ai->config.rtsThres,
4961                  (int)ai->config.txLifetime,
4962                  (int)ai->config.rxLifetime,
4963                  ai->config.txDiversity == 1 ? "left" :
4964                  ai->config.txDiversity == 2 ? "right" : "both",
4965                  ai->config.rxDiversity == 1 ? "left" :
4966                  ai->config.rxDiversity == 2 ? "right" : "both",
4967                  (int)ai->config.fragThresh,
4968                  ai->config.authType == AUTH_ENCRYPT ? "encrypt" :
4969                  ai->config.authType == AUTH_SHAREDKEY ? "shared" : "open",
4970                  ai->config.modulation == 0 ? "default" :
4971                  ai->config.modulation == MOD_CCK ? "cck" :
4972                  ai->config.modulation == MOD_MOK ? "mok" : "error",
4973                  ai->config.preamble == PREAMBLE_AUTO ? "auto" :
4974                  ai->config.preamble == PREAMBLE_LONG ? "long" :
4975                  ai->config.preamble == PREAMBLE_SHORT ? "short" : "error"
4976                 );
4977         data->readlen = strlen( data->rbuffer );
4978         return 0;
4979 }
4980
4981 static void proc_SSID_on_close( struct inode *inode, struct file *file ) {
4982         struct proc_data *data = (struct proc_data *)file->private_data;
4983         struct proc_dir_entry *dp = PDE(inode);
4984         struct net_device *dev = dp->data;
4985         struct airo_info *ai = dev->priv;
4986         SsidRid SSID_rid;
4987         Resp rsp;
4988         int i;
4989         int offset = 0;
4990
4991         if ( !data->writelen ) return;
4992
4993         memset( &SSID_rid, 0, sizeof( SSID_rid ) );
4994
4995         for( i = 0; i < 3; i++ ) {
4996                 int j;
4997                 for( j = 0; j+offset < data->writelen && j < 32 &&
4998                              data->wbuffer[offset+j] != '\n'; j++ ) {
4999                         SSID_rid.ssids[i].ssid[j] = data->wbuffer[offset+j];
5000                 }
5001                 if ( j == 0 ) break;
5002                 SSID_rid.ssids[i].len = j;
5003                 offset += j;
5004                 while( data->wbuffer[offset] != '\n' &&
5005                        offset < data->writelen ) offset++;
5006                 offset++;
5007         }
5008         if (i)
5009                 SSID_rid.len = sizeof(SSID_rid);
5010         disable_MAC(ai, 1);
5011         writeSsidRid(ai, &SSID_rid, 1);
5012         enable_MAC(ai, &rsp, 1);
5013 }
5014
5015 static inline u8 hexVal(char c) {
5016         if (c>='0' && c<='9') return c -= '0';
5017         if (c>='a' && c<='f') return c -= 'a'-10;
5018         if (c>='A' && c<='F') return c -= 'A'-10;
5019         return 0;
5020 }
5021
5022 static void proc_APList_on_close( struct inode *inode, struct file *file ) {
5023         struct proc_data *data = (struct proc_data *)file->private_data;
5024         struct proc_dir_entry *dp = PDE(inode);
5025         struct net_device *dev = dp->data;
5026         struct airo_info *ai = dev->priv;
5027         APListRid APList_rid;
5028         Resp rsp;
5029         int i;
5030
5031         if ( !data->writelen ) return;
5032
5033         memset( &APList_rid, 0, sizeof(APList_rid) );
5034         APList_rid.len = sizeof(APList_rid);
5035
5036         for( i = 0; i < 4 && data->writelen >= (i+1)*6*3; i++ ) {
5037                 int j;
5038                 for( j = 0; j < 6*3 && data->wbuffer[j+i*6*3]; j++ ) {
5039                         switch(j%3) {
5040                         case 0:
5041                                 APList_rid.ap[i][j/3]=
5042                                         hexVal(data->wbuffer[j+i*6*3])<<4;
5043                                 break;
5044                         case 1:
5045                                 APList_rid.ap[i][j/3]|=
5046                                         hexVal(data->wbuffer[j+i*6*3]);
5047                                 break;
5048                         }
5049                 }
5050         }
5051         disable_MAC(ai, 1);
5052         writeAPListRid(ai, &APList_rid, 1);
5053         enable_MAC(ai, &rsp, 1);
5054 }
5055
5056 /* This function wraps PC4500_writerid with a MAC disable */
5057 static int do_writerid( struct airo_info *ai, u16 rid, const void *rid_data,
5058                         int len, int dummy ) {
5059         int rc;
5060         Resp rsp;
5061
5062         disable_MAC(ai, 1);
5063         rc = PC4500_writerid(ai, rid, rid_data, len, 1);
5064         enable_MAC(ai, &rsp, 1);
5065         return rc;
5066 }
5067
5068 /* Returns the length of the key at the index.  If index == 0xffff
5069  * the index of the transmit key is returned.  If the key doesn't exist,
5070  * -1 will be returned.
5071  */
5072 static int get_wep_key(struct airo_info *ai, u16 index) {
5073         WepKeyRid wkr;
5074         int rc;
5075         u16 lastindex;
5076
5077         rc = readWepKeyRid(ai, &wkr, 1, 1);
5078         if (rc == SUCCESS) do {
5079                 lastindex = wkr.kindex;
5080                 if (wkr.kindex == index) {
5081                         if (index == 0xffff) {
5082                                 return wkr.mac[0];
5083                         }
5084                         return wkr.klen;
5085                 }
5086                 readWepKeyRid(ai, &wkr, 0, 1);
5087         } while(lastindex != wkr.kindex);
5088         return -1;
5089 }
5090
5091 static int set_wep_key(struct airo_info *ai, u16 index,
5092                        const char *key, u16 keylen, int perm, int lock ) {
5093         static const unsigned char macaddr[ETH_ALEN] = { 0x01, 0, 0, 0, 0, 0 };
5094         WepKeyRid wkr;
5095         Resp rsp;
5096
5097         memset(&wkr, 0, sizeof(wkr));
5098         if (keylen == 0) {
5099 // We are selecting which key to use
5100                 wkr.len = sizeof(wkr);
5101                 wkr.kindex = 0xffff;
5102                 wkr.mac[0] = (char)index;
5103                 if (perm) printk(KERN_INFO "Setting transmit key to %d\n", index);
5104                 if (perm) ai->defindex = (char)index;
5105         } else {
5106 // We are actually setting the key
5107                 wkr.len = sizeof(wkr);
5108                 wkr.kindex = index;
5109                 wkr.klen = keylen;
5110                 memcpy( wkr.key, key, keylen );
5111                 memcpy( wkr.mac, macaddr, ETH_ALEN );
5112                 printk(KERN_INFO "Setting key %d\n", index);
5113         }
5114
5115         disable_MAC(ai, lock);
5116         writeWepKeyRid(ai, &wkr, perm, lock);
5117         enable_MAC(ai, &rsp, lock);
5118         return 0;
5119 }
5120
5121 static void proc_wepkey_on_close( struct inode *inode, struct file *file ) {
5122         struct proc_data *data;
5123         struct proc_dir_entry *dp = PDE(inode);
5124         struct net_device *dev = dp->data;
5125         struct airo_info *ai = dev->priv;
5126         int i;
5127         char key[16];
5128         u16 index = 0;
5129         int j = 0;
5130
5131         memset(key, 0, sizeof(key));
5132
5133         data = (struct proc_data *)file->private_data;
5134         if ( !data->writelen ) return;
5135
5136         if (data->wbuffer[0] >= '0' && data->wbuffer[0] <= '3' &&
5137             (data->wbuffer[1] == ' ' || data->wbuffer[1] == '\n')) {
5138                 index = data->wbuffer[0] - '0';
5139                 if (data->wbuffer[1] == '\n') {
5140                         set_wep_key(ai, index, NULL, 0, 1, 1);
5141                         return;
5142                 }
5143                 j = 2;
5144         } else {
5145                 printk(KERN_ERR "airo:  WepKey passed invalid key index\n");
5146                 return;
5147         }
5148
5149         for( i = 0; i < 16*3 && data->wbuffer[i+j]; i++ ) {
5150                 switch(i%3) {
5151                 case 0:
5152                         key[i/3] = hexVal(data->wbuffer[i+j])<<4;
5153                         break;
5154                 case 1:
5155                         key[i/3] |= hexVal(data->wbuffer[i+j]);
5156                         break;
5157                 }
5158         }
5159         set_wep_key(ai, index, key, i/3, 1, 1);
5160 }
5161
5162 static int proc_wepkey_open( struct inode *inode, struct file *file ) {
5163         struct proc_data *data;
5164         struct proc_dir_entry *dp = PDE(inode);
5165         struct net_device *dev = dp->data;
5166         struct airo_info *ai = dev->priv;
5167         char *ptr;
5168         WepKeyRid wkr;
5169         u16 lastindex;
5170         int j=0;
5171         int rc;
5172
5173         if ((file->private_data = kmalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
5174                 return -ENOMEM;
5175         memset(file->private_data, 0, sizeof(struct proc_data));
5176         memset(&wkr, 0, sizeof(wkr));
5177         data = (struct proc_data *)file->private_data;
5178         if ((data->rbuffer = kmalloc( 180, GFP_KERNEL )) == NULL) {
5179                 kfree (file->private_data);
5180                 return -ENOMEM;
5181         }
5182         memset(data->rbuffer, 0, 180);
5183         data->writelen = 0;
5184         data->maxwritelen = 80;
5185         if ((data->wbuffer = kmalloc( 80, GFP_KERNEL )) == NULL) {
5186                 kfree (data->rbuffer);
5187                 kfree (file->private_data);
5188                 return -ENOMEM;
5189         }
5190         memset( data->wbuffer, 0, 80 );
5191         data->on_close = proc_wepkey_on_close;
5192
5193         ptr = data->rbuffer;
5194         strcpy(ptr, "No wep keys\n");
5195         rc = readWepKeyRid(ai, &wkr, 1, 1);
5196         if (rc == SUCCESS) do {
5197                 lastindex = wkr.kindex;
5198                 if (wkr.kindex == 0xffff) {
5199                         j += sprintf(ptr+j, "Tx key = %d\n",
5200                                      (int)wkr.mac[0]);
5201                 } else {
5202                         j += sprintf(ptr+j, "Key %d set with length = %d\n",
5203                                      (int)wkr.kindex, (int)wkr.klen);
5204                 }
5205                 readWepKeyRid(ai, &wkr, 0, 1);
5206         } while((lastindex != wkr.kindex) && (j < 180-30));
5207
5208         data->readlen = strlen( data->rbuffer );
5209         return 0;
5210 }
5211
5212 static int proc_SSID_open( struct inode *inode, struct file *file ) {
5213         struct proc_data *data;
5214         struct proc_dir_entry *dp = PDE(inode);
5215         struct net_device *dev = dp->data;
5216         struct airo_info *ai = dev->priv;
5217         int i;
5218         char *ptr;
5219         SsidRid SSID_rid;
5220
5221         if ((file->private_data = kmalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
5222                 return -ENOMEM;
5223         memset(file->private_data, 0, sizeof(struct proc_data));
5224         data = (struct proc_data *)file->private_data;
5225         if ((data->rbuffer = kmalloc( 104, GFP_KERNEL )) == NULL) {
5226                 kfree (file->private_data);
5227                 return -ENOMEM;
5228         }
5229         data->writelen = 0;
5230         data->maxwritelen = 33*3;
5231         if ((data->wbuffer = kmalloc( 33*3, GFP_KERNEL )) == NULL) {
5232                 kfree (data->rbuffer);
5233                 kfree (file->private_data);
5234                 return -ENOMEM;
5235         }
5236         memset( data->wbuffer, 0, 33*3 );
5237         data->on_close = proc_SSID_on_close;
5238
5239         readSsidRid(ai, &SSID_rid);
5240         ptr = data->rbuffer;
5241         for( i = 0; i < 3; i++ ) {
5242                 int j;
5243                 if ( !SSID_rid.ssids[i].len ) break;
5244                 for( j = 0; j < 32 &&
5245                              j < SSID_rid.ssids[i].len &&
5246                              SSID_rid.ssids[i].ssid[j]; j++ ) {
5247                         *ptr++ = SSID_rid.ssids[i].ssid[j];
5248                 }
5249                 *ptr++ = '\n';
5250         }
5251         *ptr = '\0';
5252         data->readlen = strlen( data->rbuffer );
5253         return 0;
5254 }
5255
5256 static int proc_APList_open( struct inode *inode, struct file *file ) {
5257         struct proc_data *data;
5258         struct proc_dir_entry *dp = PDE(inode);
5259         struct net_device *dev = dp->data;
5260         struct airo_info *ai = dev->priv;
5261         int i;
5262         char *ptr;
5263         APListRid APList_rid;
5264
5265         if ((file->private_data = kmalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
5266                 return -ENOMEM;
5267         memset(file->private_data, 0, sizeof(struct proc_data));
5268         data = (struct proc_data *)file->private_data;
5269         if ((data->rbuffer = kmalloc( 104, GFP_KERNEL )) == NULL) {
5270                 kfree (file->private_data);
5271                 return -ENOMEM;
5272         }
5273         data->writelen = 0;
5274         data->maxwritelen = 4*6*3;
5275         if ((data->wbuffer = kmalloc( data->maxwritelen, GFP_KERNEL )) == NULL) {
5276                 kfree (data->rbuffer);
5277                 kfree (file->private_data);
5278                 return -ENOMEM;
5279         }
5280         memset( data->wbuffer, 0, data->maxwritelen );
5281         data->on_close = proc_APList_on_close;
5282
5283         readAPListRid(ai, &APList_rid);
5284         ptr = data->rbuffer;
5285         for( i = 0; i < 4; i++ ) {
5286 // We end when we find a zero MAC
5287                 if ( !*(int*)APList_rid.ap[i] &&
5288                      !*(int*)&APList_rid.ap[i][2]) break;
5289                 ptr += sprintf(ptr, "%02x:%02x:%02x:%02x:%02x:%02x\n",
5290                                (int)APList_rid.ap[i][0],
5291                                (int)APList_rid.ap[i][1],
5292                                (int)APList_rid.ap[i][2],
5293                                (int)APList_rid.ap[i][3],
5294                                (int)APList_rid.ap[i][4],
5295                                (int)APList_rid.ap[i][5]);
5296         }
5297         if (i==0) ptr += sprintf(ptr, "Not using specific APs\n");
5298
5299         *ptr = '\0';
5300         data->readlen = strlen( data->rbuffer );
5301         return 0;
5302 }
5303
5304 static int proc_BSSList_open( struct inode *inode, struct file *file ) {
5305         struct proc_data *data;
5306         struct proc_dir_entry *dp = PDE(inode);
5307         struct net_device *dev = dp->data;
5308         struct airo_info *ai = dev->priv;
5309         char *ptr;
5310         BSSListRid BSSList_rid;
5311         int rc;
5312         /* If doLoseSync is not 1, we won't do a Lose Sync */
5313         int doLoseSync = -1;
5314
5315         if ((file->private_data = kmalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
5316                 return -ENOMEM;
5317         memset(file->private_data, 0, sizeof(struct proc_data));
5318         data = (struct proc_data *)file->private_data;
5319         if ((data->rbuffer = kmalloc( 1024, GFP_KERNEL )) == NULL) {
5320                 kfree (file->private_data);
5321                 return -ENOMEM;
5322         }
5323         data->writelen = 0;
5324         data->maxwritelen = 0;
5325         data->wbuffer = NULL;
5326         data->on_close = NULL;
5327
5328         if (file->f_mode & FMODE_WRITE) {
5329                 if (!(file->f_mode & FMODE_READ)) {
5330                         Cmd cmd;
5331                         Resp rsp;
5332
5333                         if (ai->flags & FLAG_RADIO_MASK) return -ENETDOWN;
5334                         memset(&cmd, 0, sizeof(cmd));
5335                         cmd.cmd=CMD_LISTBSS;
5336                         if (down_interruptible(&ai->sem))
5337                                 return -ERESTARTSYS;
5338                         issuecommand(ai, &cmd, &rsp);
5339                         up(&ai->sem);
5340                         data->readlen = 0;
5341                         return 0;
5342                 }
5343                 doLoseSync = 1;
5344         }
5345         ptr = data->rbuffer;
5346         /* There is a race condition here if there are concurrent opens.
5347            Since it is a rare condition, we'll just live with it, otherwise
5348            we have to add a spin lock... */
5349         rc = readBSSListRid(ai, doLoseSync, &BSSList_rid);
5350         while(rc == 0 && BSSList_rid.index != 0xffff) {
5351                 ptr += sprintf(ptr, "%02x:%02x:%02x:%02x:%02x:%02x %*s rssi = %d",
5352                                 (int)BSSList_rid.bssid[0],
5353                                 (int)BSSList_rid.bssid[1],
5354                                 (int)BSSList_rid.bssid[2],
5355                                 (int)BSSList_rid.bssid[3],
5356                                 (int)BSSList_rid.bssid[4],
5357                                 (int)BSSList_rid.bssid[5],
5358                                 (int)BSSList_rid.ssidLen,
5359                                 BSSList_rid.ssid,
5360                                 (int)BSSList_rid.dBm);
5361                 ptr += sprintf(ptr, " channel = %d %s %s %s %s\n",
5362                                 (int)BSSList_rid.dsChannel,
5363                                 BSSList_rid.cap & CAP_ESS ? "ESS" : "",
5364                                 BSSList_rid.cap & CAP_IBSS ? "adhoc" : "",
5365                                 BSSList_rid.cap & CAP_PRIVACY ? "wep" : "",
5366                                 BSSList_rid.cap & CAP_SHORTHDR ? "shorthdr" : "");
5367                 rc = readBSSListRid(ai, 0, &BSSList_rid);
5368         }
5369         *ptr = '\0';
5370         data->readlen = strlen( data->rbuffer );
5371         return 0;
5372 }
5373
5374 static int proc_close( struct inode *inode, struct file *file )
5375 {
5376         struct proc_data *data = (struct proc_data *)file->private_data;
5377         if ( data->on_close != NULL ) data->on_close( inode, file );
5378         if ( data->rbuffer ) kfree( data->rbuffer );
5379         if ( data->wbuffer ) kfree( data->wbuffer );
5380         kfree( data );
5381         return 0;
5382 }
5383
5384 static struct net_device_list {
5385         struct net_device *dev;
5386         struct net_device_list *next;
5387 } *airo_devices;
5388
5389 /* Since the card doesn't automatically switch to the right WEP mode,
5390    we will make it do it.  If the card isn't associated, every secs we
5391    will switch WEP modes to see if that will help.  If the card is
5392    associated we will check every minute to see if anything has
5393    changed. */
5394 static void timer_func( struct net_device *dev ) {
5395         struct airo_info *apriv = dev->priv;
5396         Resp rsp;
5397
5398 /* We don't have a link so try changing the authtype */
5399         readConfigRid(apriv, 0);
5400         disable_MAC(apriv, 0);
5401         switch(apriv->config.authType) {
5402                 case AUTH_ENCRYPT:
5403 /* So drop to OPEN */
5404                         apriv->config.authType = AUTH_OPEN;
5405                         break;
5406                 case AUTH_SHAREDKEY:
5407                         if (apriv->keyindex < auto_wep) {
5408                                 set_wep_key(apriv, apriv->keyindex, NULL, 0, 0, 0);
5409                                 apriv->config.authType = AUTH_SHAREDKEY;
5410                                 apriv->keyindex++;
5411                         } else {
5412                                 /* Drop to ENCRYPT */
5413                                 apriv->keyindex = 0;
5414                                 set_wep_key(apriv, apriv->defindex, NULL, 0, 0, 0);
5415                                 apriv->config.authType = AUTH_ENCRYPT;
5416                         }
5417                         break;
5418                 default:  /* We'll escalate to SHAREDKEY */
5419                         apriv->config.authType = AUTH_SHAREDKEY;
5420         }
5421         set_bit (FLAG_COMMIT, &apriv->flags);
5422         writeConfigRid(apriv, 0);
5423         enable_MAC(apriv, &rsp, 0);
5424         up(&apriv->sem);
5425
5426 /* Schedule check to see if the change worked */
5427         clear_bit(JOB_AUTOWEP, &apriv->flags);
5428         apriv->expires = RUN_AT(HZ*3);
5429 }
5430
5431 static int add_airo_dev( struct net_device *dev ) {
5432         struct net_device_list *node = kmalloc( sizeof( *node ), GFP_KERNEL );
5433         if ( !node )
5434                 return -ENOMEM;
5435
5436         node->dev = dev;
5437         node->next = airo_devices;
5438         airo_devices = node;
5439
5440         return 0;
5441 }
5442
5443 static void del_airo_dev( struct net_device *dev ) {
5444         struct net_device_list **p = &airo_devices;
5445         while( *p && ( (*p)->dev != dev ) )
5446                 p = &(*p)->next;
5447         if ( *p && (*p)->dev == dev )
5448                 *p = (*p)->next;
5449 }
5450
5451 #ifdef CONFIG_PCI
5452 static int __devinit airo_pci_probe(struct pci_dev *pdev,
5453                                     const struct pci_device_id *pent)
5454 {
5455         struct net_device *dev;
5456
5457         if (pci_enable_device(pdev))
5458                 return -ENODEV;
5459         pci_set_master(pdev);
5460
5461         if (pdev->device == 0x5000 || pdev->device == 0xa504)
5462                         dev = _init_airo_card(pdev->irq, pdev->resource[0].start, 0, pdev, &pdev->dev);
5463         else
5464                         dev = _init_airo_card(pdev->irq, pdev->resource[2].start, 0, pdev, &pdev->dev);
5465         if (!dev)
5466                 return -ENODEV;
5467
5468         pci_set_drvdata(pdev, dev);
5469         return 0;
5470 }
5471
5472 static void __devexit airo_pci_remove(struct pci_dev *pdev)
5473 {
5474 }
5475
5476 static int airo_pci_suspend(struct pci_dev *pdev, pm_message_t state)
5477 {
5478         struct net_device *dev = pci_get_drvdata(pdev);
5479         struct airo_info *ai = dev->priv;
5480         Cmd cmd;
5481         Resp rsp;
5482
5483         if ((ai->APList == NULL) &&
5484                 (ai->APList = kmalloc(sizeof(APListRid), GFP_KERNEL)) == NULL)
5485                 return -ENOMEM;
5486         if ((ai->SSID == NULL) &&
5487                 (ai->SSID = kmalloc(sizeof(SsidRid), GFP_KERNEL)) == NULL)
5488                 return -ENOMEM;
5489         readAPListRid(ai, ai->APList);
5490         readSsidRid(ai, ai->SSID);
5491         memset(&cmd, 0, sizeof(cmd));
5492         /* the lock will be released at the end of the resume callback */
5493         if (down_interruptible(&ai->sem))
5494                 return -EAGAIN;
5495         disable_MAC(ai, 0);
5496         netif_device_detach(dev);
5497         ai->power = state;
5498         cmd.cmd=HOSTSLEEP;
5499         issuecommand(ai, &cmd, &rsp);
5500
5501         pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
5502         pci_save_state(pdev);
5503         return pci_set_power_state(pdev, pci_choose_state(pdev, state));
5504 }
5505
5506 static int airo_pci_resume(struct pci_dev *pdev)
5507 {
5508         struct net_device *dev = pci_get_drvdata(pdev);
5509         struct airo_info *ai = dev->priv;
5510         Resp rsp;
5511
5512         pci_set_power_state(pdev, 0);
5513         pci_restore_state(pdev);
5514         pci_enable_wake(pdev, pci_choose_state(pdev, ai->power), 0);
5515
5516         if (ai->power > 1) {
5517                 reset_card(dev, 0);
5518                 mpi_init_descriptors(ai);
5519                 setup_card(ai, dev->dev_addr, 0);
5520                 clear_bit(FLAG_RADIO_OFF, &ai->flags);
5521                 clear_bit(FLAG_PENDING_XMIT, &ai->flags);
5522         } else {
5523                 OUT4500(ai, EVACK, EV_AWAKEN);
5524                 OUT4500(ai, EVACK, EV_AWAKEN);
5525                 msleep(100);
5526         }
5527
5528         set_bit (FLAG_COMMIT, &ai->flags);
5529         disable_MAC(ai, 0);
5530         msleep(200);
5531         if (ai->SSID) {
5532                 writeSsidRid(ai, ai->SSID, 0);
5533                 kfree(ai->SSID);
5534                 ai->SSID = NULL;
5535         }
5536         if (ai->APList) {
5537                 writeAPListRid(ai, ai->APList, 0);
5538                 kfree(ai->APList);
5539                 ai->APList = NULL;
5540         }
5541         writeConfigRid(ai, 0);
5542         enable_MAC(ai, &rsp, 0);
5543         ai->power = PMSG_ON;
5544         netif_device_attach(dev);
5545         netif_wake_queue(dev);
5546         enable_interrupts(ai);
5547         up(&ai->sem);
5548         return 0;
5549 }
5550 #endif
5551
5552 static int __init airo_init_module( void )
5553 {
5554         int i, have_isa_dev = 0;
5555
5556         airo_entry = create_proc_entry("aironet",
5557                                        S_IFDIR | airo_perm,
5558                                        proc_root_driver);
5559         airo_entry->uid = proc_uid;
5560         airo_entry->gid = proc_gid;
5561
5562         for( i = 0; i < 4 && io[i] && irq[i]; i++ ) {
5563                 printk( KERN_INFO
5564                         "airo:  Trying to configure ISA adapter at irq=%d io=0x%x\n",
5565                         irq[i], io[i] );
5566                 if (init_airo_card( irq[i], io[i], 0, NULL ))
5567                         have_isa_dev = 1;
5568         }
5569
5570 #ifdef CONFIG_PCI
5571         printk( KERN_INFO "airo:  Probing for PCI adapters\n" );
5572         pci_register_driver(&airo_driver);
5573         printk( KERN_INFO "airo:  Finished probing for PCI adapters\n" );
5574 #endif
5575
5576         /* Always exit with success, as we are a library module
5577          * as well as a driver module
5578          */
5579         return 0;
5580 }
5581
5582 static void __exit airo_cleanup_module( void )
5583 {
5584         while( airo_devices ) {
5585                 printk( KERN_INFO "airo: Unregistering %s\n", airo_devices->dev->name );
5586                 stop_airo_card( airo_devices->dev, 1 );
5587         }
5588 #ifdef CONFIG_PCI
5589         pci_unregister_driver(&airo_driver);
5590 #endif
5591         remove_proc_entry("aironet", proc_root_driver);
5592 }
5593
5594 #ifdef WIRELESS_EXT
5595 /*
5596  * Initial Wireless Extension code for Aironet driver by :
5597  *      Jean Tourrilhes <jt@hpl.hp.com> - HPL - 17 November 00
5598  * Conversion to new driver API by :
5599  *      Jean Tourrilhes <jt@hpl.hp.com> - HPL - 26 March 02
5600  * Javier also did a good amount of work here, adding some new extensions
5601  * and fixing my code. Let's just say that without him this code just
5602  * would not work at all... - Jean II
5603  */
5604
5605 static u8 airo_rssi_to_dbm (tdsRssiEntry *rssi_rid, u8 rssi)
5606 {
5607         if( !rssi_rid )
5608                 return 0;
5609
5610         return (0x100 - rssi_rid[rssi].rssidBm);
5611 }
5612
5613 static u8 airo_dbm_to_pct (tdsRssiEntry *rssi_rid, u8 dbm)
5614 {
5615         int i;
5616
5617         if( !rssi_rid )
5618                 return 0;
5619
5620         for( i = 0; i < 256; i++ )
5621                 if (rssi_rid[i].rssidBm == dbm)
5622                         return rssi_rid[i].rssipct;
5623
5624         return 0;
5625 }
5626
5627
5628 static int airo_get_quality (StatusRid *status_rid, CapabilityRid *cap_rid)
5629 {
5630         int quality = 0;
5631
5632         if ((status_rid->mode & 0x3f) == 0x3f && (cap_rid->hardCap & 8)) {
5633                 if (memcmp(cap_rid->prodName, "350", 3))
5634                         if (status_rid->signalQuality > 0x20)
5635                                 quality = 0;
5636                         else
5637                                 quality = 0x20 - status_rid->signalQuality;
5638                 else
5639                         if (status_rid->signalQuality > 0xb0)
5640                                 quality = 0;
5641                         else if (status_rid->signalQuality < 0x10)
5642                                 quality = 0xa0;
5643                         else
5644                                 quality = 0xb0 - status_rid->signalQuality;
5645         }
5646         return quality;
5647 }
5648
5649 #define airo_get_max_quality(cap_rid) (memcmp((cap_rid)->prodName, "350", 3) ? 0x20 : 0xa0)
5650 #define airo_get_avg_quality(cap_rid) (memcmp((cap_rid)->prodName, "350", 3) ? 0x10 : 0x50);
5651
5652 /*------------------------------------------------------------------*/
5653 /*
5654  * Wireless Handler : get protocol name
5655  */
5656 static int airo_get_name(struct net_device *dev,
5657                          struct iw_request_info *info,
5658                          char *cwrq,
5659                          char *extra)
5660 {
5661         strcpy(cwrq, "IEEE 802.11-DS");
5662         return 0;
5663 }
5664
5665 /*------------------------------------------------------------------*/
5666 /*
5667  * Wireless Handler : set frequency
5668  */
5669 static int airo_set_freq(struct net_device *dev,
5670                          struct iw_request_info *info,
5671                          struct iw_freq *fwrq,
5672                          char *extra)
5673 {
5674         struct airo_info *local = dev->priv;
5675         int rc = -EINPROGRESS;          /* Call commit handler */
5676
5677         /* If setting by frequency, convert to a channel */
5678         if((fwrq->e == 1) &&
5679            (fwrq->m >= (int) 2.412e8) &&
5680            (fwrq->m <= (int) 2.487e8)) {
5681                 int f = fwrq->m / 100000;
5682                 int c = 0;
5683                 while((c < 14) && (f != frequency_list[c]))
5684                         c++;
5685                 /* Hack to fall through... */
5686                 fwrq->e = 0;
5687                 fwrq->m = c + 1;
5688         }
5689         /* Setting by channel number */
5690         if((fwrq->m > 1000) || (fwrq->e > 0))
5691                 rc = -EOPNOTSUPP;
5692         else {
5693                 int channel = fwrq->m;
5694                 /* We should do a better check than that,
5695                  * based on the card capability !!! */
5696                 if((channel < 1) || (channel > 16)) {
5697                         printk(KERN_DEBUG "%s: New channel value of %d is invalid!\n", dev->name, fwrq->m);
5698                         rc = -EINVAL;
5699                 } else {
5700                         readConfigRid(local, 1);
5701                         /* Yes ! We can set it !!! */
5702                         local->config.channelSet = (u16)(channel - 1);
5703                         set_bit (FLAG_COMMIT, &local->flags);
5704                 }
5705         }
5706         return rc;
5707 }
5708
5709 /*------------------------------------------------------------------*/
5710 /*
5711  * Wireless Handler : get frequency
5712  */
5713 static int airo_get_freq(struct net_device *dev,
5714                          struct iw_request_info *info,
5715                          struct iw_freq *fwrq,
5716                          char *extra)
5717 {
5718         struct airo_info *local = dev->priv;
5719         StatusRid status_rid;           /* Card status info */
5720
5721         readConfigRid(local, 1);
5722         if ((local->config.opmode & 0xFF) == MODE_STA_ESS)
5723                 status_rid.channel = local->config.channelSet;
5724         else
5725                 readStatusRid(local, &status_rid, 1);
5726
5727 #ifdef WEXT_USECHANNELS
5728         fwrq->m = ((int)status_rid.channel) + 1;
5729         fwrq->e = 0;
5730 #else
5731         {
5732                 int f = (int)status_rid.channel;
5733                 fwrq->m = frequency_list[f] * 100000;
5734                 fwrq->e = 1;
5735         }
5736 #endif
5737
5738         return 0;
5739 }
5740
5741 /*------------------------------------------------------------------*/
5742 /*
5743  * Wireless Handler : set ESSID
5744  */
5745 static int airo_set_essid(struct net_device *dev,
5746                           struct iw_request_info *info,
5747                           struct iw_point *dwrq,
5748                           char *extra)
5749 {
5750         struct airo_info *local = dev->priv;
5751         Resp rsp;
5752         SsidRid SSID_rid;               /* SSIDs */
5753
5754         /* Reload the list of current SSID */
5755         readSsidRid(local, &SSID_rid);
5756
5757         /* Check if we asked for `any' */
5758         if(dwrq->flags == 0) {
5759                 /* Just send an empty SSID list */
5760                 memset(&SSID_rid, 0, sizeof(SSID_rid));
5761         } else {
5762                 int     index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
5763
5764                 /* Check the size of the string */
5765                 if(dwrq->length > IW_ESSID_MAX_SIZE+1) {
5766                         return -E2BIG ;
5767                 }
5768                 /* Check if index is valid */
5769                 if((index < 0) || (index >= 4)) {
5770                         return -EINVAL;
5771                 }
5772
5773                 /* Set the SSID */
5774                 memset(SSID_rid.ssids[index].ssid, 0,
5775                        sizeof(SSID_rid.ssids[index].ssid));
5776                 memcpy(SSID_rid.ssids[index].ssid, extra, dwrq->length);
5777                 SSID_rid.ssids[index].len = dwrq->length - 1;
5778         }
5779         SSID_rid.len = sizeof(SSID_rid);
5780         /* Write it to the card */
5781         disable_MAC(local, 1);
5782         writeSsidRid(local, &SSID_rid, 1);
5783         enable_MAC(local, &rsp, 1);
5784
5785         return 0;
5786 }
5787
5788 /*------------------------------------------------------------------*/
5789 /*
5790  * Wireless Handler : get ESSID
5791  */
5792 static int airo_get_essid(struct net_device *dev,
5793                           struct iw_request_info *info,
5794                           struct iw_point *dwrq,
5795                           char *extra)
5796 {
5797         struct airo_info *local = dev->priv;
5798         StatusRid status_rid;           /* Card status info */
5799
5800         readStatusRid(local, &status_rid, 1);
5801
5802         /* Note : if dwrq->flags != 0, we should
5803          * get the relevant SSID from the SSID list... */
5804
5805         /* Get the current SSID */
5806         memcpy(extra, status_rid.SSID, status_rid.SSIDlen);
5807         extra[status_rid.SSIDlen] = '\0';
5808         /* If none, we may want to get the one that was set */
5809
5810         /* Push it out ! */
5811         dwrq->length = status_rid.SSIDlen + 1;
5812         dwrq->flags = 1; /* active */
5813
5814         return 0;
5815 }
5816
5817 /*------------------------------------------------------------------*/
5818 /*
5819  * Wireless Handler : set AP address
5820  */
5821 static int airo_set_wap(struct net_device *dev,
5822                         struct iw_request_info *info,
5823                         struct sockaddr *awrq,
5824                         char *extra)
5825 {
5826         struct airo_info *local = dev->priv;
5827         Cmd cmd;
5828         Resp rsp;
5829         APListRid APList_rid;
5830         static const unsigned char bcast[ETH_ALEN] = { 255, 255, 255, 255, 255, 255 };
5831
5832         if (awrq->sa_family != ARPHRD_ETHER)
5833                 return -EINVAL;
5834         else if (!memcmp(bcast, awrq->sa_data, ETH_ALEN)) {
5835                 memset(&cmd, 0, sizeof(cmd));
5836                 cmd.cmd=CMD_LOSE_SYNC;
5837                 if (down_interruptible(&local->sem))
5838                         return -ERESTARTSYS;
5839                 issuecommand(local, &cmd, &rsp);
5840                 up(&local->sem);
5841         } else {
5842                 memset(&APList_rid, 0, sizeof(APList_rid));
5843                 APList_rid.len = sizeof(APList_rid);
5844                 memcpy(APList_rid.ap[0], awrq->sa_data, ETH_ALEN);
5845                 disable_MAC(local, 1);
5846                 writeAPListRid(local, &APList_rid, 1);
5847                 enable_MAC(local, &rsp, 1);
5848         }
5849         return 0;
5850 }
5851
5852 /*------------------------------------------------------------------*/
5853 /*
5854  * Wireless Handler : get AP address
5855  */
5856 static int airo_get_wap(struct net_device *dev,
5857                         struct iw_request_info *info,
5858                         struct sockaddr *awrq,
5859                         char *extra)
5860 {
5861         struct airo_info *local = dev->priv;
5862         StatusRid status_rid;           /* Card status info */
5863
5864         readStatusRid(local, &status_rid, 1);
5865
5866         /* Tentative. This seems to work, wow, I'm lucky !!! */
5867         memcpy(awrq->sa_data, status_rid.bssid[0], ETH_ALEN);
5868         awrq->sa_family = ARPHRD_ETHER;
5869
5870         return 0;
5871 }
5872
5873 /*------------------------------------------------------------------*/
5874 /*
5875  * Wireless Handler : set Nickname
5876  */
5877 static int airo_set_nick(struct net_device *dev,
5878                          struct iw_request_info *info,
5879                          struct iw_point *dwrq,
5880                          char *extra)
5881 {
5882         struct airo_info *local = dev->priv;
5883
5884         /* Check the size of the string */
5885         if(dwrq->length > 16 + 1) {
5886                 return -E2BIG;
5887         }
5888         readConfigRid(local, 1);
5889         memset(local->config.nodeName, 0, sizeof(local->config.nodeName));
5890         memcpy(local->config.nodeName, extra, dwrq->length);
5891         set_bit (FLAG_COMMIT, &local->flags);
5892
5893         return -EINPROGRESS;            /* Call commit handler */
5894 }
5895
5896 /*------------------------------------------------------------------*/
5897 /*
5898  * Wireless Handler : get Nickname
5899  */
5900 static int airo_get_nick(struct net_device *dev,
5901                          struct iw_request_info *info,
5902                          struct iw_point *dwrq,
5903                          char *extra)
5904 {
5905         struct airo_info *local = dev->priv;
5906
5907         readConfigRid(local, 1);
5908         strncpy(extra, local->config.nodeName, 16);
5909         extra[16] = '\0';
5910         dwrq->length = strlen(extra) + 1;
5911
5912         return 0;
5913 }
5914
5915 /*------------------------------------------------------------------*/
5916 /*
5917  * Wireless Handler : set Bit-Rate
5918  */
5919 static int airo_set_rate(struct net_device *dev,
5920                          struct iw_request_info *info,
5921                          struct iw_param *vwrq,
5922                          char *extra)
5923 {
5924         struct airo_info *local = dev->priv;
5925         CapabilityRid cap_rid;          /* Card capability info */
5926         u8      brate = 0;
5927         int     i;
5928
5929         /* First : get a valid bit rate value */
5930         readCapabilityRid(local, &cap_rid, 1);
5931
5932         /* Which type of value ? */
5933         if((vwrq->value < 8) && (vwrq->value >= 0)) {
5934                 /* Setting by rate index */
5935                 /* Find value in the magic rate table */
5936                 brate = cap_rid.supportedRates[vwrq->value];
5937         } else {
5938                 /* Setting by frequency value */
5939                 u8      normvalue = (u8) (vwrq->value/500000);
5940
5941                 /* Check if rate is valid */
5942                 for(i = 0 ; i < 8 ; i++) {
5943                         if(normvalue == cap_rid.supportedRates[i]) {
5944                                 brate = normvalue;
5945                                 break;
5946                         }
5947                 }
5948         }
5949         /* -1 designed the max rate (mostly auto mode) */
5950         if(vwrq->value == -1) {
5951                 /* Get the highest available rate */
5952                 for(i = 0 ; i < 8 ; i++) {
5953                         if(cap_rid.supportedRates[i] == 0)
5954                                 break;
5955                 }
5956                 if(i != 0)
5957                         brate = cap_rid.supportedRates[i - 1];
5958         }
5959         /* Check that it is valid */
5960         if(brate == 0) {
5961                 return -EINVAL;
5962         }
5963
5964         readConfigRid(local, 1);
5965         /* Now, check if we want a fixed or auto value */
5966         if(vwrq->fixed == 0) {
5967                 /* Fill all the rates up to this max rate */
5968                 memset(local->config.rates, 0, 8);
5969                 for(i = 0 ; i < 8 ; i++) {
5970                         local->config.rates[i] = cap_rid.supportedRates[i];
5971                         if(local->config.rates[i] == brate)
5972                                 break;
5973                 }
5974         } else {
5975                 /* Fixed mode */
5976                 /* One rate, fixed */
5977                 memset(local->config.rates, 0, 8);
5978                 local->config.rates[0] = brate;
5979         }
5980         set_bit (FLAG_COMMIT, &local->flags);
5981
5982         return -EINPROGRESS;            /* Call commit handler */
5983 }
5984
5985 /*------------------------------------------------------------------*/
5986 /*
5987  * Wireless Handler : get Bit-Rate
5988  */
5989 static int airo_get_rate(struct net_device *dev,
5990                          struct iw_request_info *info,
5991                          struct iw_param *vwrq,
5992                          char *extra)
5993 {
5994         struct airo_info *local = dev->priv;
5995         StatusRid status_rid;           /* Card status info */
5996
5997         readStatusRid(local, &status_rid, 1);
5998
5999         vwrq->value = status_rid.currentXmitRate * 500000;
6000         /* If more than one rate, set auto */
6001         readConfigRid(local, 1);
6002         vwrq->fixed = (local->config.rates[1] == 0);
6003
6004         return 0;
6005 }
6006
6007 /*------------------------------------------------------------------*/
6008 /*
6009  * Wireless Handler : set RTS threshold
6010  */
6011 static int airo_set_rts(struct net_device *dev,
6012                         struct iw_request_info *info,
6013                         struct iw_param *vwrq,
6014                         char *extra)
6015 {
6016         struct airo_info *local = dev->priv;
6017         int rthr = vwrq->value;
6018
6019         if(vwrq->disabled)
6020                 rthr = 2312;
6021         if((rthr < 0) || (rthr > 2312)) {
6022                 return -EINVAL;
6023         }
6024         readConfigRid(local, 1);
6025         local->config.rtsThres = rthr;
6026         set_bit (FLAG_COMMIT, &local->flags);
6027
6028         return -EINPROGRESS;            /* Call commit handler */
6029 }
6030
6031 /*------------------------------------------------------------------*/
6032 /*
6033  * Wireless Handler : get RTS threshold
6034  */
6035 static int airo_get_rts(struct net_device *dev,
6036                         struct iw_request_info *info,
6037                         struct iw_param *vwrq,
6038                         char *extra)
6039 {
6040         struct airo_info *local = dev->priv;
6041
6042         readConfigRid(local, 1);
6043         vwrq->value = local->config.rtsThres;
6044         vwrq->disabled = (vwrq->value >= 2312);
6045         vwrq->fixed = 1;
6046
6047         return 0;
6048 }
6049
6050 /*------------------------------------------------------------------*/
6051 /*
6052  * Wireless Handler : set Fragmentation threshold
6053  */
6054 static int airo_set_frag(struct net_device *dev,
6055                          struct iw_request_info *info,
6056                          struct iw_param *vwrq,
6057                          char *extra)
6058 {
6059         struct airo_info *local = dev->priv;
6060         int fthr = vwrq->value;
6061
6062         if(vwrq->disabled)
6063                 fthr = 2312;
6064         if((fthr < 256) || (fthr > 2312)) {
6065                 return -EINVAL;
6066         }
6067         fthr &= ~0x1;   /* Get an even value - is it really needed ??? */
6068         readConfigRid(local, 1);
6069         local->config.fragThresh = (u16)fthr;
6070         set_bit (FLAG_COMMIT, &local->flags);
6071
6072         return -EINPROGRESS;            /* Call commit handler */
6073 }
6074
6075 /*------------------------------------------------------------------*/
6076 /*
6077  * Wireless Handler : get Fragmentation threshold
6078  */
6079 static int airo_get_frag(struct net_device *dev,
6080                          struct iw_request_info *info,
6081                          struct iw_param *vwrq,
6082                          char *extra)
6083 {
6084         struct airo_info *local = dev->priv;
6085
6086         readConfigRid(local, 1);
6087         vwrq->value = local->config.fragThresh;
6088         vwrq->disabled = (vwrq->value >= 2312);
6089         vwrq->fixed = 1;
6090
6091         return 0;
6092 }
6093
6094 /*------------------------------------------------------------------*/
6095 /*
6096  * Wireless Handler : set Mode of Operation
6097  */
6098 static int airo_set_mode(struct net_device *dev,
6099                          struct iw_request_info *info,
6100                          __u32 *uwrq,
6101                          char *extra)
6102 {
6103         struct airo_info *local = dev->priv;
6104         int reset = 0;
6105
6106         readConfigRid(local, 1);
6107         if ((local->config.rmode & 0xff) >= RXMODE_RFMON)
6108                 reset = 1;
6109
6110         switch(*uwrq) {
6111                 case IW_MODE_ADHOC:
6112                         local->config.opmode &= 0xFF00;
6113                         local->config.opmode |= MODE_STA_IBSS;
6114                         local->config.rmode &= 0xfe00;
6115                         local->config.scanMode = SCANMODE_ACTIVE;
6116                         clear_bit (FLAG_802_11, &local->flags);
6117                         break;
6118                 case IW_MODE_INFRA:
6119                         local->config.opmode &= 0xFF00;
6120                         local->config.opmode |= MODE_STA_ESS;
6121                         local->config.rmode &= 0xfe00;
6122                         local->config.scanMode = SCANMODE_ACTIVE;
6123                         clear_bit (FLAG_802_11, &local->flags);
6124                         break;
6125                 case IW_MODE_MASTER:
6126                         local->config.opmode &= 0xFF00;
6127                         local->config.opmode |= MODE_AP;
6128                         local->config.rmode &= 0xfe00;
6129                         local->config.scanMode = SCANMODE_ACTIVE;
6130                         clear_bit (FLAG_802_11, &local->flags);
6131                         break;
6132                 case IW_MODE_REPEAT:
6133                         local->config.opmode &= 0xFF00;
6134                         local->config.opmode |= MODE_AP_RPTR;
6135                         local->config.rmode &= 0xfe00;
6136                         local->config.scanMode = SCANMODE_ACTIVE;
6137                         clear_bit (FLAG_802_11, &local->flags);
6138                         break;
6139                 case IW_MODE_MONITOR:
6140                         local->config.opmode &= 0xFF00;
6141                         local->config.opmode |= MODE_STA_ESS;
6142                         local->config.rmode &= 0xfe00;
6143                         local->config.rmode |= RXMODE_RFMON | RXMODE_DISABLE_802_3_HEADER;
6144                         local->config.scanMode = SCANMODE_PASSIVE;
6145                         set_bit (FLAG_802_11, &local->flags);
6146                         break;
6147                 default:
6148                         return -EINVAL;
6149         }
6150         if (reset)
6151                 set_bit (FLAG_RESET, &local->flags);
6152         set_bit (FLAG_COMMIT, &local->flags);
6153
6154         return -EINPROGRESS;            /* Call commit handler */
6155 }
6156
6157 /*------------------------------------------------------------------*/
6158 /*
6159  * Wireless Handler : get Mode of Operation
6160  */
6161 static int airo_get_mode(struct net_device *dev,
6162                          struct iw_request_info *info,
6163                          __u32 *uwrq,
6164                          char *extra)
6165 {
6166         struct airo_info *local = dev->priv;
6167
6168         readConfigRid(local, 1);
6169         /* If not managed, assume it's ad-hoc */
6170         switch (local->config.opmode & 0xFF) {
6171                 case MODE_STA_ESS:
6172                         *uwrq = IW_MODE_INFRA;
6173                         break;
6174                 case MODE_AP:
6175                         *uwrq = IW_MODE_MASTER;
6176                         break;
6177                 case MODE_AP_RPTR:
6178                         *uwrq = IW_MODE_REPEAT;
6179                         break;
6180                 default:
6181                         *uwrq = IW_MODE_ADHOC;
6182         }
6183
6184         return 0;
6185 }
6186
6187 /*------------------------------------------------------------------*/
6188 /*
6189  * Wireless Handler : set Encryption Key
6190  */
6191 static int airo_set_encode(struct net_device *dev,
6192                            struct iw_request_info *info,
6193                            struct iw_point *dwrq,
6194                            char *extra)
6195 {
6196         struct airo_info *local = dev->priv;
6197         CapabilityRid cap_rid;          /* Card capability info */
6198
6199         /* Is WEP supported ? */
6200         readCapabilityRid(local, &cap_rid, 1);
6201         /* Older firmware doesn't support this...
6202         if(!(cap_rid.softCap & 2)) {
6203                 return -EOPNOTSUPP;
6204         } */
6205         readConfigRid(local, 1);
6206
6207         /* Basic checking: do we have a key to set ?
6208          * Note : with the new API, it's impossible to get a NULL pointer.
6209          * Therefore, we need to check a key size == 0 instead.
6210          * New version of iwconfig properly set the IW_ENCODE_NOKEY flag
6211          * when no key is present (only change flags), but older versions
6212          * don't do it. - Jean II */
6213         if (dwrq->length > 0) {
6214                 wep_key_t key;
6215                 int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
6216                 int current_index = get_wep_key(local, 0xffff);
6217                 /* Check the size of the key */
6218                 if (dwrq->length > MAX_KEY_SIZE) {
6219                         return -EINVAL;
6220                 }
6221                 /* Check the index (none -> use current) */
6222                 if ((index < 0) || (index >= ((cap_rid.softCap & 0x80) ? 4:1)))
6223                         index = current_index;
6224                 /* Set the length */
6225                 if (dwrq->length > MIN_KEY_SIZE)
6226                         key.len = MAX_KEY_SIZE;
6227                 else
6228                         if (dwrq->length > 0)
6229                                 key.len = MIN_KEY_SIZE;
6230                         else
6231                                 /* Disable the key */
6232                                 key.len = 0;
6233                 /* Check if the key is not marked as invalid */
6234                 if(!(dwrq->flags & IW_ENCODE_NOKEY)) {
6235                         /* Cleanup */
6236                         memset(key.key, 0, MAX_KEY_SIZE);
6237                         /* Copy the key in the driver */
6238                         memcpy(key.key, extra, dwrq->length);
6239                         /* Send the key to the card */
6240                         set_wep_key(local, index, key.key, key.len, 1, 1);
6241                 }
6242                 /* WE specify that if a valid key is set, encryption
6243                  * should be enabled (user may turn it off later)
6244                  * This is also how "iwconfig ethX key on" works */
6245                 if((index == current_index) && (key.len > 0) &&
6246                    (local->config.authType == AUTH_OPEN)) {
6247                         local->config.authType = AUTH_ENCRYPT;
6248                         set_bit (FLAG_COMMIT, &local->flags);
6249                 }
6250         } else {
6251                 /* Do we want to just set the transmit key index ? */
6252                 int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
6253                 if ((index >= 0) && (index < ((cap_rid.softCap & 0x80)?4:1))) {
6254                         set_wep_key(local, index, NULL, 0, 1, 1);
6255                 } else
6256                         /* Don't complain if only change the mode */
6257                         if(!dwrq->flags & IW_ENCODE_MODE) {
6258                                 return -EINVAL;
6259                         }
6260         }
6261         /* Read the flags */
6262         if(dwrq->flags & IW_ENCODE_DISABLED)
6263                 local->config.authType = AUTH_OPEN;     // disable encryption
6264         if(dwrq->flags & IW_ENCODE_RESTRICTED)
6265                 local->config.authType = AUTH_SHAREDKEY;        // Only Both
6266         if(dwrq->flags & IW_ENCODE_OPEN)
6267                 local->config.authType = AUTH_ENCRYPT;  // Only Wep
6268         /* Commit the changes to flags if needed */
6269         if(dwrq->flags & IW_ENCODE_MODE)
6270                 set_bit (FLAG_COMMIT, &local->flags);
6271         return -EINPROGRESS;            /* Call commit handler */
6272 }
6273
6274 /*------------------------------------------------------------------*/
6275 /*
6276  * Wireless Handler : get Encryption Key
6277  */
6278 static int airo_get_encode(struct net_device *dev,
6279                            struct iw_request_info *info,
6280                            struct iw_point *dwrq,
6281                            char *extra)
6282 {
6283         struct airo_info *local = dev->priv;
6284         int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
6285         CapabilityRid cap_rid;          /* Card capability info */
6286
6287         /* Is it supported ? */
6288         readCapabilityRid(local, &cap_rid, 1);
6289         if(!(cap_rid.softCap & 2)) {
6290                 return -EOPNOTSUPP;
6291         }
6292         readConfigRid(local, 1);
6293         /* Check encryption mode */
6294         switch(local->config.authType)  {
6295                 case AUTH_ENCRYPT:
6296                         dwrq->flags = IW_ENCODE_OPEN;
6297                         break;
6298                 case AUTH_SHAREDKEY:
6299                         dwrq->flags = IW_ENCODE_RESTRICTED;
6300                         break;
6301                 default:
6302                 case AUTH_OPEN:
6303                         dwrq->flags = IW_ENCODE_DISABLED;
6304                         break;
6305         }
6306         /* We can't return the key, so set the proper flag and return zero */
6307         dwrq->flags |= IW_ENCODE_NOKEY;
6308         memset(extra, 0, 16);
6309
6310         /* Which key do we want ? -1 -> tx index */
6311         if ((index < 0) || (index >= ((cap_rid.softCap & 0x80) ? 4 : 1)))
6312                 index = get_wep_key(local, 0xffff);
6313         dwrq->flags |= index + 1;
6314         /* Copy the key to the user buffer */
6315         dwrq->length = get_wep_key(local, index);
6316         if (dwrq->length > 16) {
6317                 dwrq->length=0;
6318         }
6319         return 0;
6320 }
6321
6322 /*------------------------------------------------------------------*/
6323 /*
6324  * Wireless Handler : set Tx-Power
6325  */
6326 static int airo_set_txpow(struct net_device *dev,
6327                           struct iw_request_info *info,
6328                           struct iw_param *vwrq,
6329                           char *extra)
6330 {
6331         struct airo_info *local = dev->priv;
6332         CapabilityRid cap_rid;          /* Card capability info */
6333         int i;
6334         int rc = -EINVAL;
6335
6336         readCapabilityRid(local, &cap_rid, 1);
6337
6338         if (vwrq->disabled) {
6339                 set_bit (FLAG_RADIO_OFF, &local->flags);
6340                 set_bit (FLAG_COMMIT, &local->flags);
6341                 return -EINPROGRESS;            /* Call commit handler */
6342         }
6343         if (vwrq->flags != IW_TXPOW_MWATT) {
6344                 return -EINVAL;
6345         }
6346         clear_bit (FLAG_RADIO_OFF, &local->flags);
6347         for (i = 0; cap_rid.txPowerLevels[i] && (i < 8); i++)
6348                 if ((vwrq->value==cap_rid.txPowerLevels[i])) {
6349                         readConfigRid(local, 1);
6350                         local->config.txPower = vwrq->value;
6351                         set_bit (FLAG_COMMIT, &local->flags);
6352                         rc = -EINPROGRESS;      /* Call commit handler */
6353                         break;
6354                 }
6355         return rc;
6356 }
6357
6358 /*------------------------------------------------------------------*/
6359 /*
6360  * Wireless Handler : get Tx-Power
6361  */
6362 static int airo_get_txpow(struct net_device *dev,
6363                           struct iw_request_info *info,
6364                           struct iw_param *vwrq,
6365                           char *extra)
6366 {
6367         struct airo_info *local = dev->priv;
6368
6369         readConfigRid(local, 1);
6370         vwrq->value = local->config.txPower;
6371         vwrq->fixed = 1;        /* No power control */
6372         vwrq->disabled = test_bit(FLAG_RADIO_OFF, &local->flags);
6373         vwrq->flags = IW_TXPOW_MWATT;
6374
6375         return 0;
6376 }
6377
6378 /*------------------------------------------------------------------*/
6379 /*
6380  * Wireless Handler : set Retry limits
6381  */
6382 static int airo_set_retry(struct net_device *dev,
6383                           struct iw_request_info *info,
6384                           struct iw_param *vwrq,
6385                           char *extra)
6386 {
6387         struct airo_info *local = dev->priv;
6388         int rc = -EINVAL;
6389
6390         if(vwrq->disabled) {
6391                 return -EINVAL;
6392         }
6393         readConfigRid(local, 1);
6394         if(vwrq->flags & IW_RETRY_LIMIT) {
6395                 if(vwrq->flags & IW_RETRY_MAX)
6396                         local->config.longRetryLimit = vwrq->value;
6397                 else if (vwrq->flags & IW_RETRY_MIN)
6398                         local->config.shortRetryLimit = vwrq->value;
6399                 else {
6400                         /* No modifier : set both */
6401                         local->config.longRetryLimit = vwrq->value;
6402                         local->config.shortRetryLimit = vwrq->value;
6403                 }
6404                 set_bit (FLAG_COMMIT, &local->flags);
6405                 rc = -EINPROGRESS;              /* Call commit handler */
6406         }
6407         if(vwrq->flags & IW_RETRY_LIFETIME) {
6408                 local->config.txLifetime = vwrq->value / 1024;
6409                 set_bit (FLAG_COMMIT, &local->flags);
6410                 rc = -EINPROGRESS;              /* Call commit handler */
6411         }
6412         return rc;
6413 }
6414
6415 /*------------------------------------------------------------------*/
6416 /*
6417  * Wireless Handler : get Retry limits
6418  */
6419 static int airo_get_retry(struct net_device *dev,
6420                           struct iw_request_info *info,
6421                           struct iw_param *vwrq,
6422                           char *extra)
6423 {
6424         struct airo_info *local = dev->priv;
6425
6426         vwrq->disabled = 0;      /* Can't be disabled */
6427
6428         readConfigRid(local, 1);
6429         /* Note : by default, display the min retry number */
6430         if((vwrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
6431                 vwrq->flags = IW_RETRY_LIFETIME;
6432                 vwrq->value = (int)local->config.txLifetime * 1024;
6433         } else if((vwrq->flags & IW_RETRY_MAX)) {
6434                 vwrq->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
6435                 vwrq->value = (int)local->config.longRetryLimit;
6436         } else {
6437                 vwrq->flags = IW_RETRY_LIMIT;
6438                 vwrq->value = (int)local->config.shortRetryLimit;
6439                 if((int)local->config.shortRetryLimit != (int)local->config.longRetryLimit)
6440                         vwrq->flags |= IW_RETRY_MIN;
6441         }
6442
6443         return 0;
6444 }
6445
6446 /*------------------------------------------------------------------*/
6447 /*
6448  * Wireless Handler : get range info
6449  */
6450 static int airo_get_range(struct net_device *dev,
6451                           struct iw_request_info *info,
6452                           struct iw_point *dwrq,
6453                           char *extra)
6454 {
6455         struct airo_info *local = dev->priv;
6456         struct iw_range *range = (struct iw_range *) extra;
6457         CapabilityRid cap_rid;          /* Card capability info */
6458         int             i;
6459         int             k;
6460
6461         readCapabilityRid(local, &cap_rid, 1);
6462
6463         dwrq->length = sizeof(struct iw_range);
6464         memset(range, 0, sizeof(*range));
6465         range->min_nwid = 0x0000;
6466         range->max_nwid = 0x0000;
6467         range->num_channels = 14;
6468         /* Should be based on cap_rid.country to give only
6469          * what the current card support */
6470         k = 0;
6471         for(i = 0; i < 14; i++) {
6472                 range->freq[k].i = i + 1; /* List index */
6473                 range->freq[k].m = frequency_list[i] * 100000;
6474                 range->freq[k++].e = 1; /* Values in table in MHz -> * 10^5 * 10 */
6475         }
6476         range->num_frequency = k;
6477
6478         range->sensitivity = 65535;
6479
6480         /* Hum... Should put the right values there */
6481         if (local->rssi)
6482                 range->max_qual.qual = 100;     /* % */
6483         else
6484                 range->max_qual.qual = airo_get_max_quality(&cap_rid);
6485         range->max_qual.level = 0;      /* 0 means we use dBm  */
6486         range->max_qual.noise = 0;
6487         range->max_qual.updated = 0;
6488
6489         /* Experimental measurements - boundary 11/5.5 Mb/s */
6490         /* Note : with or without the (local->rssi), results
6491          * are somewhat different. - Jean II */
6492         if (local->rssi) {
6493                 range->avg_qual.qual = 50;      /* % */
6494                 range->avg_qual.level = 186;    /* -70 dBm */
6495         } else {
6496                 range->avg_qual.qual = airo_get_avg_quality(&cap_rid);
6497                 range->avg_qual.level = 176;    /* -80 dBm */
6498         }
6499         range->avg_qual.noise = 0;
6500         range->avg_qual.updated = 0;
6501
6502         for(i = 0 ; i < 8 ; i++) {
6503                 range->bitrate[i] = cap_rid.supportedRates[i] * 500000;
6504                 if(range->bitrate[i] == 0)
6505                         break;
6506         }
6507         range->num_bitrates = i;
6508
6509         /* Set an indication of the max TCP throughput
6510          * in bit/s that we can expect using this interface.
6511          * May be use for QoS stuff... Jean II */
6512         if(i > 2)
6513                 range->throughput = 5000 * 1000;
6514         else
6515                 range->throughput = 1500 * 1000;
6516
6517         range->min_rts = 0;
6518         range->max_rts = 2312;
6519         range->min_frag = 256;
6520         range->max_frag = 2312;
6521
6522         if(cap_rid.softCap & 2) {
6523                 // WEP: RC4 40 bits
6524                 range->encoding_size[0] = 5;
6525                 // RC4 ~128 bits
6526                 if (cap_rid.softCap & 0x100) {
6527                         range->encoding_size[1] = 13;
6528                         range->num_encoding_sizes = 2;
6529                 } else
6530                         range->num_encoding_sizes = 1;
6531                 range->max_encoding_tokens = (cap_rid.softCap & 0x80) ? 4 : 1;
6532         } else {
6533                 range->num_encoding_sizes = 0;
6534                 range->max_encoding_tokens = 0;
6535         }
6536         range->min_pmp = 0;
6537         range->max_pmp = 5000000;       /* 5 secs */
6538         range->min_pmt = 0;
6539         range->max_pmt = 65535 * 1024;  /* ??? */
6540         range->pmp_flags = IW_POWER_PERIOD;
6541         range->pmt_flags = IW_POWER_TIMEOUT;
6542         range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_ALL_R;
6543
6544         /* Transmit Power - values are in mW */
6545         for(i = 0 ; i < 8 ; i++) {
6546                 range->txpower[i] = cap_rid.txPowerLevels[i];
6547                 if(range->txpower[i] == 0)
6548                         break;
6549         }
6550         range->num_txpower = i;
6551         range->txpower_capa = IW_TXPOW_MWATT;
6552         range->we_version_source = 12;
6553         range->we_version_compiled = WIRELESS_EXT;
6554         range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
6555         range->retry_flags = IW_RETRY_LIMIT;
6556         range->r_time_flags = IW_RETRY_LIFETIME;
6557         range->min_retry = 1;
6558         range->max_retry = 65535;
6559         range->min_r_time = 1024;
6560         range->max_r_time = 65535 * 1024;
6561
6562         /* Event capability (kernel + driver) */
6563         range->event_capa[0] = (IW_EVENT_CAPA_K_0 |
6564                                 IW_EVENT_CAPA_MASK(SIOCGIWTHRSPY) |
6565                                 IW_EVENT_CAPA_MASK(SIOCGIWAP) |
6566                                 IW_EVENT_CAPA_MASK(SIOCGIWSCAN));
6567         range->event_capa[1] = IW_EVENT_CAPA_K_1;
6568         range->event_capa[4] = IW_EVENT_CAPA_MASK(IWEVTXDROP);
6569         return 0;
6570 }
6571
6572 /*------------------------------------------------------------------*/
6573 /*
6574  * Wireless Handler : set Power Management
6575  */
6576 static int airo_set_power(struct net_device *dev,
6577                           struct iw_request_info *info,
6578                           struct iw_param *vwrq,
6579                           char *extra)
6580 {
6581         struct airo_info *local = dev->priv;
6582
6583         readConfigRid(local, 1);
6584         if (vwrq->disabled) {
6585                 if ((local->config.rmode & 0xFF) >= RXMODE_RFMON) {
6586                         return -EINVAL;
6587                 }
6588                 local->config.powerSaveMode = POWERSAVE_CAM;
6589                 local->config.rmode &= 0xFF00;
6590                 local->config.rmode |= RXMODE_BC_MC_ADDR;
6591                 set_bit (FLAG_COMMIT, &local->flags);
6592                 return -EINPROGRESS;            /* Call commit handler */
6593         }
6594         if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
6595                 local->config.fastListenDelay = (vwrq->value + 500) / 1024;
6596                 local->config.powerSaveMode = POWERSAVE_PSPCAM;
6597                 set_bit (FLAG_COMMIT, &local->flags);
6598         } else if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_PERIOD) {
6599                 local->config.fastListenInterval = local->config.listenInterval = (vwrq->value + 500) / 1024;
6600                 local->config.powerSaveMode = POWERSAVE_PSPCAM;
6601                 set_bit (FLAG_COMMIT, &local->flags);
6602         }
6603         switch (vwrq->flags & IW_POWER_MODE) {
6604                 case IW_POWER_UNICAST_R:
6605                         if ((local->config.rmode & 0xFF) >= RXMODE_RFMON) {
6606                                 return -EINVAL;
6607                         }
6608                         local->config.rmode &= 0xFF00;
6609                         local->config.rmode |= RXMODE_ADDR;
6610                         set_bit (FLAG_COMMIT, &local->flags);
6611                         break;
6612                 case IW_POWER_ALL_R:
6613                         if ((local->config.rmode & 0xFF) >= RXMODE_RFMON) {
6614                                 return -EINVAL;
6615                         }
6616                         local->config.rmode &= 0xFF00;
6617                         local->config.rmode |= RXMODE_BC_MC_ADDR;
6618                         set_bit (FLAG_COMMIT, &local->flags);
6619                 case IW_POWER_ON:
6620                         break;
6621                 default:
6622                         return -EINVAL;
6623         }
6624         // Note : we may want to factor local->need_commit here
6625         // Note2 : may also want to factor RXMODE_RFMON test
6626         return -EINPROGRESS;            /* Call commit handler */
6627 }
6628
6629 /*------------------------------------------------------------------*/
6630 /*
6631  * Wireless Handler : get Power Management
6632  */
6633 static int airo_get_power(struct net_device *dev,
6634                           struct iw_request_info *info,
6635                           struct iw_param *vwrq,
6636                           char *extra)
6637 {
6638         struct airo_info *local = dev->priv;
6639         int mode;
6640
6641         readConfigRid(local, 1);
6642         mode = local->config.powerSaveMode;
6643         if ((vwrq->disabled = (mode == POWERSAVE_CAM)))
6644                 return 0;
6645         if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
6646                 vwrq->value = (int)local->config.fastListenDelay * 1024;
6647                 vwrq->flags = IW_POWER_TIMEOUT;
6648         } else {
6649                 vwrq->value = (int)local->config.fastListenInterval * 1024;
6650                 vwrq->flags = IW_POWER_PERIOD;
6651         }
6652         if ((local->config.rmode & 0xFF) == RXMODE_ADDR)
6653                 vwrq->flags |= IW_POWER_UNICAST_R;
6654         else
6655                 vwrq->flags |= IW_POWER_ALL_R;
6656
6657         return 0;
6658 }
6659
6660 /*------------------------------------------------------------------*/
6661 /*
6662  * Wireless Handler : set Sensitivity
6663  */
6664 static int airo_set_sens(struct net_device *dev,
6665                          struct iw_request_info *info,
6666                          struct iw_param *vwrq,
6667                          char *extra)
6668 {
6669         struct airo_info *local = dev->priv;
6670
6671         readConfigRid(local, 1);
6672         local->config.rssiThreshold = vwrq->disabled ? RSSI_DEFAULT : vwrq->value;
6673         set_bit (FLAG_COMMIT, &local->flags);
6674
6675         return -EINPROGRESS;            /* Call commit handler */
6676 }
6677
6678 /*------------------------------------------------------------------*/
6679 /*
6680  * Wireless Handler : get Sensitivity
6681  */
6682 static int airo_get_sens(struct net_device *dev,
6683                          struct iw_request_info *info,
6684                          struct iw_param *vwrq,
6685                          char *extra)
6686 {
6687         struct airo_info *local = dev->priv;
6688
6689         readConfigRid(local, 1);
6690         vwrq->value = local->config.rssiThreshold;
6691         vwrq->disabled = (vwrq->value == 0);
6692         vwrq->fixed = 1;
6693
6694         return 0;
6695 }
6696
6697 /*------------------------------------------------------------------*/
6698 /*
6699  * Wireless Handler : get AP List
6700  * Note : this is deprecated in favor of IWSCAN
6701  */
6702 static int airo_get_aplist(struct net_device *dev,
6703                            struct iw_request_info *info,
6704                            struct iw_point *dwrq,
6705                            char *extra)
6706 {
6707         struct airo_info *local = dev->priv;
6708         struct sockaddr *address = (struct sockaddr *) extra;
6709         struct iw_quality qual[IW_MAX_AP];
6710         BSSListRid BSSList;
6711         int i;
6712         int loseSync = capable(CAP_NET_ADMIN) ? 1: -1;
6713
6714         for (i = 0; i < IW_MAX_AP; i++) {
6715                 if (readBSSListRid(local, loseSync, &BSSList))
6716                         break;
6717                 loseSync = 0;
6718                 memcpy(address[i].sa_data, BSSList.bssid, ETH_ALEN);
6719                 address[i].sa_family = ARPHRD_ETHER;
6720                 if (local->rssi) {
6721                         qual[i].level = 0x100 - BSSList.dBm;
6722                         qual[i].qual = airo_dbm_to_pct( local->rssi, BSSList.dBm );
6723                         qual[i].updated = IW_QUAL_QUAL_UPDATED;
6724                 } else {
6725                         qual[i].level = (BSSList.dBm + 321) / 2;
6726                         qual[i].qual = 0;
6727                         qual[i].updated = IW_QUAL_QUAL_INVALID;
6728                 }
6729                 qual[i].noise = local->wstats.qual.noise;
6730                 qual[i].updated = IW_QUAL_LEVEL_UPDATED
6731                                 | IW_QUAL_NOISE_UPDATED;
6732                 if (BSSList.index == 0xffff)
6733                         break;
6734         }
6735         if (!i) {
6736                 StatusRid status_rid;           /* Card status info */
6737                 readStatusRid(local, &status_rid, 1);
6738                 for (i = 0;
6739                      i < min(IW_MAX_AP, 4) &&
6740                              (status_rid.bssid[i][0]
6741                               & status_rid.bssid[i][1]
6742                               & status_rid.bssid[i][2]
6743                               & status_rid.bssid[i][3]
6744                               & status_rid.bssid[i][4]
6745                               & status_rid.bssid[i][5])!=0xff &&
6746                              (status_rid.bssid[i][0]
6747                               | status_rid.bssid[i][1]
6748                               | status_rid.bssid[i][2]
6749                               | status_rid.bssid[i][3]
6750                               | status_rid.bssid[i][4]
6751                               | status_rid.bssid[i][5]);
6752                      i++) {
6753                         memcpy(address[i].sa_data,
6754                                status_rid.bssid[i], ETH_ALEN);
6755                         address[i].sa_family = ARPHRD_ETHER;
6756                 }
6757         } else {
6758                 dwrq->flags = 1; /* Should be define'd */
6759                 memcpy(extra + sizeof(struct sockaddr)*i,
6760                        &qual,  sizeof(struct iw_quality)*i);
6761         }
6762         dwrq->length = i;
6763
6764         return 0;
6765 }
6766
6767 /*------------------------------------------------------------------*/
6768 /*
6769  * Wireless Handler : Initiate Scan
6770  */
6771 static int airo_set_scan(struct net_device *dev,
6772                          struct iw_request_info *info,
6773                          struct iw_param *vwrq,
6774                          char *extra)
6775 {
6776         struct airo_info *ai = dev->priv;
6777         Cmd cmd;
6778         Resp rsp;
6779
6780         /* Note : you may have realised that, as this is a SET operation,
6781          * this is privileged and therefore a normal user can't
6782          * perform scanning.
6783          * This is not an error, while the device perform scanning,
6784          * traffic doesn't flow, so it's a perfect DoS...
6785          * Jean II */
6786         if (ai->flags & FLAG_RADIO_MASK) return -ENETDOWN;
6787
6788         /* Initiate a scan command */
6789         memset(&cmd, 0, sizeof(cmd));
6790         cmd.cmd=CMD_LISTBSS;
6791         if (down_interruptible(&ai->sem))
6792                 return -ERESTARTSYS;
6793         issuecommand(ai, &cmd, &rsp);
6794         ai->scan_timestamp = jiffies;
6795         up(&ai->sem);
6796
6797         /* At this point, just return to the user. */
6798
6799         return 0;
6800 }
6801
6802 /*------------------------------------------------------------------*/
6803 /*
6804  * Translate scan data returned from the card to a card independent
6805  * format that the Wireless Tools will understand - Jean II
6806  */
6807 static inline char *airo_translate_scan(struct net_device *dev,
6808                                         char *current_ev,
6809                                         char *end_buf,
6810                                         BSSListRid *bss)
6811 {
6812         struct airo_info *ai = dev->priv;
6813         struct iw_event         iwe;            /* Temporary buffer */
6814         u16                     capabilities;
6815         char *                  current_val;    /* For rates */
6816         int                     i;
6817
6818         /* First entry *MUST* be the AP MAC address */
6819         iwe.cmd = SIOCGIWAP;
6820         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
6821         memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
6822         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_ADDR_LEN);
6823
6824         /* Other entries will be displayed in the order we give them */
6825
6826         /* Add the ESSID */
6827         iwe.u.data.length = bss->ssidLen;
6828         if(iwe.u.data.length > 32)
6829                 iwe.u.data.length = 32;
6830         iwe.cmd = SIOCGIWESSID;
6831         iwe.u.data.flags = 1;
6832         current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, bss->ssid);
6833
6834         /* Add mode */
6835         iwe.cmd = SIOCGIWMODE;
6836         capabilities = le16_to_cpu(bss->cap);
6837         if(capabilities & (CAP_ESS | CAP_IBSS)) {
6838                 if(capabilities & CAP_ESS)
6839                         iwe.u.mode = IW_MODE_MASTER;
6840                 else
6841                         iwe.u.mode = IW_MODE_ADHOC;
6842                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_UINT_LEN);
6843         }
6844
6845         /* Add frequency */
6846         iwe.cmd = SIOCGIWFREQ;
6847         iwe.u.freq.m = le16_to_cpu(bss->dsChannel);
6848         iwe.u.freq.m = frequency_list[iwe.u.freq.m] * 100000;
6849         iwe.u.freq.e = 1;
6850         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_FREQ_LEN);
6851
6852         /* Add quality statistics */
6853         iwe.cmd = IWEVQUAL;
6854         if (ai->rssi) {
6855                 iwe.u.qual.level = 0x100 - bss->dBm;
6856                 iwe.u.qual.qual = airo_dbm_to_pct( ai->rssi, bss->dBm );
6857                 iwe.u.qual.updated = IW_QUAL_QUAL_UPDATED;
6858         } else {
6859                 iwe.u.qual.level = (bss->dBm + 321) / 2;
6860                 iwe.u.qual.qual = 0;
6861                 iwe.u.qual.updated = IW_QUAL_QUAL_INVALID;
6862         }
6863         iwe.u.qual.noise = ai->wstats.qual.noise;
6864         iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED
6865                         | IW_QUAL_NOISE_UPDATED;
6866         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);
6867
6868         /* Add encryption capability */
6869         iwe.cmd = SIOCGIWENCODE;
6870         if(capabilities & CAP_PRIVACY)
6871                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
6872         else
6873                 iwe.u.data.flags = IW_ENCODE_DISABLED;
6874         iwe.u.data.length = 0;
6875         current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, bss->ssid);
6876
6877         /* Rate : stuffing multiple values in a single event require a bit
6878          * more of magic - Jean II */
6879         current_val = current_ev + IW_EV_LCP_LEN;
6880
6881         iwe.cmd = SIOCGIWRATE;
6882         /* Those two flags are ignored... */
6883         iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
6884         /* Max 8 values */
6885         for(i = 0 ; i < 8 ; i++) {
6886                 /* NULL terminated */
6887                 if(bss->rates[i] == 0)
6888                         break;
6889                 /* Bit rate given in 500 kb/s units (+ 0x80) */
6890                 iwe.u.bitrate.value = ((bss->rates[i] & 0x7f) * 500000);
6891                 /* Add new value to event */
6892                 current_val = iwe_stream_add_value(current_ev, current_val, end_buf, &iwe, IW_EV_PARAM_LEN);
6893         }
6894         /* Check if we added any event */
6895         if((current_val - current_ev) > IW_EV_LCP_LEN)
6896                 current_ev = current_val;
6897
6898         /* The other data in the scan result are not really
6899          * interesting, so for now drop it - Jean II */
6900         return current_ev;
6901 }
6902
6903 /*------------------------------------------------------------------*/
6904 /*
6905  * Wireless Handler : Read Scan Results
6906  */
6907 static int airo_get_scan(struct net_device *dev,
6908                          struct iw_request_info *info,
6909                          struct iw_point *dwrq,
6910                          char *extra)
6911 {
6912         struct airo_info *ai = dev->priv;
6913         BSSListRid BSSList;
6914         int rc;
6915         char *current_ev = extra;
6916
6917         /* When we are associated again, the scan has surely finished.
6918          * Just in case, let's make sure enough time has elapsed since
6919          * we started the scan. - Javier */
6920         if(ai->scan_timestamp && time_before(jiffies,ai->scan_timestamp+3*HZ)) {
6921                 /* Important note : we don't want to block the caller
6922                  * until results are ready for various reasons.
6923                  * First, managing wait queues is complex and racy
6924                  * (there may be multiple simultaneous callers).
6925                  * Second, we grab some rtnetlink lock before comming
6926                  * here (in dev_ioctl()).
6927                  * Third, the caller can wait on the Wireless Event
6928                  * - Jean II */
6929                 return -EAGAIN;
6930         }
6931         ai->scan_timestamp = 0;
6932
6933         /* There's only a race with proc_BSSList_open(), but its
6934          * consequences are begnign. So I don't bother fixing it - Javier */
6935
6936         /* Try to read the first entry of the scan result */
6937         rc = PC4500_readrid(ai, RID_BSSLISTFIRST, &BSSList, sizeof(BSSList), 1);
6938         if((rc) || (BSSList.index == 0xffff)) {
6939                 /* Client error, no scan results...
6940                  * The caller need to restart the scan. */
6941                 return -ENODATA;
6942         }
6943
6944         /* Read and parse all entries */
6945         while((!rc) && (BSSList.index != 0xffff)) {
6946                 /* Translate to WE format this entry */
6947                 current_ev = airo_translate_scan(dev, current_ev,
6948                                                  extra + dwrq->length,
6949                                                  &BSSList);
6950
6951                 /* Check if there is space for one more entry */
6952                 if((extra + dwrq->length - current_ev) <= IW_EV_ADDR_LEN) {
6953                         /* Ask user space to try again with a bigger buffer */
6954                         return -E2BIG;
6955                 }
6956
6957                 /* Read next entry */
6958                 rc = PC4500_readrid(ai, RID_BSSLISTNEXT,
6959                                     &BSSList, sizeof(BSSList), 1);
6960         }
6961         /* Length of data */
6962         dwrq->length = (current_ev - extra);
6963         dwrq->flags = 0;        /* todo */
6964
6965         return 0;
6966 }
6967
6968 /*------------------------------------------------------------------*/
6969 /*
6970  * Commit handler : called after a bunch of SET operations
6971  */
6972 static int airo_config_commit(struct net_device *dev,
6973                               struct iw_request_info *info,     /* NULL */
6974                               void *zwrq,                       /* NULL */
6975                               char *extra)                      /* NULL */
6976 {
6977         struct airo_info *local = dev->priv;
6978         Resp rsp;
6979
6980         if (!test_bit (FLAG_COMMIT, &local->flags))
6981                 return 0;
6982
6983         /* Some of the "SET" function may have modified some of the
6984          * parameters. It's now time to commit them in the card */
6985         disable_MAC(local, 1);
6986         if (test_bit (FLAG_RESET, &local->flags)) {
6987                 APListRid APList_rid;
6988                 SsidRid SSID_rid;
6989
6990                 readAPListRid(local, &APList_rid);
6991                 readSsidRid(local, &SSID_rid);
6992                 if (test_bit(FLAG_MPI,&local->flags))
6993                         setup_card(local, dev->dev_addr, 1 );
6994                 else
6995                         reset_airo_card(dev);
6996                 disable_MAC(local, 1);
6997                 writeSsidRid(local, &SSID_rid, 1);
6998                 writeAPListRid(local, &APList_rid, 1);
6999         }
7000         if (down_interruptible(&local->sem))
7001                 return -ERESTARTSYS;
7002         writeConfigRid(local, 0);
7003         enable_MAC(local, &rsp, 0);
7004         if (test_bit (FLAG_RESET, &local->flags))
7005                 airo_set_promisc(local);
7006         else
7007                 up(&local->sem);
7008
7009         return 0;
7010 }
7011
7012 /*------------------------------------------------------------------*/
7013 /*
7014  * Structures to export the Wireless Handlers
7015  */
7016
7017 static const struct iw_priv_args airo_private_args[] = {
7018 /*{ cmd,         set_args,                            get_args, name } */
7019   { AIROIOCTL, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | sizeof (aironet_ioctl),
7020     IW_PRIV_TYPE_BYTE | 2047, "airoioctl" },
7021   { AIROIDIFC, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | sizeof (aironet_ioctl),
7022     IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "airoidifc" },
7023 };
7024
7025 static const iw_handler         airo_handler[] =
7026 {
7027         (iw_handler) airo_config_commit,        /* SIOCSIWCOMMIT */
7028         (iw_handler) airo_get_name,             /* SIOCGIWNAME */
7029         (iw_handler) NULL,                      /* SIOCSIWNWID */
7030         (iw_handler) NULL,                      /* SIOCGIWNWID */
7031         (iw_handler) airo_set_freq,             /* SIOCSIWFREQ */
7032         (iw_handler) airo_get_freq,             /* SIOCGIWFREQ */
7033         (iw_handler) airo_set_mode,             /* SIOCSIWMODE */
7034         (iw_handler) airo_get_mode,             /* SIOCGIWMODE */
7035         (iw_handler) airo_set_sens,             /* SIOCSIWSENS */
7036         (iw_handler) airo_get_sens,             /* SIOCGIWSENS */
7037         (iw_handler) NULL,                      /* SIOCSIWRANGE */
7038         (iw_handler) airo_get_range,            /* SIOCGIWRANGE */
7039         (iw_handler) NULL,                      /* SIOCSIWPRIV */
7040         (iw_handler) NULL,                      /* SIOCGIWPRIV */
7041         (iw_handler) NULL,                      /* SIOCSIWSTATS */
7042         (iw_handler) NULL,                      /* SIOCGIWSTATS */
7043         iw_handler_set_spy,                     /* SIOCSIWSPY */
7044         iw_handler_get_spy,                     /* SIOCGIWSPY */
7045         iw_handler_set_thrspy,                  /* SIOCSIWTHRSPY */
7046         iw_handler_get_thrspy,                  /* SIOCGIWTHRSPY */
7047         (iw_handler) airo_set_wap,              /* SIOCSIWAP */
7048         (iw_handler) airo_get_wap,              /* SIOCGIWAP */
7049         (iw_handler) NULL,                      /* -- hole -- */
7050         (iw_handler) airo_get_aplist,           /* SIOCGIWAPLIST */
7051         (iw_handler) airo_set_scan,             /* SIOCSIWSCAN */
7052         (iw_handler) airo_get_scan,             /* SIOCGIWSCAN */
7053         (iw_handler) airo_set_essid,            /* SIOCSIWESSID */
7054         (iw_handler) airo_get_essid,            /* SIOCGIWESSID */
7055         (iw_handler) airo_set_nick,             /* SIOCSIWNICKN */
7056         (iw_handler) airo_get_nick,             /* SIOCGIWNICKN */
7057         (iw_handler) NULL,                      /* -- hole -- */
7058         (iw_handler) NULL,                      /* -- hole -- */
7059         (iw_handler) airo_set_rate,             /* SIOCSIWRATE */
7060         (iw_handler) airo_get_rate,             /* SIOCGIWRATE */
7061         (iw_handler) airo_set_rts,              /* SIOCSIWRTS */
7062         (iw_handler) airo_get_rts,              /* SIOCGIWRTS */
7063         (iw_handler) airo_set_frag,             /* SIOCSIWFRAG */
7064         (iw_handler) airo_get_frag,             /* SIOCGIWFRAG */
7065         (iw_handler) airo_set_txpow,            /* SIOCSIWTXPOW */
7066         (iw_handler) airo_get_txpow,            /* SIOCGIWTXPOW */
7067         (iw_handler) airo_set_retry,            /* SIOCSIWRETRY */
7068         (iw_handler) airo_get_retry,            /* SIOCGIWRETRY */
7069         (iw_handler) airo_set_encode,           /* SIOCSIWENCODE */
7070         (iw_handler) airo_get_encode,           /* SIOCGIWENCODE */
7071         (iw_handler) airo_set_power,            /* SIOCSIWPOWER */
7072         (iw_handler) airo_get_power,            /* SIOCGIWPOWER */
7073 };
7074
7075 /* Note : don't describe AIROIDIFC and AIROOLDIDIFC in here.
7076  * We want to force the use of the ioctl code, because those can't be
7077  * won't work the iw_handler code (because they simultaneously read
7078  * and write data and iw_handler can't do that).
7079  * Note that it's perfectly legal to read/write on a single ioctl command,
7080  * you just can't use iwpriv and need to force it via the ioctl handler.
7081  * Jean II */
7082 static const iw_handler         airo_private_handler[] =
7083 {
7084         NULL,                           /* SIOCIWFIRSTPRIV */
7085 };
7086
7087 static const struct iw_handler_def      airo_handler_def =
7088 {
7089         .num_standard   = sizeof(airo_handler)/sizeof(iw_handler),
7090         .num_private    = sizeof(airo_private_handler)/sizeof(iw_handler),
7091         .num_private_args = sizeof(airo_private_args)/sizeof(struct iw_priv_args),
7092         .standard       = airo_handler,
7093         .private        = airo_private_handler,
7094         .private_args   = airo_private_args,
7095         .get_wireless_stats = airo_get_wireless_stats,
7096 };
7097
7098 #endif /* WIRELESS_EXT */
7099
7100 /*
7101  * This defines the configuration part of the Wireless Extensions
7102  * Note : irq and spinlock protection will occur in the subroutines
7103  *
7104  * TODO :
7105  *      o Check input value more carefully and fill correct values in range
7106  *      o Test and shakeout the bugs (if any)
7107  *
7108  * Jean II
7109  *
7110  * Javier Achirica did a great job of merging code from the unnamed CISCO
7111  * developer that added support for flashing the card.
7112  */
7113 static int airo_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
7114 {
7115         int rc = 0;
7116         struct airo_info *ai = (struct airo_info *)dev->priv;
7117
7118         if (ai->power)
7119                 return 0;
7120
7121         switch (cmd) {
7122 #ifdef CISCO_EXT
7123         case AIROIDIFC:
7124 #ifdef AIROOLDIDIFC
7125         case AIROOLDIDIFC:
7126 #endif
7127         {
7128                 int val = AIROMAGIC;
7129                 aironet_ioctl com;
7130                 if (copy_from_user(&com,rq->ifr_data,sizeof(com)))
7131                         rc = -EFAULT;
7132                 else if (copy_to_user(com.data,(char *)&val,sizeof(val)))
7133                         rc = -EFAULT;
7134         }
7135         break;
7136
7137         case AIROIOCTL:
7138 #ifdef AIROOLDIOCTL
7139         case AIROOLDIOCTL:
7140 #endif
7141                 /* Get the command struct and hand it off for evaluation by
7142                  * the proper subfunction
7143                  */
7144         {
7145                 aironet_ioctl com;
7146                 if (copy_from_user(&com,rq->ifr_data,sizeof(com))) {
7147                         rc = -EFAULT;
7148                         break;
7149                 }
7150
7151                 /* Separate R/W functions bracket legality here
7152                  */
7153                 if ( com.command == AIRORSWVERSION ) {
7154                         if (copy_to_user(com.data, swversion, sizeof(swversion)))
7155                                 rc = -EFAULT;
7156                         else
7157                                 rc = 0;
7158                 }
7159                 else if ( com.command <= AIRORRID)
7160                         rc = readrids(dev,&com);
7161                 else if ( com.command >= AIROPCAP && com.command <= (AIROPLEAPUSR+2) )
7162                         rc = writerids(dev,&com);
7163                 else if ( com.command >= AIROFLSHRST && com.command <= AIRORESTART )
7164                         rc = flashcard(dev,&com);
7165                 else
7166                         rc = -EINVAL;      /* Bad command in ioctl */
7167         }
7168         break;
7169 #endif /* CISCO_EXT */
7170
7171         // All other calls are currently unsupported
7172         default:
7173                 rc = -EOPNOTSUPP;
7174         }
7175         return rc;
7176 }
7177
7178 #ifdef WIRELESS_EXT
7179 /*
7180  * Get the Wireless stats out of the driver
7181  * Note : irq and spinlock protection will occur in the subroutines
7182  *
7183  * TODO :
7184  *      o Check if work in Ad-Hoc mode (otherwise, use SPY, as in wvlan_cs)
7185  *
7186  * Jean
7187  */
7188 static void airo_read_wireless_stats(struct airo_info *local)
7189 {
7190         StatusRid status_rid;
7191         StatsRid stats_rid;
7192         CapabilityRid cap_rid;
7193         u32 *vals = stats_rid.vals;
7194
7195         /* Get stats out of the card */
7196         clear_bit(JOB_WSTATS, &local->flags);
7197         if (local->power) {
7198                 up(&local->sem);
7199                 return;
7200         }
7201         readCapabilityRid(local, &cap_rid, 0);
7202         readStatusRid(local, &status_rid, 0);
7203         readStatsRid(local, &stats_rid, RID_STATS, 0);
7204         up(&local->sem);
7205
7206         /* The status */
7207         local->wstats.status = status_rid.mode;
7208
7209         /* Signal quality and co */
7210         if (local->rssi) {
7211                 local->wstats.qual.level = airo_rssi_to_dbm( local->rssi, status_rid.sigQuality );
7212                 /* normalizedSignalStrength appears to be a percentage */
7213                 local->wstats.qual.qual = status_rid.normalizedSignalStrength;
7214         } else {
7215                 local->wstats.qual.level = (status_rid.normalizedSignalStrength + 321) / 2;
7216                 local->wstats.qual.qual = airo_get_quality(&status_rid, &cap_rid);
7217         }
7218         local->wstats.qual.updated = IW_QUAL_QUAL_UPDATED | IW_QUAL_LEVEL_UPDATED;
7219         if (status_rid.len >= 124) {
7220                 local->wstats.qual.noise = 0x100 - status_rid.noisedBm;
7221                 local->wstats.qual.updated |= IW_QUAL_NOISE_UPDATED;
7222         } else {
7223                 local->wstats.qual.noise = 0;
7224                 local->wstats.qual.updated |= IW_QUAL_NOISE_INVALID;
7225         }
7226
7227         /* Packets discarded in the wireless adapter due to wireless
7228          * specific problems */
7229         local->wstats.discard.nwid = vals[56] + vals[57] + vals[58];/* SSID Mismatch */
7230         local->wstats.discard.code = vals[6];/* RxWepErr */
7231         local->wstats.discard.fragment = vals[30];
7232         local->wstats.discard.retries = vals[10];
7233         local->wstats.discard.misc = vals[1] + vals[32];
7234         local->wstats.miss.beacon = vals[34];
7235 }
7236
7237 struct iw_statistics *airo_get_wireless_stats(struct net_device *dev)
7238 {
7239         struct airo_info *local =  dev->priv;
7240
7241         if (!test_bit(JOB_WSTATS, &local->flags)) {
7242                 /* Get stats out of the card if available */
7243                 if (down_trylock(&local->sem) != 0) {
7244                         set_bit(JOB_WSTATS, &local->flags);
7245                         wake_up_interruptible(&local->thr_wait);
7246                 } else
7247                         airo_read_wireless_stats(local);
7248         }
7249
7250         return &local->wstats;
7251 }
7252 #endif /* WIRELESS_EXT */
7253
7254 #ifdef CISCO_EXT
7255 /*
7256  * This just translates from driver IOCTL codes to the command codes to
7257  * feed to the radio's host interface. Things can be added/deleted
7258  * as needed.  This represents the READ side of control I/O to
7259  * the card
7260  */
7261 static int readrids(struct net_device *dev, aironet_ioctl *comp) {
7262         unsigned short ridcode;
7263         unsigned char *iobuf;
7264         int len;
7265         struct airo_info *ai = dev->priv;
7266         Resp rsp;
7267
7268         if (test_bit(FLAG_FLASHING, &ai->flags))
7269                 return -EIO;
7270
7271         switch(comp->command)
7272         {
7273         case AIROGCAP:      ridcode = RID_CAPABILITIES; break;
7274         case AIROGCFG:      ridcode = RID_CONFIG;
7275                 if (test_bit(FLAG_COMMIT, &ai->flags)) {
7276                         disable_MAC (ai, 1);
7277                         writeConfigRid (ai, 1);
7278                         enable_MAC (ai, &rsp, 1);
7279                 }
7280                 break;
7281         case AIROGSLIST:    ridcode = RID_SSID;         break;
7282         case AIROGVLIST:    ridcode = RID_APLIST;       break;
7283         case AIROGDRVNAM:   ridcode = RID_DRVNAME;      break;
7284         case AIROGEHTENC:   ridcode = RID_ETHERENCAP;   break;
7285         case AIROGWEPKTMP:  ridcode = RID_WEP_TEMP;
7286                 /* Only super-user can read WEP keys */
7287                 if (!capable(CAP_NET_ADMIN))
7288                         return -EPERM;
7289                 break;
7290         case AIROGWEPKNV:   ridcode = RID_WEP_PERM;
7291                 /* Only super-user can read WEP keys */
7292                 if (!capable(CAP_NET_ADMIN))
7293                         return -EPERM;
7294                 break;
7295         case AIROGSTAT:     ridcode = RID_STATUS;       break;
7296         case AIROGSTATSD32: ridcode = RID_STATSDELTA;   break;
7297         case AIROGSTATSC32: ridcode = RID_STATS;        break;
7298 #ifdef MICSUPPORT
7299         case AIROGMICSTATS:
7300                 if (copy_to_user(comp->data, &ai->micstats,
7301                                  min((int)comp->len,(int)sizeof(ai->micstats))))
7302                         return -EFAULT;
7303                 return 0;
7304 #endif
7305         case AIRORRID:      ridcode = comp->ridnum;     break;
7306         default:
7307                 return -EINVAL;
7308                 break;
7309         }
7310
7311         if ((iobuf = kmalloc(RIDSIZE, GFP_KERNEL)) == NULL)
7312                 return -ENOMEM;
7313
7314         PC4500_readrid(ai,ridcode,iobuf,RIDSIZE, 1);
7315         /* get the count of bytes in the rid  docs say 1st 2 bytes is it.
7316          * then return it to the user
7317          * 9/22/2000 Honor user given length
7318          */
7319         len = comp->len;
7320
7321         if (copy_to_user(comp->data, iobuf, min(len, (int)RIDSIZE))) {
7322                 kfree (iobuf);
7323                 return -EFAULT;
7324         }
7325         kfree (iobuf);
7326         return 0;
7327 }
7328
7329 /*
7330  * Danger Will Robinson write the rids here
7331  */
7332
7333 static int writerids(struct net_device *dev, aironet_ioctl *comp) {
7334         struct airo_info *ai = dev->priv;
7335         int  ridcode;
7336 #ifdef MICSUPPORT
7337         int  enabled;
7338 #endif
7339         Resp      rsp;
7340         static int (* writer)(struct airo_info *, u16 rid, const void *, int, int);
7341         unsigned char *iobuf;
7342
7343         /* Only super-user can write RIDs */
7344         if (!capable(CAP_NET_ADMIN))
7345                 return -EPERM;
7346
7347         if (test_bit(FLAG_FLASHING, &ai->flags))
7348                 return -EIO;
7349
7350         ridcode = 0;
7351         writer = do_writerid;
7352
7353         switch(comp->command)
7354         {
7355         case AIROPSIDS:     ridcode = RID_SSID;         break;
7356         case AIROPCAP:      ridcode = RID_CAPABILITIES; break;
7357         case AIROPAPLIST:   ridcode = RID_APLIST;       break;
7358         case AIROPCFG: ai->config.len = 0;
7359                             clear_bit(FLAG_COMMIT, &ai->flags);
7360                             ridcode = RID_CONFIG;       break;
7361         case AIROPWEPKEYNV: ridcode = RID_WEP_PERM;     break;
7362         case AIROPLEAPUSR:  ridcode = RID_LEAPUSERNAME; break;
7363         case AIROPLEAPPWD:  ridcode = RID_LEAPPASSWORD; break;
7364         case AIROPWEPKEY:   ridcode = RID_WEP_TEMP; writer = PC4500_writerid;
7365                 break;
7366         case AIROPLEAPUSR+1: ridcode = 0xFF2A;          break;
7367         case AIROPLEAPUSR+2: ridcode = 0xFF2B;          break;
7368
7369                 /* this is not really a rid but a command given to the card
7370                  * same with MAC off
7371                  */
7372         case AIROPMACON:
7373                 if (enable_MAC(ai, &rsp, 1) != 0)
7374                         return -EIO;
7375                 return 0;
7376
7377                 /*
7378                  * Evidently this code in the airo driver does not get a symbol
7379                  * as disable_MAC. it's probably so short the compiler does not gen one.
7380                  */
7381         case AIROPMACOFF:
7382                 disable_MAC(ai, 1);
7383                 return 0;
7384
7385                 /* This command merely clears the counts does not actually store any data
7386                  * only reads rid. But as it changes the cards state, I put it in the
7387                  * writerid routines.
7388                  */
7389         case AIROPSTCLR:
7390                 if ((iobuf = kmalloc(RIDSIZE, GFP_KERNEL)) == NULL)
7391                         return -ENOMEM;
7392
7393                 PC4500_readrid(ai,RID_STATSDELTACLEAR,iobuf,RIDSIZE, 1);
7394
7395 #ifdef MICSUPPORT
7396                 enabled = ai->micstats.enabled;
7397                 memset(&ai->micstats,0,sizeof(ai->micstats));
7398                 ai->micstats.enabled = enabled;
7399 #endif
7400
7401                 if (copy_to_user(comp->data, iobuf,
7402                                  min((int)comp->len, (int)RIDSIZE))) {
7403                         kfree (iobuf);
7404                         return -EFAULT;
7405                 }
7406                 kfree (iobuf);
7407                 return 0;
7408
7409         default:
7410                 return -EOPNOTSUPP;     /* Blarg! */
7411         }
7412         if(comp->len > RIDSIZE)
7413                 return -EINVAL;
7414
7415         if ((iobuf = kmalloc(RIDSIZE, GFP_KERNEL)) == NULL)
7416                 return -ENOMEM;
7417
7418         if (copy_from_user(iobuf,comp->data,comp->len)) {
7419                 kfree (iobuf);
7420                 return -EFAULT;
7421         }
7422
7423         if (comp->command == AIROPCFG) {
7424                 ConfigRid *cfg = (ConfigRid *)iobuf;
7425
7426                 if (test_bit(FLAG_MIC_CAPABLE, &ai->flags))
7427                         cfg->opmode |= MODE_MIC;
7428
7429                 if ((cfg->opmode & 0xFF) == MODE_STA_IBSS)
7430                         set_bit (FLAG_ADHOC, &ai->flags);
7431                 else
7432                         clear_bit (FLAG_ADHOC, &ai->flags);
7433         }
7434
7435         if((*writer)(ai, ridcode, iobuf,comp->len,1)) {
7436                 kfree (iobuf);
7437                 return -EIO;
7438         }
7439         kfree (iobuf);
7440         return 0;
7441 }
7442
7443 /*****************************************************************************
7444  * Ancillary flash / mod functions much black magic lurkes here              *
7445  *****************************************************************************
7446  */
7447
7448 /*
7449  * Flash command switch table
7450  */
7451
7452 int flashcard(struct net_device *dev, aironet_ioctl *comp) {
7453         int z;
7454         int cmdreset(struct airo_info *);
7455         int setflashmode(struct airo_info *);
7456         int flashgchar(struct airo_info *,int,int);
7457         int flashpchar(struct airo_info *,int,int);
7458         int flashputbuf(struct airo_info *);
7459         int flashrestart(struct airo_info *,struct net_device *);
7460
7461         /* Only super-user can modify flash */
7462         if (!capable(CAP_NET_ADMIN))
7463                 return -EPERM;
7464
7465         switch(comp->command)
7466         {
7467         case AIROFLSHRST:
7468                 return cmdreset((struct airo_info *)dev->priv);
7469
7470         case AIROFLSHSTFL:
7471                 if (!((struct airo_info *)dev->priv)->flash &&
7472                         (((struct airo_info *)dev->priv)->flash = kmalloc (FLASHSIZE, GFP_KERNEL)) == NULL)
7473                         return -ENOMEM;
7474                 return setflashmode((struct airo_info *)dev->priv);
7475
7476         case AIROFLSHGCHR: /* Get char from aux */
7477                 if(comp->len != sizeof(int))
7478                         return -EINVAL;
7479                 if (copy_from_user(&z,comp->data,comp->len))
7480                         return -EFAULT;
7481                 return flashgchar((struct airo_info *)dev->priv,z,8000);
7482
7483         case AIROFLSHPCHR: /* Send char to card. */
7484                 if(comp->len != sizeof(int))
7485                         return -EINVAL;
7486                 if (copy_from_user(&z,comp->data,comp->len))
7487                         return -EFAULT;
7488                 return flashpchar((struct airo_info *)dev->priv,z,8000);
7489
7490         case AIROFLPUTBUF: /* Send 32k to card */
7491                 if (!((struct airo_info *)dev->priv)->flash)
7492                         return -ENOMEM;
7493                 if(comp->len > FLASHSIZE)
7494                         return -EINVAL;
7495                 if(copy_from_user(((struct airo_info *)dev->priv)->flash,comp->data,comp->len))
7496                         return -EFAULT;
7497
7498                 flashputbuf((struct airo_info *)dev->priv);
7499                 return 0;
7500
7501         case AIRORESTART:
7502                 if(flashrestart((struct airo_info *)dev->priv,dev))
7503                         return -EIO;
7504                 return 0;
7505         }
7506         return -EINVAL;
7507 }
7508
7509 #define FLASH_COMMAND  0x7e7e
7510
7511 /*
7512  * STEP 1)
7513  * Disable MAC and do soft reset on
7514  * card.
7515  */
7516
7517 int cmdreset(struct airo_info *ai) {
7518         disable_MAC(ai, 1);
7519
7520         if(!waitbusy (ai)){
7521                 printk(KERN_INFO "Waitbusy hang before RESET\n");
7522                 return -EBUSY;
7523         }
7524
7525         OUT4500(ai,COMMAND,CMD_SOFTRESET);
7526
7527         ssleep(1);                      /* WAS 600 12/7/00 */
7528
7529         if(!waitbusy (ai)){
7530                 printk(KERN_INFO "Waitbusy hang AFTER RESET\n");
7531                 return -EBUSY;
7532         }
7533         return 0;
7534 }
7535
7536 /* STEP 2)
7537  * Put the card in legendary flash
7538  * mode
7539  */
7540
7541 int setflashmode (struct airo_info *ai) {
7542         set_bit (FLAG_FLASHING, &ai->flags);
7543
7544         OUT4500(ai, SWS0, FLASH_COMMAND);
7545         OUT4500(ai, SWS1, FLASH_COMMAND);
7546         if (probe) {
7547                 OUT4500(ai, SWS0, FLASH_COMMAND);
7548                 OUT4500(ai, COMMAND,0x10);
7549         } else {
7550                 OUT4500(ai, SWS2, FLASH_COMMAND);
7551                 OUT4500(ai, SWS3, FLASH_COMMAND);
7552                 OUT4500(ai, COMMAND,0);
7553         }
7554         msleep(500);            /* 500ms delay */
7555
7556         if(!waitbusy(ai)) {
7557                 clear_bit (FLAG_FLASHING, &ai->flags);
7558                 printk(KERN_INFO "Waitbusy hang after setflash mode\n");
7559                 return -EIO;
7560         }
7561         return 0;
7562 }
7563
7564 /* Put character to SWS0 wait for dwelltime
7565  * x 50us for  echo .
7566  */
7567
7568 int flashpchar(struct airo_info *ai,int byte,int dwelltime) {
7569         int echo;
7570         int waittime;
7571
7572         byte |= 0x8000;
7573
7574         if(dwelltime == 0 )
7575                 dwelltime = 200;
7576
7577         waittime=dwelltime;
7578
7579         /* Wait for busy bit d15 to go false indicating buffer empty */
7580         while ((IN4500 (ai, SWS0) & 0x8000) && waittime > 0) {
7581                 udelay (50);
7582                 waittime -= 50;
7583         }
7584
7585         /* timeout for busy clear wait */
7586         if(waittime <= 0 ){
7587                 printk(KERN_INFO "flash putchar busywait timeout! \n");
7588                 return -EBUSY;
7589         }
7590
7591         /* Port is clear now write byte and wait for it to echo back */
7592         do {
7593                 OUT4500(ai,SWS0,byte);
7594                 udelay(50);
7595                 dwelltime -= 50;
7596                 echo = IN4500(ai,SWS1);
7597         } while (dwelltime >= 0 && echo != byte);
7598
7599         OUT4500(ai,SWS1,0);
7600
7601         return (echo == byte) ? 0 : -EIO;
7602 }
7603
7604 /*
7605  * Get a character from the card matching matchbyte
7606  * Step 3)
7607  */
7608 int flashgchar(struct airo_info *ai,int matchbyte,int dwelltime){
7609         int           rchar;
7610         unsigned char rbyte=0;
7611
7612         do {
7613                 rchar = IN4500(ai,SWS1);
7614
7615                 if(dwelltime && !(0x8000 & rchar)){
7616                         dwelltime -= 10;
7617                         mdelay(10);
7618                         continue;
7619                 }
7620                 rbyte = 0xff & rchar;
7621
7622                 if( (rbyte == matchbyte) && (0x8000 & rchar) ){
7623                         OUT4500(ai,SWS1,0);
7624                         return 0;
7625                 }
7626                 if( rbyte == 0x81 || rbyte == 0x82 || rbyte == 0x83 || rbyte == 0x1a || 0xffff == rchar)
7627                         break;
7628                 OUT4500(ai,SWS1,0);
7629
7630         }while(dwelltime > 0);
7631         return -EIO;
7632 }
7633
7634 /*
7635  * Transfer 32k of firmware data from user buffer to our buffer and
7636  * send to the card
7637  */
7638
7639 int flashputbuf(struct airo_info *ai){
7640         int            nwords;
7641
7642         /* Write stuff */
7643         if (test_bit(FLAG_MPI,&ai->flags))
7644                 memcpy_toio(ai->pciaux + 0x8000, ai->flash, FLASHSIZE);
7645         else {
7646                 OUT4500(ai,AUXPAGE,0x100);
7647                 OUT4500(ai,AUXOFF,0);
7648
7649                 for(nwords=0;nwords != FLASHSIZE / 2;nwords++){
7650                         OUT4500(ai,AUXDATA,ai->flash[nwords] & 0xffff);
7651                 }
7652         }
7653         OUT4500(ai,SWS0,0x8000);
7654
7655         return 0;
7656 }
7657
7658 /*
7659  *
7660  */
7661 int flashrestart(struct airo_info *ai,struct net_device *dev){
7662         int    i,status;
7663
7664         ssleep(1);                      /* Added 12/7/00 */
7665         clear_bit (FLAG_FLASHING, &ai->flags);
7666         if (test_bit(FLAG_MPI, &ai->flags)) {
7667                 status = mpi_init_descriptors(ai);
7668                 if (status != SUCCESS)
7669                         return status;
7670         }
7671         status = setup_card(ai, dev->dev_addr, 1);
7672
7673         if (!test_bit(FLAG_MPI,&ai->flags))
7674                 for( i = 0; i < MAX_FIDS; i++ ) {
7675                         ai->fids[i] = transmit_allocate
7676                                 ( ai, 2312, i >= MAX_FIDS / 2 );
7677                 }
7678
7679         ssleep(1);                      /* Added 12/7/00 */
7680         return status;
7681 }
7682 #endif /* CISCO_EXT */
7683
7684 /*
7685     This program is free software; you can redistribute it and/or
7686     modify it under the terms of the GNU General Public License
7687     as published by the Free Software Foundation; either version 2
7688     of the License, or (at your option) any later version.
7689
7690     This program is distributed in the hope that it will be useful,
7691     but WITHOUT ANY WARRANTY; without even the implied warranty of
7692     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7693     GNU General Public License for more details.
7694
7695     In addition:
7696
7697     Redistribution and use in source and binary forms, with or without
7698     modification, are permitted provided that the following conditions
7699     are met:
7700
7701     1. Redistributions of source code must retain the above copyright
7702        notice, this list of conditions and the following disclaimer.
7703     2. Redistributions in binary form must reproduce the above copyright
7704        notice, this list of conditions and the following disclaimer in the
7705        documentation and/or other materials provided with the distribution.
7706     3. The name of the author may not be used to endorse or promote
7707        products derived from this software without specific prior written
7708        permission.
7709
7710     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
7711     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
7712     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7713     ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
7714     INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
7715     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
7716     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
7717     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
7718     STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
7719     IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
7720     POSSIBILITY OF SUCH DAMAGE.
7721 */
7722
7723 module_init(airo_init_module);
7724 module_exit(airo_cleanup_module);