root/drivers/net/wireless/cisco/airo.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. bap_read
  2. age_mic_context
  3. micinit
  4. micsetup
  5. encapsulate
  6. decapsulate
  7. RxSeqValid
  8. MoveWindow
  9. emmh32_setseed
  10. emmh32_init
  11. emmh32_update
  12. emmh32_final
  13. readBSSListRid
  14. readWepKeyRid
  15. writeWepKeyRid
  16. readSsidRid
  17. writeSsidRid
  18. readConfigRid
  19. checkThrottle
  20. writeConfigRid
  21. readStatusRid
  22. writeAPListRid
  23. readCapabilityRid
  24. readStatsRid
  25. try_auto_wep
  26. airo_open
  27. mpi_start_xmit
  28. mpi_send_packet
  29. get_tx_error
  30. airo_end_xmit
  31. airo_start_xmit
  32. airo_end_xmit11
  33. airo_start_xmit11
  34. airo_read_stats
  35. airo_get_stats
  36. airo_set_promisc
  37. airo_set_multicast_list
  38. airo_set_mac_address
  39. add_airo_dev
  40. del_airo_dev
  41. airo_close
  42. stop_airo_card
  43. wll_header_parse
  44. mpi_unmap_card
  45. mpi_init_descriptors
  46. mpi_map_card
  47. wifi_setup
  48. init_wifidev
  49. reset_card
  50. airo_networks_allocate
  51. airo_networks_free
  52. airo_networks_initialize
  53. _init_airo_card
  54. init_airo_card
  55. waitbusy
  56. reset_airo_card
  57. airo_send_event
  58. airo_process_scan_results
  59. airo_thread
  60. header_len
  61. airo_handle_cisco_mic
  62. airo_print_status
  63. airo_handle_link
  64. airo_handle_rx
  65. airo_handle_tx
  66. airo_interrupt
  67. OUT4500
  68. IN4500
  69. enable_MAC
  70. disable_MAC
  71. enable_interrupts
  72. disable_interrupts
  73. mpi_receive_802_3
  74. mpi_receive_802_11
  75. set_auth_type
  76. setup_card
  77. issuecommand
  78. bap_setup
  79. aux_setup
  80. aux_bap_read
  81. fast_bap_read
  82. bap_write
  83. PC4500_accessrid
  84. PC4500_readrid
  85. PC4500_writerid
  86. transmit_allocate
  87. transmit_802_3_packet
  88. transmit_802_11_packet
  89. setup_proc_entry
  90. takedown_proc_entry
  91. proc_read
  92. proc_write
  93. proc_status_open
  94. proc_statsdelta_open
  95. proc_stats_open
  96. proc_stats_rid_open
  97. get_dec_u16
  98. sniffing_mode
  99. proc_config_on_close
  100. get_rmode
  101. proc_config_open
  102. proc_SSID_on_close
  103. proc_APList_on_close
  104. do_writerid
  105. get_wep_key
  106. get_wep_tx_idx
  107. set_wep_key
  108. set_wep_tx_idx
  109. proc_wepkey_on_close
  110. proc_wepkey_open
  111. proc_SSID_open
  112. proc_APList_open
  113. proc_BSSList_open
  114. proc_close
  115. timer_func
  116. airo_pci_probe
  117. airo_pci_remove
  118. airo_pci_suspend
  119. airo_pci_resume
  120. airo_init_module
  121. airo_cleanup_module
  122. airo_rssi_to_dbm
  123. airo_dbm_to_pct
  124. airo_get_quality
  125. airo_get_name
  126. airo_set_freq
  127. airo_get_freq
  128. airo_set_essid
  129. airo_get_essid
  130. airo_set_wap
  131. airo_get_wap
  132. airo_set_nick
  133. airo_get_nick
  134. airo_set_rate
  135. airo_get_rate
  136. airo_set_rts
  137. airo_get_rts
  138. airo_set_frag
  139. airo_get_frag
  140. airo_set_mode
  141. airo_get_mode
  142. valid_index
  143. airo_set_encode
  144. airo_get_encode
  145. airo_set_encodeext
  146. airo_get_encodeext
  147. airo_set_auth
  148. airo_get_auth
  149. airo_set_txpow
  150. airo_get_txpow
  151. airo_set_retry
  152. airo_get_retry
  153. airo_get_range
  154. airo_set_power
  155. airo_get_power
  156. airo_set_sens
  157. airo_get_sens
  158. airo_get_aplist
  159. airo_set_scan
  160. airo_translate_scan
  161. airo_get_scan
  162. airo_config_commit
  163. airo_ioctl
  164. airo_read_wireless_stats
  165. airo_get_wireless_stats
  166. readrids
  167. writerids
  168. flashcard
  169. cmdreset
  170. setflashmode
  171. flashpchar
  172. flashgchar
  173. flashputbuf
  174. flashrestart

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

/* [<][>][^][v][top][bottom][index][help] */