airo.c 218.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*======================================================================

    Aironet driver for 4500 and 4800 series cards

    This code is released under both the GPL version 2 and BSD licenses.
    Either license may be used.  The respective licenses are found at
    the end of this file.

    This code was developed by Benjamin Reed <breed@users.sourceforge.net>
    including portions of which come from the Aironet PC4500
    Developer's Reference Manual and used with permission.  Copyright
    (C) 1999 Benjamin Reed.  All Rights Reserved.  Permission to use
    code in the Developer's manual was granted for this driver by
    Aironet.  Major code contributions were received from Javier Achirica
    <achirica@users.sourceforge.net> and Jean Tourrilhes <jt@hpl.hp.com>.
    Code was also integrated from the Cisco Aironet driver for Linux.
    Support for MPI350 cards was added by Fabrice Bellet
    <fabrice@bellet.info>.

======================================================================*/

22
#include <linux/err.h>
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <linux/init.h>

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>

#include <linux/sched.h>
#include <linux/ptrace.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/in.h>
#include <linux/bitops.h>
37
#include <linux/scatterlist.h>
38
#include <linux/crypto.h>
L
Linus Torvalds 已提交
39 40
#include <asm/io.h>
#include <asm/system.h>
A
Al Viro 已提交
41
#include <asm/unaligned.h>
L
Linus Torvalds 已提交
42 43 44 45 46 47 48 49

#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/if_arp.h>
#include <linux/ioport.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
50
#include <net/ieee80211.h>
S
Sukadev Bhattiprolu 已提交
51
#include <linux/kthread.h>
52
#include <linux/freezer.h>
L
Linus Torvalds 已提交
53

54 55
#include "airo.h"

56 57
#define DRV_NAME "airo"

L
Linus Torvalds 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#ifdef CONFIG_PCI
static struct pci_device_id card_ids[] = {
	{ 0x14b9, 1, PCI_ANY_ID, PCI_ANY_ID, },
	{ 0x14b9, 0x4500, PCI_ANY_ID, PCI_ANY_ID },
	{ 0x14b9, 0x4800, PCI_ANY_ID, PCI_ANY_ID, },
	{ 0x14b9, 0x0340, PCI_ANY_ID, PCI_ANY_ID, },
	{ 0x14b9, 0x0350, PCI_ANY_ID, PCI_ANY_ID, },
	{ 0x14b9, 0x5000, PCI_ANY_ID, PCI_ANY_ID, },
	{ 0x14b9, 0xa504, PCI_ANY_ID, PCI_ANY_ID, },
	{ 0, }
};
MODULE_DEVICE_TABLE(pci, card_ids);

static int airo_pci_probe(struct pci_dev *, const struct pci_device_id *);
static void airo_pci_remove(struct pci_dev *);
73
static int airo_pci_suspend(struct pci_dev *pdev, pm_message_t state);
L
Linus Torvalds 已提交
74 75 76
static int airo_pci_resume(struct pci_dev *pdev);

static struct pci_driver airo_driver = {
77
	.name     = DRV_NAME,
L
Linus Torvalds 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	.id_table = card_ids,
	.probe    = airo_pci_probe,
	.remove   = __devexit_p(airo_pci_remove),
	.suspend  = airo_pci_suspend,
	.resume   = airo_pci_resume,
};
#endif /* CONFIG_PCI */

/* Include Wireless Extension definition and check version - Jean II */
#include <linux/wireless.h>
#define WIRELESS_SPY		// enable iwspy support
#include <net/iw_handler.h>	// New driver API

#define CISCO_EXT		// enable Cisco extensions
#ifdef CISCO_EXT
#include <linux/delay.h>
#endif

/* Hack to do some power saving */
#define POWER_ON_DOWN

/* As you can see this list is HUGH!
   I really don't know what a lot of these counts are about, but they
   are all here for completeness.  If the IGNLABEL macro is put in
   infront of the label, that statistic will not be included in the list
   of statistics in the /proc filesystem */

#define IGNLABEL(comment) NULL
static char *statsLabels[] = {
	"RxOverrun",
	IGNLABEL("RxPlcpCrcErr"),
	IGNLABEL("RxPlcpFormatErr"),
	IGNLABEL("RxPlcpLengthErr"),
	"RxMacCrcErr",
	"RxMacCrcOk",
	"RxWepErr",
	"RxWepOk",
	"RetryLong",
	"RetryShort",
	"MaxRetries",
	"NoAck",
	"NoCts",
	"RxAck",
	"RxCts",
	"TxAck",
	"TxRts",
	"TxCts",
	"TxMc",
	"TxBc",
	"TxUcFrags",
	"TxUcPackets",
	"TxBeacon",
	"RxBeacon",
	"TxSinColl",
	"TxMulColl",
	"DefersNo",
	"DefersProt",
	"DefersEngy",
	"DupFram",
	"RxFragDisc",
	"TxAged",
	"RxAged",
	"LostSync-MaxRetry",
	"LostSync-MissedBeacons",
	"LostSync-ArlExceeded",
	"LostSync-Deauth",
	"LostSync-Disassoced",
	"LostSync-TsfTiming",
	"HostTxMc",
	"HostTxBc",
	"HostTxUc",
	"HostTxFail",
	"HostRxMc",
	"HostRxBc",
	"HostRxUc",
	"HostRxDiscard",
	IGNLABEL("HmacTxMc"),
	IGNLABEL("HmacTxBc"),
	IGNLABEL("HmacTxUc"),
	IGNLABEL("HmacTxFail"),
	IGNLABEL("HmacRxMc"),
	IGNLABEL("HmacRxBc"),
	IGNLABEL("HmacRxUc"),
	IGNLABEL("HmacRxDiscard"),
	IGNLABEL("HmacRxAccepted"),
	"SsidMismatch",
	"ApMismatch",
	"RatesMismatch",
	"AuthReject",
	"AuthTimeout",
	"AssocReject",
	"AssocTimeout",
	IGNLABEL("ReasonOutsideTable"),
	IGNLABEL("ReasonStatus1"),
	IGNLABEL("ReasonStatus2"),
	IGNLABEL("ReasonStatus3"),
	IGNLABEL("ReasonStatus4"),
	IGNLABEL("ReasonStatus5"),
	IGNLABEL("ReasonStatus6"),
	IGNLABEL("ReasonStatus7"),
	IGNLABEL("ReasonStatus8"),
	IGNLABEL("ReasonStatus9"),
	IGNLABEL("ReasonStatus10"),
	IGNLABEL("ReasonStatus11"),
	IGNLABEL("ReasonStatus12"),
	IGNLABEL("ReasonStatus13"),
	IGNLABEL("ReasonStatus14"),
	IGNLABEL("ReasonStatus15"),
	IGNLABEL("ReasonStatus16"),
	IGNLABEL("ReasonStatus17"),
	IGNLABEL("ReasonStatus18"),
	IGNLABEL("ReasonStatus19"),
	"RxMan",
	"TxMan",
	"RxRefresh",
	"TxRefresh",
	"RxPoll",
	"TxPoll",
	"HostRetries",
	"LostSync-HostReq",
	"HostTxBytes",
	"HostRxBytes",
	"ElapsedUsec",
	"ElapsedSec",
	"LostSyncBetterAP",
	"PrivacyMismatch",
	"Jammed",
	"DiscRxNotWepped",
	"PhyEleMismatch",
	(char*)-1 };
#ifndef RUN_AT
#define RUN_AT(x) (jiffies+(x))
#endif


/* These variables are for insmod, since it seems that the rates
   can only be set in setup_card.  Rates should be a comma separated
   (no spaces) list of rates (up to 8). */

static int rates[8];
static int basic_rate;
static char *ssids[3];

static int io[4];
static int irq[4];

static
int maxencrypt /* = 0 */; /* The highest rate that the card can encrypt at.
		       0 means no limit.  For old cards this was 4 */

static int auto_wep /* = 0 */; /* If set, it tries to figure out the wep mode */
static int aux_bap /* = 0 */; /* Checks to see if the aux ports are needed to read
		    the bap, needed on some older cards and buses. */
static int adhoc;

static int probe = 1;

static int proc_uid /* = 0 */;

static int proc_gid /* = 0 */;

static int airo_perm = 0555;

static int proc_perm = 0644;

MODULE_AUTHOR("Benjamin Reed");
MODULE_DESCRIPTION("Support for Cisco/Aironet 802.11 wireless ethernet \
245 246
cards.  Direct support for ISA/PCI/MPI cards and support \
for PCMCIA when used with airo_cs.");
L
Linus Torvalds 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
MODULE_LICENSE("Dual BSD/GPL");
MODULE_SUPPORTED_DEVICE("Aironet 4500, 4800 and Cisco 340/350");
module_param_array(io, int, NULL, 0);
module_param_array(irq, int, NULL, 0);
module_param(basic_rate, int, 0);
module_param_array(rates, int, NULL, 0);
module_param_array(ssids, charp, NULL, 0);
module_param(auto_wep, int, 0);
MODULE_PARM_DESC(auto_wep, "If non-zero, the driver will keep looping through \
the authentication options until an association is made.  The value of \
auto_wep is number of the wep keys to check.  A value of 2 will try using \
the key at index 0 and index 1.");
module_param(aux_bap, int, 0);
MODULE_PARM_DESC(aux_bap, "If non-zero, the driver will switch into a mode \
than seems to work better for older cards with some older buses.  Before \
switching it checks that the switch is needed.");
module_param(maxencrypt, int, 0);
MODULE_PARM_DESC(maxencrypt, "The maximum speed that the card can do \
encryption.  Units are in 512kbs.  Zero (default) means there is no limit. \
Older cards used to be limited to 2mbs (4).");
module_param(adhoc, int, 0);
MODULE_PARM_DESC(adhoc, "If non-zero, the card will start in adhoc mode.");
module_param(probe, int, 0);
MODULE_PARM_DESC(probe, "If zero, the driver won't start the card.");

module_param(proc_uid, int, 0);
MODULE_PARM_DESC(proc_uid, "The uid that the /proc files will belong to.");
module_param(proc_gid, int, 0);
MODULE_PARM_DESC(proc_gid, "The gid that the /proc files will belong to.");
module_param(airo_perm, int, 0);
MODULE_PARM_DESC(airo_perm, "The permission bits of /proc/[driver/]aironet.");
module_param(proc_perm, int, 0);
MODULE_PARM_DESC(proc_perm, "The permission bits of the files in /proc");

/* This is a kind of sloppy hack to get this information to OUT4500 and
   IN4500.  I would be extremely interested in the situation where this
   doesn't work though!!! */
static int do8bitIO = 0;

/* Return codes */
#define SUCCESS 0
#define ERROR -1
#define NO_PACKET -2

/* Commands */
#define NOP2		0x0000
#define MAC_ENABLE	0x0001
#define MAC_DISABLE	0x0002
#define CMD_LOSE_SYNC	0x0003 /* Not sure what this does... */
#define CMD_SOFTRESET	0x0004
#define HOSTSLEEP	0x0005
#define CMD_MAGIC_PKT	0x0006
#define CMD_SETWAKEMASK	0x0007
#define CMD_READCFG	0x0008
#define CMD_SETMODE	0x0009
#define CMD_ALLOCATETX	0x000a
#define CMD_TRANSMIT	0x000b
#define CMD_DEALLOCATETX 0x000c
#define NOP		0x0010
#define CMD_WORKAROUND	0x0011
#define CMD_ALLOCATEAUX 0x0020
#define CMD_ACCESS	0x0021
#define CMD_PCIBAP	0x0022
#define CMD_PCIAUX	0x0023
#define CMD_ALLOCBUF	0x0028
#define CMD_GETTLV	0x0029
#define CMD_PUTTLV	0x002a
#define CMD_DELTLV	0x002b
#define CMD_FINDNEXTTLV	0x002c
#define CMD_PSPNODES	0x0030
#define CMD_SETCW	0x0031    
#define CMD_SETPCF	0x0032    
#define CMD_SETPHYREG	0x003e
#define CMD_TXTEST	0x003f
#define MAC_ENABLETX	0x0101
#define CMD_LISTBSS	0x0103
#define CMD_SAVECFG	0x0108
#define CMD_ENABLEAUX	0x0111
#define CMD_WRITERID	0x0121
#define CMD_USEPSPNODES	0x0130
#define MAC_ENABLERX	0x0201

/* Command errors */
#define ERROR_QUALIF 0x00
#define ERROR_ILLCMD 0x01
#define ERROR_ILLFMT 0x02
#define ERROR_INVFID 0x03
#define ERROR_INVRID 0x04
#define ERROR_LARGE 0x05
#define ERROR_NDISABL 0x06
#define ERROR_ALLOCBSY 0x07
#define ERROR_NORD 0x0B
#define ERROR_NOWR 0x0C
#define ERROR_INVFIDTX 0x0D
#define ERROR_TESTACT 0x0E
#define ERROR_TAGNFND 0x12
#define ERROR_DECODE 0x20
#define ERROR_DESCUNAV 0x21
#define ERROR_BADLEN 0x22
#define ERROR_MODE 0x80
#define ERROR_HOP 0x81
#define ERROR_BINTER 0x82
#define ERROR_RXMODE 0x83
#define ERROR_MACADDR 0x84
#define ERROR_RATES 0x85
#define ERROR_ORDER 0x86
#define ERROR_SCAN 0x87
#define ERROR_AUTH 0x88
#define ERROR_PSMODE 0x89
#define ERROR_RTYPE 0x8A
#define ERROR_DIVER 0x8B
#define ERROR_SSID 0x8C
#define ERROR_APLIST 0x8D
#define ERROR_AUTOWAKE 0x8E
#define ERROR_LEAP 0x8F

/* Registers */
#define COMMAND 0x00
#define PARAM0 0x02
#define PARAM1 0x04
#define PARAM2 0x06
#define STATUS 0x08
#define RESP0 0x0a
#define RESP1 0x0c
#define RESP2 0x0e
#define LINKSTAT 0x10
#define SELECT0 0x18
#define OFFSET0 0x1c
#define RXFID 0x20
#define TXALLOCFID 0x22
#define TXCOMPLFID 0x24
#define DATA0 0x36
#define EVSTAT 0x30
#define EVINTEN 0x32
#define EVACK 0x34
#define SWS0 0x28
#define SWS1 0x2a
#define SWS2 0x2c
#define SWS3 0x2e
#define AUXPAGE 0x3A
#define AUXOFF 0x3C
#define AUXDATA 0x3E

#define FID_TX 1
#define FID_RX 2
/* Offset into aux memory for descriptors */
#define AUX_OFFSET 0x800
/* Size of allocated packets */
#define PKTSIZE 1840
#define RIDSIZE 2048
/* Size of the transmit queue */
#define MAXTXQ 64

/* BAP selectors */
#define BAP0 0 // Used for receiving packets
#define BAP1 2 // Used for xmiting packets and working with RIDS

/* Flags */
#define COMMAND_BUSY 0x8000

#define BAP_BUSY 0x8000
#define BAP_ERR 0x4000
#define BAP_DONE 0x2000

#define PROMISC 0xffff
#define NOPROMISC 0x0000

#define EV_CMD 0x10
#define EV_CLEARCOMMANDBUSY 0x4000
#define EV_RX 0x01
#define EV_TX 0x02
#define EV_TXEXC 0x04
#define EV_ALLOC 0x08
#define EV_LINK 0x80
#define EV_AWAKE 0x100
#define EV_TXCPY 0x400
#define EV_UNKNOWN 0x800
#define EV_MIC 0x1000 /* Message Integrity Check Interrupt */
#define EV_AWAKEN 0x2000
#define STATUS_INTS (EV_AWAKE|EV_LINK|EV_TXEXC|EV_TX|EV_TXCPY|EV_RX|EV_MIC)

#ifdef CHECK_UNKNOWN_INTS
#define IGNORE_INTS ( EV_CMD | EV_UNKNOWN)
#else
#define IGNORE_INTS (~STATUS_INTS)
#endif

/* RID TYPES */
#define RID_RW 0x20

/* The RIDs */
#define RID_CAPABILITIES 0xFF00
#define RID_APINFO     0xFF01
#define RID_RADIOINFO  0xFF02
#define RID_UNKNOWN3   0xFF03
#define RID_RSSI       0xFF04
#define RID_CONFIG     0xFF10
#define RID_SSID       0xFF11
#define RID_APLIST     0xFF12
#define RID_DRVNAME    0xFF13
#define RID_ETHERENCAP 0xFF14
#define RID_WEP_TEMP   0xFF15
#define RID_WEP_PERM   0xFF16
#define RID_MODULATION 0xFF17
#define RID_OPTIONS    0xFF18
#define RID_ACTUALCONFIG 0xFF20 /*readonly*/
#define RID_FACTORYCONFIG 0xFF21
#define RID_UNKNOWN22  0xFF22
#define RID_LEAPUSERNAME 0xFF23
#define RID_LEAPPASSWORD 0xFF24
#define RID_STATUS     0xFF50
#define RID_BEACON_HST 0xFF51
#define RID_BUSY_HST   0xFF52
#define RID_RETRIES_HST 0xFF53
#define RID_UNKNOWN54  0xFF54
#define RID_UNKNOWN55  0xFF55
#define RID_UNKNOWN56  0xFF56
#define RID_MIC        0xFF57
#define RID_STATS16    0xFF60
#define RID_STATS16DELTA 0xFF61
#define RID_STATS16DELTACLEAR 0xFF62
#define RID_STATS      0xFF68
#define RID_STATSDELTA 0xFF69
#define RID_STATSDELTACLEAR 0xFF6A
#define RID_ECHOTEST_RID 0xFF70
#define RID_ECHOTEST_RESULTS 0xFF71
#define RID_BSSLISTFIRST 0xFF72
#define RID_BSSLISTNEXT  0xFF73
475 476
#define RID_WPA_BSSLISTFIRST 0xFF74
#define RID_WPA_BSSLISTNEXT  0xFF75
L
Linus Torvalds 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503

typedef struct {
	u16 cmd;
	u16 parm0;
	u16 parm1;
	u16 parm2;
} Cmd;

typedef struct {
	u16 status;
	u16 rsp0;
	u16 rsp1;
	u16 rsp2;
} Resp;

/*
 * Rids and endian-ness:  The Rids will always be in cpu endian, since
 * this all the patches from the big-endian guys end up doing that.
 * so all rid access should use the read/writeXXXRid routines.
 */

/* This is redundant for x86 archs, but it seems necessary for ARM */
#pragma pack(1)

/* This structure came from an email sent to me from an engineer at
   aironet for inclusion into this driver */
typedef struct {
A
Al Viro 已提交
504 505
	__le16 len;
	__le16 kindex;
L
Linus Torvalds 已提交
506
	u8 mac[ETH_ALEN];
A
Al Viro 已提交
507
	__le16 klen;
L
Linus Torvalds 已提交
508 509 510 511 512
	u8 key[16];
} WepKeyRid;

/* These structures are from the Aironet's PC4500 Developers Manual */
typedef struct {
A
Al Viro 已提交
513
	__le16 len;
L
Linus Torvalds 已提交
514 515 516 517
	u8 ssid[32];
} Ssid;

typedef struct {
A
Al Viro 已提交
518
	__le16 len;
L
Linus Torvalds 已提交
519 520 521 522
	Ssid ssids[3];
} SsidRid;

typedef struct {
A
Al Viro 已提交
523 524 525 526 527
        __le16 len;
        __le16 modulation;
#define MOD_DEFAULT cpu_to_le16(0)
#define MOD_CCK cpu_to_le16(1)
#define MOD_MOK cpu_to_le16(2)
L
Linus Torvalds 已提交
528 529 530
} ModulationRid;

typedef struct {
A
Al Viro 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
	__le16 len; /* sizeof(ConfigRid) */
	__le16 opmode; /* operating mode */
#define MODE_STA_IBSS cpu_to_le16(0)
#define MODE_STA_ESS cpu_to_le16(1)
#define MODE_AP cpu_to_le16(2)
#define MODE_AP_RPTR cpu_to_le16(3)
#define MODE_CFG_MASK cpu_to_le16(0xff)
#define MODE_ETHERNET_HOST cpu_to_le16(0<<8) /* rx payloads converted */
#define MODE_LLC_HOST cpu_to_le16(1<<8) /* rx payloads left as is */
#define MODE_AIRONET_EXTEND cpu_to_le16(1<<9) /* enable Aironet extenstions */
#define MODE_AP_INTERFACE cpu_to_le16(1<<10) /* enable ap interface extensions */
#define MODE_ANTENNA_ALIGN cpu_to_le16(1<<11) /* enable antenna alignment */
#define MODE_ETHER_LLC cpu_to_le16(1<<12) /* enable ethernet LLC */
#define MODE_LEAF_NODE cpu_to_le16(1<<13) /* enable leaf node bridge */
#define MODE_CF_POLLABLE cpu_to_le16(1<<14) /* enable CF pollable */
#define MODE_MIC cpu_to_le16(1<<15) /* enable MIC */
	__le16 rmode; /* receive mode */
#define RXMODE_BC_MC_ADDR cpu_to_le16(0)
#define RXMODE_BC_ADDR cpu_to_le16(1) /* ignore multicasts */
#define RXMODE_ADDR cpu_to_le16(2) /* ignore multicast and broadcast */
#define RXMODE_RFMON cpu_to_le16(3) /* wireless monitor mode */
#define RXMODE_RFMON_ANYBSS cpu_to_le16(4)
#define RXMODE_LANMON cpu_to_le16(5) /* lan style monitor -- data packets only */
#define RXMODE_MASK cpu_to_le16(255)
#define RXMODE_DISABLE_802_3_HEADER cpu_to_le16(1<<8) /* disables 802.3 header on rx */
#define RXMODE_FULL_MASK (RXMODE_MASK | RXMODE_DISABLE_802_3_HEADER)
#define RXMODE_NORMALIZED_RSSI cpu_to_le16(1<<9) /* return normalized RSSI */
	__le16 fragThresh;
	__le16 rtsThres;
L
Linus Torvalds 已提交
560 561
	u8 macAddr[ETH_ALEN];
	u8 rates[8];
A
Al Viro 已提交
562 563 564 565 566 567 568 569 570 571
	__le16 shortRetryLimit;
	__le16 longRetryLimit;
	__le16 txLifetime; /* in kusec */
	__le16 rxLifetime; /* in kusec */
	__le16 stationary;
	__le16 ordering;
	__le16 u16deviceType; /* for overriding device type */
	__le16 cfpRate;
	__le16 cfpDuration;
	__le16 _reserved1[3];
L
Linus Torvalds 已提交
572
	/*---------- Scanning/Associating ----------*/
A
Al Viro 已提交
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	__le16 scanMode;
#define SCANMODE_ACTIVE cpu_to_le16(0)
#define SCANMODE_PASSIVE cpu_to_le16(1)
#define SCANMODE_AIROSCAN cpu_to_le16(2)
	__le16 probeDelay; /* in kusec */
	__le16 probeEnergyTimeout; /* in kusec */
        __le16 probeResponseTimeout;
	__le16 beaconListenTimeout;
	__le16 joinNetTimeout;
	__le16 authTimeout;
	__le16 authType;
#define AUTH_OPEN cpu_to_le16(0x1)
#define AUTH_ENCRYPT cpu_to_le16(0x101)
#define AUTH_SHAREDKEY cpu_to_le16(0x102)
#define AUTH_ALLOW_UNENCRYPTED cpu_to_le16(0x200)
	__le16 associationTimeout;
	__le16 specifiedApTimeout;
	__le16 offlineScanInterval;
	__le16 offlineScanDuration;
	__le16 linkLossDelay;
	__le16 maxBeaconLostTime;
	__le16 refreshInterval;
#define DISABLE_REFRESH cpu_to_le16(0xFFFF)
	__le16 _reserved1a[1];
L
Linus Torvalds 已提交
597
	/*---------- Power save operation ----------*/
A
Al Viro 已提交
598 599 600 601 602 603 604 605 606 607
	__le16 powerSaveMode;
#define POWERSAVE_CAM cpu_to_le16(0)
#define POWERSAVE_PSP cpu_to_le16(1)
#define POWERSAVE_PSPCAM cpu_to_le16(2)
	__le16 sleepForDtims;
	__le16 listenInterval;
	__le16 fastListenInterval;
	__le16 listenDecay;
	__le16 fastListenDelay;
	__le16 _reserved2[2];
L
Linus Torvalds 已提交
608
	/*---------- Ap/Ibss config items ----------*/
A
Al Viro 已提交
609 610 611 612 613 614 615 616
	__le16 beaconPeriod;
	__le16 atimDuration;
	__le16 hopPeriod;
	__le16 channelSet;
	__le16 channel;
	__le16 dtimPeriod;
	__le16 bridgeDistance;
	__le16 radioID;
L
Linus Torvalds 已提交
617
	/*---------- Radio configuration ----------*/
A
Al Viro 已提交
618 619 620 621
	__le16 radioType;
#define RADIOTYPE_DEFAULT cpu_to_le16(0)
#define RADIOTYPE_802_11 cpu_to_le16(1)
#define RADIOTYPE_LEGACY cpu_to_le16(2)
L
Linus Torvalds 已提交
622 623
	u8 rxDiversity;
	u8 txDiversity;
A
Al Viro 已提交
624
	__le16 txPower;
L
Linus Torvalds 已提交
625
#define TXPOWER_DEFAULT 0
A
Al Viro 已提交
626
	__le16 rssiThreshold;
L
Linus Torvalds 已提交
627
#define RSSI_DEFAULT 0
A
Al Viro 已提交
628 629 630 631 632 633 634
        __le16 modulation;
#define PREAMBLE_AUTO cpu_to_le16(0)
#define PREAMBLE_LONG cpu_to_le16(1)
#define PREAMBLE_SHORT cpu_to_le16(2)
	__le16 preamble;
	__le16 homeProduct;
	__le16 radioSpecific;
L
Linus Torvalds 已提交
635 636
	/*---------- Aironet Extensions ----------*/
	u8 nodeName[16];
A
Al Viro 已提交
637 638 639 640
	__le16 arlThreshold;
	__le16 arlDecay;
	__le16 arlDelay;
	__le16 _reserved4[1];
L
Linus Torvalds 已提交
641 642 643 644 645 646 647 648 649
	/*---------- Aironet Extensions ----------*/
	u8 magicAction;
#define MAGIC_ACTION_STSCHG 1
#define MAGIC_ACTION_RESUME 2
#define MAGIC_IGNORE_MCAST (1<<8)
#define MAGIC_IGNORE_BCAST (1<<9)
#define MAGIC_SWITCH_TO_PSP (0<<10)
#define MAGIC_STAY_IN_CAM (1<<10)
	u8 magicControl;
A
Al Viro 已提交
650
	__le16 autoWake;
L
Linus Torvalds 已提交
651 652 653
} ConfigRid;

typedef struct {
A
Al Viro 已提交
654
	__le16 len;
L
Linus Torvalds 已提交
655
	u8 mac[ETH_ALEN];
A
Al Viro 已提交
656 657 658 659
	__le16 mode;
	__le16 errorCode;
	__le16 sigQuality;
	__le16 SSIDlen;
L
Linus Torvalds 已提交
660 661 662
	char SSID[32];
	char apName[16];
	u8 bssid[4][ETH_ALEN];
A
Al Viro 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
	__le16 beaconPeriod;
	__le16 dimPeriod;
	__le16 atimDuration;
	__le16 hopPeriod;
	__le16 channelSet;
	__le16 channel;
	__le16 hopsToBackbone;
	__le16 apTotalLoad;
	__le16 generatedLoad;
	__le16 accumulatedArl;
	__le16 signalQuality;
	__le16 currentXmitRate;
	__le16 apDevExtensions;
	__le16 normalizedSignalStrength;
	__le16 shortPreamble;
L
Linus Torvalds 已提交
678 679 680 681 682 683 684
	u8 apIP[4];
	u8 noisePercent; /* Noise percent in last second */
	u8 noisedBm; /* Noise dBm in last second */
	u8 noiseAvePercent; /* Noise percent in last minute */
	u8 noiseAvedBm; /* Noise dBm in last minute */
	u8 noiseMaxPercent; /* Highest noise percent in last minute */
	u8 noiseMaxdBm; /* Highest noise dbm in last minute */
A
Al Viro 已提交
685
	__le16 load;
L
Linus Torvalds 已提交
686
	u8 carrier[4];
A
Al Viro 已提交
687
	__le16 assocStatus;
L
Linus Torvalds 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
#define STAT_NOPACKETS 0
#define STAT_NOCARRIERSET 10
#define STAT_GOTCARRIERSET 11
#define STAT_WRONGSSID 20
#define STAT_BADCHANNEL 25
#define STAT_BADBITRATES 30
#define STAT_BADPRIVACY 35
#define STAT_APFOUND 40
#define STAT_APREJECTED 50
#define STAT_AUTHENTICATING 60
#define STAT_DEAUTHENTICATED 61
#define STAT_AUTHTIMEOUT 62
#define STAT_ASSOCIATING 70
#define STAT_DEASSOCIATED 71
#define STAT_ASSOCTIMEOUT 72
#define STAT_NOTAIROAP 73
#define STAT_ASSOCIATED 80
#define STAT_LEAPING 90
#define STAT_LEAPFAILED 91
#define STAT_LEAPTIMEDOUT 92
#define STAT_LEAPCOMPLETE 93
} StatusRid;

typedef struct {
A
Al Viro 已提交
712 713 714
	__le16 len;
	__le16 spacer;
	__le32 vals[100];
L
Linus Torvalds 已提交
715 716 717 718
} StatsRid;


typedef struct {
A
Al Viro 已提交
719
	__le16 len;
L
Linus Torvalds 已提交
720 721 722 723
	u8 ap[4][ETH_ALEN];
} APListRid;

typedef struct {
724
	__le16 len;
L
Linus Torvalds 已提交
725 726
	char oui[3];
	char zero;
727
	__le16 prodNum;
L
Linus Torvalds 已提交
728 729 730 731 732
	char manName[32];
	char prodName[16];
	char prodVer[8];
	char factoryAddr[ETH_ALEN];
	char aironetAddr[ETH_ALEN];
733 734
	__le16 radioType;
	__le16 country;
L
Linus Torvalds 已提交
735 736 737 738
	char callid[ETH_ALEN];
	char supportedRates[8];
	char rxDiversity;
	char txDiversity;
739 740 741 742 743 744 745 746 747 748 749
	__le16 txPowerLevels[8];
	__le16 hardVer;
	__le16 hardCap;
	__le16 tempRange;
	__le16 softVer;
	__le16 softSubVer;
	__le16 interfaceVer;
	__le16 softCap;
	__le16 bootBlockVer;
	__le16 requiredHard;
	__le16 extSoftCap;
L
Linus Torvalds 已提交
750 751
} CapabilityRid;

752 753 754

/* Only present on firmware >= 5.30.17 */
typedef struct {
A
Al Viro 已提交
755
  __le16 unknown[4];
756 757 758 759
  u8 fixed[12]; /* WLAN management frame */
  u8 iep[624];
} BSSListRidExtra;

L
Linus Torvalds 已提交
760
typedef struct {
A
Al Viro 已提交
761 762
  __le16 len;
  __le16 index; /* First is 0 and 0xffff means end of list */
L
Linus Torvalds 已提交
763 764 765
#define RADIO_FH 1 /* Frequency hopping radio type */
#define RADIO_DS 2 /* Direct sequence radio type */
#define RADIO_TMA 4 /* Proprietary radio used in old cards (2500) */
A
Al Viro 已提交
766
  __le16 radioType;
L
Linus Torvalds 已提交
767 768 769 770
  u8 bssid[ETH_ALEN]; /* Mac address of the BSS */
  u8 zero;
  u8 ssidLen;
  u8 ssid[32];
A
Al Viro 已提交
771 772 773 774 775 776 777
  __le16 dBm;
#define CAP_ESS cpu_to_le16(1<<0)
#define CAP_IBSS cpu_to_le16(1<<1)
#define CAP_PRIVACY cpu_to_le16(1<<4)
#define CAP_SHORTHDR cpu_to_le16(1<<5)
  __le16 cap;
  __le16 beaconInterval;
L
Linus Torvalds 已提交
778 779
  u8 rates[8]; /* Same as rates for config rid */
  struct { /* For frequency hopping only */
A
Al Viro 已提交
780
    __le16 dwell;
L
Linus Torvalds 已提交
781 782 783 784 785
    u8 hopSet;
    u8 hopPattern;
    u8 hopIndex;
    u8 fill;
  } fh;
A
Al Viro 已提交
786 787
  __le16 dsChannel;
  __le16 atimWindow;
788 789 790

  /* Only present on firmware >= 5.30.17 */
  BSSListRidExtra extra;
L
Linus Torvalds 已提交
791 792
} BSSListRid;

793 794 795 796 797
typedef struct {
  BSSListRid bss;
  struct list_head list;
} BSSListElement;

L
Linus Torvalds 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
typedef struct {
  u8 rssipct;
  u8 rssidBm;
} tdsRssiEntry;

typedef struct {
  u16 len;
  tdsRssiEntry x[256];
} tdsRssiRid;

typedef struct {
	u16 len;
	u16 state;
	u16 multicastValid;
	u8  multicast[16];
	u16 unicastValid;
	u8  unicast[16];
} MICRid;

typedef struct {
A
Al Viro 已提交
818
	__be16 typelen;
L
Linus Torvalds 已提交
819 820 821 822 823 824 825 826 827 828 829

	union {
	    u8 snap[8];
	    struct {
		u8 dsap;
		u8 ssap;
		u8 control;
		u8 orgcode[3];
		u8 fieldtype[2];
	    } llc;
	} u;
A
Al Viro 已提交
830 831
	__be32 mic;
	__be32 seq;
L
Linus Torvalds 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
} MICBuffer;

typedef struct {
	u8 da[ETH_ALEN];
	u8 sa[ETH_ALEN];
} etherHead;

#pragma pack()

#define TXCTL_TXOK (1<<1) /* report if tx is ok */
#define TXCTL_TXEX (1<<2) /* report if tx fails */
#define TXCTL_802_3 (0<<3) /* 802.3 packet */
#define TXCTL_802_11 (1<<3) /* 802.11 mac packet */
#define TXCTL_ETHERNET (0<<4) /* payload has ethertype */
#define TXCTL_LLC (1<<4) /* payload is llc */
#define TXCTL_RELEASE (0<<5) /* release after completion */
#define TXCTL_NORELEASE (1<<5) /* on completion returns to host */

#define BUSY_FID 0x10000

#ifdef CISCO_EXT
#define AIROMAGIC	0xa55a
/* Warning : SIOCDEVPRIVATE may disapear during 2.5.X - Jean II */
#ifdef SIOCIWFIRSTPRIV
#ifdef SIOCDEVPRIVATE
#define AIROOLDIOCTL	SIOCDEVPRIVATE
#define AIROOLDIDIFC 	AIROOLDIOCTL + 1
#endif /* SIOCDEVPRIVATE */
#else /* SIOCIWFIRSTPRIV */
#define SIOCIWFIRSTPRIV SIOCDEVPRIVATE
#endif /* SIOCIWFIRSTPRIV */
/* This may be wrong. When using the new SIOCIWFIRSTPRIV range, we probably
 * should use only "GET" ioctls (last bit set to 1). "SET" ioctls are root
 * only and don't return the modified struct ifreq to the application which
 * is usually a problem. - Jean II */
#define AIROIOCTL	SIOCIWFIRSTPRIV
#define AIROIDIFC 	AIROIOCTL + 1

/* Ioctl constants to be used in airo_ioctl.command */

#define	AIROGCAP  		0	// Capability rid
#define AIROGCFG		1       // USED A LOT
#define AIROGSLIST		2	// System ID list
#define AIROGVLIST		3       // List of specified AP's
#define AIROGDRVNAM		4	//  NOTUSED
#define AIROGEHTENC		5	// NOTUSED
#define AIROGWEPKTMP		6
#define AIROGWEPKNV		7
#define AIROGSTAT		8
#define AIROGSTATSC32		9
#define AIROGSTATSD32		10
#define AIROGMICRID		11
#define AIROGMICSTATS		12
#define AIROGFLAGS		13
#define AIROGID			14
#define AIRORRID		15
#define AIRORSWVERSION		17

/* Leave gap of 40 commands after AIROGSTATSD32 for future */

#define AIROPCAP               	AIROGSTATSD32 + 40
#define AIROPVLIST              AIROPCAP      + 1
#define AIROPSLIST		AIROPVLIST    + 1
#define AIROPCFG		AIROPSLIST    + 1
#define AIROPSIDS		AIROPCFG      + 1
#define AIROPAPLIST		AIROPSIDS     + 1
#define AIROPMACON		AIROPAPLIST   + 1	/* Enable mac  */
#define AIROPMACOFF		AIROPMACON    + 1 	/* Disable mac */
#define AIROPSTCLR		AIROPMACOFF   + 1
#define AIROPWEPKEY		AIROPSTCLR    + 1
#define AIROPWEPKEYNV		AIROPWEPKEY   + 1
#define AIROPLEAPPWD            AIROPWEPKEYNV + 1
#define AIROPLEAPUSR            AIROPLEAPPWD  + 1

/* Flash codes */

#define AIROFLSHRST	       AIROPWEPKEYNV  + 40
#define AIROFLSHGCHR           AIROFLSHRST    + 1
#define AIROFLSHSTFL           AIROFLSHGCHR   + 1
#define AIROFLSHPCHR           AIROFLSHSTFL   + 1
#define AIROFLPUTBUF           AIROFLSHPCHR   + 1
#define AIRORESTART            AIROFLPUTBUF   + 1

#define FLASHSIZE	32768
#define AUXMEMSIZE	(256 * 1024)

typedef struct aironet_ioctl {
	unsigned short command;		// What to do
	unsigned short len;		// Len of data
	unsigned short ridnum;		// rid number
	unsigned char __user *data;	// d-data
} aironet_ioctl;

925
static char swversion[] = "2.1";
L
Linus Torvalds 已提交
926 927 928 929 930
#endif /* CISCO_EXT */

#define NUM_MODULES       2
#define MIC_MSGLEN_MAX    2400
#define EMMH32_MSGLEN_MAX MIC_MSGLEN_MAX
931
#define AIRO_DEF_MTU      2312
L
Linus Torvalds 已提交
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949

typedef struct {
	u32   size;            // size
	u8    enabled;         // MIC enabled or not
	u32   rxSuccess;       // successful packets received
	u32   rxIncorrectMIC;  // pkts dropped due to incorrect MIC comparison
	u32   rxNotMICed;      // pkts dropped due to not being MIC'd
	u32   rxMICPlummed;    // pkts dropped due to not having a MIC plummed
	u32   rxWrongSequence; // pkts dropped due to sequence number violation
	u32   reserve[32];
} mic_statistics;

typedef struct {
	u32 coeff[((EMMH32_MSGLEN_MAX)+3)>>2];
	u64 accum;	// accumulated mic, reduced to u32 in final()
	int position;	// current position (byte offset) in message
	union {
		u8  d8[4];
A
Al Viro 已提交
950
		__be32 d32;
L
Linus Torvalds 已提交
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
	} part;	// saves partial message word across update() calls
} emmh32_context;

typedef struct {
	emmh32_context seed;	    // Context - the seed
	u32		 rx;	    // Received sequence number
	u32		 tx;	    // Tx sequence number
	u32		 window;    // Start of window
	u8		 valid;	    // Flag to say if context is valid or not
	u8		 key[16];
} miccntx;

typedef struct {
	miccntx mCtx;		// Multicast context
	miccntx uCtx;		// Unicast context
} mic_module;

typedef struct {
	unsigned int  rid: 16;
	unsigned int  len: 15;
	unsigned int  valid: 1;
	dma_addr_t host_addr;
} Rid;

typedef struct {
	unsigned int  offset: 15;
	unsigned int  eoc: 1;
	unsigned int  len: 15;
	unsigned int  valid: 1;
	dma_addr_t host_addr;
} TxFid;

typedef struct {
	unsigned int  ctl: 15;
	unsigned int  rdy: 1;
	unsigned int  len: 15;
	unsigned int  valid: 1;
	dma_addr_t host_addr;
} RxFid;

/*
 * Host receive descriptor
 */
typedef struct {
	unsigned char __iomem *card_ram_off; /* offset into card memory of the
						desc */
	RxFid         rx_desc;		     /* card receive descriptor */
	char          *virtual_host_addr;    /* virtual address of host receive
					        buffer */
	int           pending;
} HostRxDesc;

/*
 * Host transmit descriptor
 */
typedef struct {
	unsigned char __iomem *card_ram_off;	     /* offset into card memory of the
						desc */
	TxFid         tx_desc;		     /* card transmit descriptor */
	char          *virtual_host_addr;    /* virtual address of host receive
					        buffer */
	int           pending;
} HostTxDesc;

/*
 * Host RID descriptor
 */
typedef struct {
	unsigned char __iomem *card_ram_off;      /* offset into card memory of the
					     descriptor */
	Rid           rid_desc;		  /* card RID descriptor */
	char          *virtual_host_addr; /* virtual address of host receive
					     buffer */
} HostRidDesc;

typedef struct {
	u16 sw0;
	u16 sw1;
	u16 status;
	u16 len;
#define HOST_SET (1 << 0)
#define HOST_INT_TX (1 << 1) /* Interrupt on successful TX */
#define HOST_INT_TXERR (1 << 2) /* Interrupt on unseccessful TX */
#define HOST_LCC_PAYLOAD (1 << 4) /* LLC payload, 0 = Ethertype */
#define HOST_DONT_RLSE (1 << 5) /* Don't release buffer when done */
#define HOST_DONT_RETRY (1 << 6) /* Don't retry trasmit */
#define HOST_CLR_AID (1 << 7) /* clear AID failure */
#define HOST_RTS (1 << 9) /* Force RTS use */
#define HOST_SHORT (1 << 10) /* Do short preamble */
	u16 ctl;
	u16 aid;
	u16 retries;
	u16 fill;
} TxCtlHdr;

typedef struct {
        u16 ctl;
        u16 duration;
        char addr1[6];
        char addr2[6];
        char addr3[6];
        u16 seq;
        char addr4[6];
} WifiHdr;


typedef struct {
	TxCtlHdr ctlhdr;
	u16 fill1;
	u16 fill2;
	WifiHdr wifihdr;
	u16 gaplen;
	u16 status;
} WifiCtlHdr;

J
Jouni Malinen 已提交
1066
static WifiCtlHdr wifictlhdr8023 = {
L
Linus Torvalds 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
	.ctlhdr = {
		.ctl	= HOST_DONT_RLSE,
	}
};

// Frequency list (map channels to frequencies)
static const long frequency_list[] = { 2412, 2417, 2422, 2427, 2432, 2437, 2442,
				2447, 2452, 2457, 2462, 2467, 2472, 2484 };

// A few details needed for WEP (Wireless Equivalent Privacy)
#define MAX_KEY_SIZE 13			// 128 (?) bits
#define MIN_KEY_SIZE  5			// 40 bits RC4 - WEP
typedef struct wep_key_t {
	u16	len;
	u8	key[16];	/* 40-bit and 104-bit keys */
} wep_key_t;

/* Backward compatibility */
#ifndef IW_ENCODE_NOKEY
#define IW_ENCODE_NOKEY         0x0800  /* Key is write only, so not present */
#define IW_ENCODE_MODE  (IW_ENCODE_DISABLED | IW_ENCODE_RESTRICTED | IW_ENCODE_OPEN)
#endif /* IW_ENCODE_NOKEY */

/* List of Wireless Handlers (new API) */
static const struct iw_handler_def	airo_handler_def;

static const char version[] = "airo.c 0.6 (Ben Reed & Javier Achirica)";

struct airo_info;

static int get_dec_u16( char *buffer, int *start, int limit );
static void OUT4500( struct airo_info *, u16 register, u16 value );
static unsigned short IN4500( struct airo_info *, u16 register );
static u16 setup_card(struct airo_info*, u8 *mac, int lock);
1101
static int enable_MAC(struct airo_info *ai, int lock);
L
Linus Torvalds 已提交
1102 1103 1104 1105 1106
static void disable_MAC(struct airo_info *ai, int lock);
static void enable_interrupts(struct airo_info*);
static void disable_interrupts(struct airo_info*);
static u16 issuecommand(struct airo_info*, Cmd *pCmd, Resp *pRsp);
static int bap_setup(struct airo_info*, u16 rid, u16 offset, int whichbap);
1107
static int aux_bap_read(struct airo_info*, __le16 *pu16Dst, int bytelen,
L
Linus Torvalds 已提交
1108
			int whichbap);
1109
static int fast_bap_read(struct airo_info*, __le16 *pu16Dst, int bytelen,
L
Linus Torvalds 已提交
1110
			 int whichbap);
1111
static int bap_write(struct airo_info*, const __le16 *pu16Src, int bytelen,
L
Linus Torvalds 已提交
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
		     int whichbap);
static int PC4500_accessrid(struct airo_info*, u16 rid, u16 accmd);
static int PC4500_readrid(struct airo_info*, u16 rid, void *pBuf, int len, int lock);
static int PC4500_writerid(struct airo_info*, u16 rid, const void
			   *pBuf, int len, int lock);
static int do_writerid( struct airo_info*, u16 rid, const void *rid_data,
			int len, int dummy );
static u16 transmit_allocate(struct airo_info*, int lenPayload, int raw);
static int transmit_802_3_packet(struct airo_info*, int len, char *pPacket);
static int transmit_802_11_packet(struct airo_info*, int len, char *pPacket);

static int mpi_send_packet (struct net_device *dev);
static void mpi_unmap_card(struct pci_dev *pci);
static void mpi_receive_802_3(struct airo_info *ai);
static void mpi_receive_802_11(struct airo_info *ai);
static int waitbusy (struct airo_info *ai);

1129
static irqreturn_t airo_interrupt( int irq, void* dev_id);
L
Linus Torvalds 已提交
1130 1131 1132
static int airo_thread(void *data);
static void timer_func( struct net_device *dev );
static int airo_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
J
Jouni Malinen 已提交
1133
static struct iw_statistics *airo_get_wireless_stats (struct net_device *dev);
L
Linus Torvalds 已提交
1134 1135 1136 1137
static void airo_read_wireless_stats (struct airo_info *local);
#ifdef CISCO_EXT
static int readrids(struct net_device *dev, aironet_ioctl *comp);
static int writerids(struct net_device *dev, aironet_ioctl *comp);
J
Jouni Malinen 已提交
1138
static int flashcard(struct net_device *dev, aironet_ioctl *comp);
L
Linus Torvalds 已提交
1139 1140 1141 1142 1143 1144
#endif /* CISCO_EXT */
static void micinit(struct airo_info *ai);
static int micsetup(struct airo_info *ai);
static int encapsulate(struct airo_info *ai, etherHead *pPacket, MICBuffer *buffer, int len);
static int decapsulate(struct airo_info *ai, MICBuffer *mic, etherHead *pPacket, u16 payLen);

1145 1146 1147
static u8 airo_rssi_to_dbm (tdsRssiEntry *rssi_rid, u8 rssi);
static u8 airo_dbm_to_pct (tdsRssiEntry *rssi_rid, u8 dbm);

1148 1149
static void airo_networks_free(struct airo_info *ai);

L
Linus Torvalds 已提交
1150 1151 1152
struct airo_info {
	struct net_device_stats	stats;
	struct net_device             *dev;
1153
	struct list_head              dev_list;
L
Linus Torvalds 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
	/* Note, we can have MAX_FIDS outstanding.  FIDs are 16-bits, so we
	   use the high bit to mark whether it is in use. */
#define MAX_FIDS 6
#define MPI_MAX_FIDS 1
	int                           fids[MAX_FIDS];
	ConfigRid config;
	char keyindex; // Used with auto wep
	char defindex; // Used with auto wep
	struct proc_dir_entry *proc_entry;
        spinlock_t aux_lock;
#define FLAG_RADIO_OFF	0	/* User disabling of MAC */
#define FLAG_RADIO_DOWN	1	/* ifup/ifdown disabling of MAC */
#define FLAG_RADIO_MASK 0x03
#define FLAG_ENABLED	2
#define FLAG_ADHOC	3	/* Needed by MIC */
#define FLAG_MIC_CAPABLE 4
#define FLAG_UPDATE_MULTI 5
#define FLAG_UPDATE_UNI 6
#define FLAG_802_11	7
1173
#define FLAG_PROMISC	8	/* IFF_PROMISC 0x100 - include/linux/if.h */
L
Linus Torvalds 已提交
1174 1175 1176 1177 1178 1179 1180
#define FLAG_PENDING_XMIT 9
#define FLAG_PENDING_XMIT11 10
#define FLAG_MPI	11
#define FLAG_REGISTERED	12
#define FLAG_COMMIT	13
#define FLAG_RESET	14
#define FLAG_FLASHING	15
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
#define FLAG_WPA_CAPABLE	16
	unsigned long flags;
#define JOB_DIE	0
#define JOB_XMIT	1
#define JOB_XMIT11	2
#define JOB_STATS	3
#define JOB_PROMISC	4
#define JOB_MIC	5
#define JOB_EVENT	6
#define JOB_AUTOWEP	7
#define JOB_WSTATS	8
#define JOB_SCAN_RESULTS  9
	unsigned long jobs;
1194
	int (*bap_read)(struct airo_info*, __le16 *pu16Dst, int bytelen,
L
Linus Torvalds 已提交
1195 1196 1197
			int whichbap);
	unsigned short *flash;
	tdsRssiEntry *rssi;
S
Sukadev Bhattiprolu 已提交
1198 1199
	struct task_struct *list_bss_task;
	struct task_struct *airo_thread_task;
L
Linus Torvalds 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208
	struct semaphore sem;
	wait_queue_head_t thr_wait;
	unsigned long expires;
	struct {
		struct sk_buff *skb;
		int fid;
	} xmit, xmit11;
	struct net_device *wifidev;
	struct iw_statistics	wstats;		// wireless stats
1209
	unsigned long		scan_timeout;	/* Time scan should be read */
L
Linus Torvalds 已提交
1210 1211 1212
	struct iw_spy_data	spy_data;
	struct iw_public_data	wireless_data;
	/* MIC stuff */
1213
	struct crypto_cipher	*tfm;
L
Linus Torvalds 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
	mic_module		mod[2];
	mic_statistics		micstats;
	HostRxDesc rxfids[MPI_MAX_FIDS]; // rx/tx/config MPI350 descriptors
	HostTxDesc txfids[MPI_MAX_FIDS];
	HostRidDesc config_desc;
	unsigned long ridbus; // phys addr of config_desc
	struct sk_buff_head txq;// tx queue used by mpi350 code
	struct pci_dev          *pci;
	unsigned char		__iomem *pcimem;
	unsigned char		__iomem *pciaux;
	unsigned char		*shared;
	dma_addr_t		shared_dma;
1226
	pm_message_t		power;
L
Linus Torvalds 已提交
1227 1228 1229 1230
	SsidRid			*SSID;
	APListRid		*APList;
#define	PCI_SHARED_LEN		2*MPI_MAX_FIDS*PKTSIZE+RIDSIZE
	char			proc_name[IFNAMSIZ];
1231

1232 1233 1234 1235 1236
	/* WPA-related stuff */
	unsigned int bssListFirst;
	unsigned int bssListNext;
	unsigned int bssListRidLen;

1237 1238 1239
	struct list_head network_list;
	struct list_head network_free_list;
	BSSListElement *networks;
L
Linus Torvalds 已提交
1240 1241
};

1242 1243 1244
static inline int bap_read(struct airo_info *ai, __le16 *pu16Dst, int bytelen,
			   int whichbap)
{
L
Linus Torvalds 已提交
1245 1246 1247 1248 1249 1250 1251 1252
	return ai->bap_read(ai, pu16Dst, bytelen, whichbap);
}

static int setup_proc_entry( struct net_device *dev,
			     struct airo_info *apriv );
static int takedown_proc_entry( struct net_device *dev,
				struct airo_info *apriv );

J
Jouni Malinen 已提交
1253 1254 1255 1256 1257 1258
static int cmdreset(struct airo_info *ai);
static int setflashmode (struct airo_info *ai);
static int flashgchar(struct airo_info *ai,int matchbyte,int dwelltime);
static int flashputbuf(struct airo_info *ai);
static int flashrestart(struct airo_info *ai,struct net_device *dev);

1259
#define airo_print(type, name, fmt, args...) \
1260
	printk(type DRV_NAME "(%s): " fmt "\n", name, ##args)
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

#define airo_print_info(name, fmt, args...) \
	airo_print(KERN_INFO, name, fmt, ##args)

#define airo_print_dbg(name, fmt, args...) \
	airo_print(KERN_DEBUG, name, fmt, ##args)

#define airo_print_warn(name, fmt, args...) \
	airo_print(KERN_WARNING, name, fmt, ##args)

#define airo_print_err(name, fmt, args...) \
	airo_print(KERN_ERR, name, fmt, ##args)


L
Linus Torvalds 已提交
1275 1276 1277 1278 1279 1280 1281
/***********************************************************************
 *                              MIC ROUTINES                           *
 ***********************************************************************
 */

static int RxSeqValid (struct airo_info *ai,miccntx *context,int mcast,u32 micSeq);
static void MoveWindow(miccntx *context, u32 micSeq);
1282 1283
static void emmh32_setseed(emmh32_context *context, u8 *pkey, int keylen,
			   struct crypto_cipher *tfm);
J
Jouni Malinen 已提交
1284 1285 1286 1287
static void emmh32_init(emmh32_context *context);
static void emmh32_update(emmh32_context *context, u8 *pOctets, int len);
static void emmh32_final(emmh32_context *context, u8 digest[4]);
static int flashpchar(struct airo_info *ai,int byte,int dwelltime);
L
Linus Torvalds 已提交
1288 1289 1290 1291 1292 1293 1294

/* micinit - Initialize mic seed */

static void micinit(struct airo_info *ai)
{
	MICRid mic_rid;

1295
	clear_bit(JOB_MIC, &ai->jobs);
L
Linus Torvalds 已提交
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
	PC4500_readrid(ai, RID_MIC, &mic_rid, sizeof(mic_rid), 0);
	up(&ai->sem);

	ai->micstats.enabled = (mic_rid.state & 0x00FF) ? 1 : 0;

	if (ai->micstats.enabled) {
		/* Key must be valid and different */
		if (mic_rid.multicastValid && (!ai->mod[0].mCtx.valid ||
		    (memcmp (ai->mod[0].mCtx.key, mic_rid.multicast,
			     sizeof(ai->mod[0].mCtx.key)) != 0))) {
			/* Age current mic Context */
			memcpy(&ai->mod[1].mCtx,&ai->mod[0].mCtx,sizeof(miccntx));
			/* Initialize new context */
			memcpy(&ai->mod[0].mCtx.key,mic_rid.multicast,sizeof(mic_rid.multicast));
			ai->mod[0].mCtx.window  = 33; //Window always points to the middle
			ai->mod[0].mCtx.rx      = 0;  //Rx Sequence numbers
			ai->mod[0].mCtx.tx      = 0;  //Tx sequence numbers
			ai->mod[0].mCtx.valid   = 1;  //Key is now valid
  
			/* Give key to mic seed */
			emmh32_setseed(&ai->mod[0].mCtx.seed,mic_rid.multicast,sizeof(mic_rid.multicast), ai->tfm);
		}

		/* Key must be valid and different */
		if (mic_rid.unicastValid && (!ai->mod[0].uCtx.valid || 
		    (memcmp(ai->mod[0].uCtx.key, mic_rid.unicast,
			    sizeof(ai->mod[0].uCtx.key)) != 0))) {
			/* Age current mic Context */
			memcpy(&ai->mod[1].uCtx,&ai->mod[0].uCtx,sizeof(miccntx));
			/* Initialize new context */
			memcpy(&ai->mod[0].uCtx.key,mic_rid.unicast,sizeof(mic_rid.unicast));
	
			ai->mod[0].uCtx.window  = 33; //Window always points to the middle
			ai->mod[0].uCtx.rx      = 0;  //Rx Sequence numbers
			ai->mod[0].uCtx.tx      = 0;  //Tx sequence numbers
			ai->mod[0].uCtx.valid   = 1;  //Key is now valid
	
			//Give key to mic seed
			emmh32_setseed(&ai->mod[0].uCtx.seed, mic_rid.unicast, sizeof(mic_rid.unicast), ai->tfm);
		}
	} else {
      /* So next time we have a valid key and mic is enabled, we will update
       * the sequence number if the key is the same as before.
       */
		ai->mod[0].uCtx.valid = 0;
		ai->mod[0].mCtx.valid = 0;
	}
}

/* micsetup - Get ready for business */

static int micsetup(struct airo_info *ai) {
	int i;

	if (ai->tfm == NULL)
1351
	        ai->tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
L
Linus Torvalds 已提交
1352

1353
        if (IS_ERR(ai->tfm)) {
1354
                airo_print_err(ai->dev->name, "failed to load transform for AES");
1355
                ai->tfm = NULL;
L
Linus Torvalds 已提交
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
                return ERROR;
        }

	for (i=0; i < NUM_MODULES; i++) {
		memset(&ai->mod[i].mCtx,0,sizeof(miccntx));
		memset(&ai->mod[i].uCtx,0,sizeof(miccntx));
	}
	return SUCCESS;
}

J
Jouni Malinen 已提交
1366
static char micsnap[] = {0xAA,0xAA,0x03,0x00,0x40,0x96,0x00,0x02};
L
Linus Torvalds 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620

/*===========================================================================
 * Description: Mic a packet
 *    
 *      Inputs: etherHead * pointer to an 802.3 frame
 *    
 *     Returns: BOOLEAN if successful, otherwise false.
 *             PacketTxLen will be updated with the mic'd packets size.
 *
 *    Caveats: It is assumed that the frame buffer will already
 *             be big enough to hold the largets mic message possible.
 *            (No memory allocation is done here).
 *  
 *    Author: sbraneky (10/15/01)
 *    Merciless hacks by rwilcher (1/14/02)
 */

static int encapsulate(struct airo_info *ai ,etherHead *frame, MICBuffer *mic, int payLen)
{
	miccntx   *context;

	// Determine correct context
	// If not adhoc, always use unicast key

	if (test_bit(FLAG_ADHOC, &ai->flags) && (frame->da[0] & 0x1))
		context = &ai->mod[0].mCtx;
	else
		context = &ai->mod[0].uCtx;
  
	if (!context->valid)
		return ERROR;

	mic->typelen = htons(payLen + 16); //Length of Mic'd packet

	memcpy(&mic->u.snap, micsnap, sizeof(micsnap)); // Add Snap

	// Add Tx sequence
	mic->seq = htonl(context->tx);
	context->tx += 2;

	emmh32_init(&context->seed); // Mic the packet
	emmh32_update(&context->seed,frame->da,ETH_ALEN * 2); // DA,SA
	emmh32_update(&context->seed,(u8*)&mic->typelen,10); // Type/Length and Snap
	emmh32_update(&context->seed,(u8*)&mic->seq,sizeof(mic->seq)); //SEQ
	emmh32_update(&context->seed,frame->da + ETH_ALEN * 2,payLen); //payload
	emmh32_final(&context->seed, (u8*)&mic->mic);

	/*    New Type/length ?????????? */
	mic->typelen = 0; //Let NIC know it could be an oversized packet
	return SUCCESS;
}

typedef enum {
    NONE,
    NOMIC,
    NOMICPLUMMED,
    SEQUENCE,
    INCORRECTMIC,
} mic_error;

/*===========================================================================
 *  Description: Decapsulates a MIC'd packet and returns the 802.3 packet
 *               (removes the MIC stuff) if packet is a valid packet.
 *      
 *       Inputs: etherHead  pointer to the 802.3 packet             
 *     
 *      Returns: BOOLEAN - TRUE if packet should be dropped otherwise FALSE
 *     
 *      Author: sbraneky (10/15/01)
 *    Merciless hacks by rwilcher (1/14/02)
 *---------------------------------------------------------------------------
 */

static int decapsulate(struct airo_info *ai, MICBuffer *mic, etherHead *eth, u16 payLen)
{
	int      i;
	u32      micSEQ;
	miccntx  *context;
	u8       digest[4];
	mic_error micError = NONE;

	// Check if the packet is a Mic'd packet

	if (!ai->micstats.enabled) {
		//No Mic set or Mic OFF but we received a MIC'd packet.
		if (memcmp ((u8*)eth + 14, micsnap, sizeof(micsnap)) == 0) {
			ai->micstats.rxMICPlummed++;
			return ERROR;
		}
		return SUCCESS;
	}

	if (ntohs(mic->typelen) == 0x888E)
		return SUCCESS;

	if (memcmp (mic->u.snap, micsnap, sizeof(micsnap)) != 0) {
	    // Mic enabled but packet isn't Mic'd
		ai->micstats.rxMICPlummed++;
	    	return ERROR;
	}

	micSEQ = ntohl(mic->seq);            //store SEQ as CPU order

	//At this point we a have a mic'd packet and mic is enabled
	//Now do the mic error checking.

	//Receive seq must be odd
	if ( (micSEQ & 1) == 0 ) {
		ai->micstats.rxWrongSequence++;
		return ERROR;
	}

	for (i = 0; i < NUM_MODULES; i++) {
		int mcast = eth->da[0] & 1;
		//Determine proper context 
		context = mcast ? &ai->mod[i].mCtx : &ai->mod[i].uCtx;
	
		//Make sure context is valid
		if (!context->valid) {
			if (i == 0)
				micError = NOMICPLUMMED;
			continue;                
		}
	       	//DeMic it 

		if (!mic->typelen)
			mic->typelen = htons(payLen + sizeof(MICBuffer) - 2);
	
		emmh32_init(&context->seed);
		emmh32_update(&context->seed, eth->da, ETH_ALEN*2); 
		emmh32_update(&context->seed, (u8 *)&mic->typelen, sizeof(mic->typelen)+sizeof(mic->u.snap)); 
		emmh32_update(&context->seed, (u8 *)&mic->seq,sizeof(mic->seq));	
		emmh32_update(&context->seed, eth->da + ETH_ALEN*2,payLen);	
		//Calculate MIC
		emmh32_final(&context->seed, digest);
	
		if (memcmp(digest, &mic->mic, 4)) { //Make sure the mics match
		  //Invalid Mic
			if (i == 0)
				micError = INCORRECTMIC;
			continue;
		}

		//Check Sequence number if mics pass
		if (RxSeqValid(ai, context, mcast, micSEQ) == SUCCESS) {
			ai->micstats.rxSuccess++;
			return SUCCESS;
		}
		if (i == 0)
			micError = SEQUENCE;
	}

	// Update statistics
	switch (micError) {
		case NOMICPLUMMED: ai->micstats.rxMICPlummed++;   break;
		case SEQUENCE:    ai->micstats.rxWrongSequence++; break;
		case INCORRECTMIC: ai->micstats.rxIncorrectMIC++; break;
		case NONE:  break;
		case NOMIC: break;
	}
	return ERROR;
}

/*===========================================================================
 * Description:  Checks the Rx Seq number to make sure it is valid
 *               and hasn't already been received
 *   
 *     Inputs: miccntx - mic context to check seq against
 *             micSeq  - the Mic seq number
 *   
 *    Returns: TRUE if valid otherwise FALSE. 
 *
 *    Author: sbraneky (10/15/01)
 *    Merciless hacks by rwilcher (1/14/02)
 *---------------------------------------------------------------------------
 */

static int RxSeqValid (struct airo_info *ai,miccntx *context,int mcast,u32 micSeq)
{
	u32 seq,index;

	//Allow for the ap being rebooted - if it is then use the next 
	//sequence number of the current sequence number - might go backwards

	if (mcast) {
		if (test_bit(FLAG_UPDATE_MULTI, &ai->flags)) {
			clear_bit (FLAG_UPDATE_MULTI, &ai->flags);
			context->window = (micSeq > 33) ? micSeq : 33;
			context->rx     = 0;        // Reset rx
		}
	} else if (test_bit(FLAG_UPDATE_UNI, &ai->flags)) {
		clear_bit (FLAG_UPDATE_UNI, &ai->flags);
		context->window = (micSeq > 33) ? micSeq : 33; // Move window
		context->rx     = 0;        // Reset rx
	}

	//Make sequence number relative to START of window
	seq = micSeq - (context->window - 33);

	//Too old of a SEQ number to check.
	if ((s32)seq < 0)
		return ERROR;
    
	if ( seq > 64 ) {
		//Window is infinite forward
		MoveWindow(context,micSeq);
		return SUCCESS;
	}

	// We are in the window. Now check the context rx bit to see if it was already sent
	seq >>= 1;         //divide by 2 because we only have odd numbers
	index = 1 << seq;  //Get an index number

	if (!(context->rx & index)) {
		//micSEQ falls inside the window.
		//Add seqence number to the list of received numbers.
		context->rx |= index;

		MoveWindow(context,micSeq);

		return SUCCESS;
	}
	return ERROR;
}

static void MoveWindow(miccntx *context, u32 micSeq)
{
	u32 shift;

	//Move window if seq greater than the middle of the window
	if (micSeq > context->window) {
		shift = (micSeq - context->window) >> 1;
    
		    //Shift out old
		if (shift < 32)
			context->rx >>= shift;
		else
			context->rx = 0;

		context->window = micSeq;      //Move window
	}
}

/*==============================================*/
/*========== EMMH ROUTINES  ====================*/
/*==============================================*/

/* mic accumulate */
#define MIC_ACCUM(val)	\
	context->accum += (u64)(val) * context->coeff[coeff_position++];

static unsigned char aes_counter[16];

/* expand the key to fill the MMH coefficient array */
1621 1622
static void emmh32_setseed(emmh32_context *context, u8 *pkey, int keylen,
			   struct crypto_cipher *tfm)
L
Linus Torvalds 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
{
  /* take the keying material, expand if necessary, truncate at 16-bytes */
  /* run through AES counter mode to generate context->coeff[] */
  
	int i,j;
	u32 counter;
	u8 *cipher, plain[16];

	crypto_cipher_setkey(tfm, pkey, 16);
	counter = 0;
1633
	for (i = 0; i < ARRAY_SIZE(context->coeff); ) {
L
Linus Torvalds 已提交
1634 1635 1636 1637 1638 1639
		aes_counter[15] = (u8)(counter >> 0);
		aes_counter[14] = (u8)(counter >> 8);
		aes_counter[13] = (u8)(counter >> 16);
		aes_counter[12] = (u8)(counter >> 24);
		counter++;
		memcpy (plain, aes_counter, 16);
1640 1641
		crypto_cipher_encrypt_one(tfm, plain, plain);
		cipher = plain;
1642
		for (j = 0; (j < 16) && (i < ARRAY_SIZE(context->coeff)); ) {
A
Al Viro 已提交
1643
			context->coeff[i++] = ntohl(*(__be32 *)&cipher[j]);
L
Linus Torvalds 已提交
1644 1645 1646 1647 1648 1649
			j += 4;
		}
	}
}

/* prepare for calculation of a new mic */
J
Jouni Malinen 已提交
1650
static void emmh32_init(emmh32_context *context)
L
Linus Torvalds 已提交
1651 1652 1653 1654 1655 1656 1657
{
	/* prepare for new mic calculation */
	context->accum = 0;
	context->position = 0;
}

/* add some bytes to the mic calculation */
J
Jouni Malinen 已提交
1658
static void emmh32_update(emmh32_context *context, u8 *pOctets, int len)
L
Linus Torvalds 已提交
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
{
	int	coeff_position, byte_position;
  
	if (len == 0) return;
  
	coeff_position = context->position >> 2;
  
	/* deal with partial 32-bit word left over from last update */
	byte_position = context->position & 3;
	if (byte_position) {
		/* have a partial word in part to deal with */
		do {
			if (len == 0) return;
			context->part.d8[byte_position++] = *pOctets++;
			context->position++;
			len--;
		} while (byte_position < 4);
A
Al Viro 已提交
1676
		MIC_ACCUM(ntohl(context->part.d32));
L
Linus Torvalds 已提交
1677 1678 1679 1680
	}

	/* deal with full 32-bit words */
	while (len >= 4) {
A
Al Viro 已提交
1681
		MIC_ACCUM(ntohl(*(__be32 *)pOctets));
L
Linus Torvalds 已提交
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
		context->position += 4;
		pOctets += 4;
		len -= 4;
	}

	/* deal with partial 32-bit word that will be left over from this update */
	byte_position = 0;
	while (len > 0) {
		context->part.d8[byte_position++] = *pOctets++;
		context->position++;
		len--;
	}
}

/* mask used to zero empty bytes for final partial word */
static u32 mask32[4] = { 0x00000000L, 0xFF000000L, 0xFFFF0000L, 0xFFFFFF00L };

/* calculate the mic */
J
Jouni Malinen 已提交
1700
static void emmh32_final(emmh32_context *context, u8 digest[4])
L
Linus Torvalds 已提交
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
{
	int	coeff_position, byte_position;
	u32	val;
  
	u64 sum, utmp;
	s64 stmp;

	coeff_position = context->position >> 2;
  
	/* deal with partial 32-bit word left over from last update */
	byte_position = context->position & 3;
	if (byte_position) {
		/* have a partial word in part to deal with */
A
Al Viro 已提交
1714
		val = ntohl(context->part.d32);
L
Linus Torvalds 已提交
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
		MIC_ACCUM(val & mask32[byte_position]);	/* zero empty bytes */
	}

	/* reduce the accumulated u64 to a 32-bit MIC */
	sum = context->accum;
	stmp = (sum  & 0xffffffffLL) - ((sum >> 32)  * 15);
	utmp = (stmp & 0xffffffffLL) - ((stmp >> 32) * 15);
	sum = utmp & 0xffffffffLL;
	if (utmp > 0x10000000fLL)
		sum -= 15;

	val = (u32)sum;
	digest[0] = (val>>24) & 0xFF;
	digest[1] = (val>>16) & 0xFF;
	digest[2] = (val>>8) & 0xFF;
	digest[3] = val & 0xFF;
}

static int readBSSListRid(struct airo_info *ai, int first,
A
Al Viro 已提交
1734 1735
		      BSSListRid *list)
{
1736 1737
	Cmd cmd;
	Resp rsp;
L
Linus Torvalds 已提交
1738 1739

	if (first == 1) {
1740 1741 1742 1743 1744
		if (ai->flags & FLAG_RADIO_MASK) return -ENETDOWN;
		memset(&cmd, 0, sizeof(cmd));
		cmd.cmd=CMD_LISTBSS;
		if (down_interruptible(&ai->sem))
			return -ERESTARTSYS;
S
Sukadev Bhattiprolu 已提交
1745
		ai->list_bss_task = current;
1746 1747 1748
		issuecommand(ai, &cmd, &rsp);
		up(&ai->sem);
		/* Let the command take effect */
S
Sukadev Bhattiprolu 已提交
1749 1750
		schedule_timeout_uninterruptible(3 * HZ);
		ai->list_bss_task = NULL;
1751
	}
A
Al Viro 已提交
1752
	return PC4500_readrid(ai, first ? ai->bssListFirst : ai->bssListNext,
1753
			    list, ai->bssListRidLen, 1);
L
Linus Torvalds 已提交
1754 1755
}

A
Al Viro 已提交
1756 1757 1758
static int readWepKeyRid(struct airo_info *ai, WepKeyRid *wkr, int temp, int lock)
{
	return PC4500_readrid(ai, temp ? RID_WEP_TEMP : RID_WEP_PERM,
L
Linus Torvalds 已提交
1759 1760 1761
				wkr, sizeof(*wkr), lock);
}

A
Al Viro 已提交
1762 1763 1764 1765 1766 1767
static int writeWepKeyRid(struct airo_info *ai, WepKeyRid *wkr, int perm, int lock)
{
	int rc;
	rc = PC4500_writerid(ai, RID_WEP_TEMP, wkr, sizeof(*wkr), lock);
	if (rc!=SUCCESS)
		airo_print_err(ai->dev->name, "WEP_TEMP set %x", rc);
L
Linus Torvalds 已提交
1768
	if (perm) {
A
Al Viro 已提交
1769 1770
		rc = PC4500_writerid(ai, RID_WEP_PERM, wkr, sizeof(*wkr), lock);
		if (rc!=SUCCESS)
1771
			airo_print_err(ai->dev->name, "WEP_PERM set %x", rc);
L
Linus Torvalds 已提交
1772 1773 1774 1775
	}
	return rc;
}

A
Al Viro 已提交
1776 1777 1778
static int readSsidRid(struct airo_info*ai, SsidRid *ssidr)
{
	return PC4500_readrid(ai, RID_SSID, ssidr, sizeof(*ssidr), 1);
L
Linus Torvalds 已提交
1779 1780
}

A
Al Viro 已提交
1781 1782 1783
static int writeSsidRid(struct airo_info*ai, SsidRid *pssidr, int lock)
{
	return PC4500_writerid(ai, RID_SSID, pssidr, sizeof(*pssidr), lock);
L
Linus Torvalds 已提交
1784
}
A
Al Viro 已提交
1785

A
Al Viro 已提交
1786 1787
static int readConfigRid(struct airo_info *ai, int lock)
{
L
Linus Torvalds 已提交
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
	int rc;
	ConfigRid cfg;

	if (ai->config.len)
		return SUCCESS;

	rc = PC4500_readrid(ai, RID_ACTUALCONFIG, &cfg, sizeof(cfg), lock);
	if (rc != SUCCESS)
		return rc;

	ai->config = cfg;
	return SUCCESS;
}
A
Al Viro 已提交
1801 1802 1803

static inline void checkThrottle(struct airo_info *ai)
{
L
Linus Torvalds 已提交
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
	int i;
/* Old hardware had a limit on encryption speed */
	if (ai->config.authType != AUTH_OPEN && maxencrypt) {
		for(i=0; i<8; i++) {
			if (ai->config.rates[i] > maxencrypt) {
				ai->config.rates[i] = 0;
			}
		}
	}
}
A
Al Viro 已提交
1814 1815 1816

static int writeConfigRid(struct airo_info *ai, int lock)
{
L
Linus Torvalds 已提交
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
	ConfigRid cfgr;

	if (!test_bit (FLAG_COMMIT, &ai->flags))
		return SUCCESS;

	clear_bit (FLAG_COMMIT, &ai->flags);
	clear_bit (FLAG_RESET, &ai->flags);
	checkThrottle(ai);
	cfgr = ai->config;

A
Al Viro 已提交
1827
	if ((cfgr.opmode & MODE_CFG_MASK) == MODE_STA_IBSS)
L
Linus Torvalds 已提交
1828 1829 1830 1831 1832 1833 1834
		set_bit(FLAG_ADHOC, &ai->flags);
	else
		clear_bit(FLAG_ADHOC, &ai->flags);

	return PC4500_writerid( ai, RID_CONFIG, &cfgr, sizeof(cfgr), lock);
}

A
Al Viro 已提交
1835 1836 1837
static int readStatusRid(struct airo_info *ai, StatusRid *statr, int lock)
{
	return PC4500_readrid(ai, RID_STATUS, statr, sizeof(*statr), lock);
L
Linus Torvalds 已提交
1838
}
A
Al Viro 已提交
1839 1840 1841 1842

static int readAPListRid(struct airo_info *ai, APListRid *aplr)
{
	return PC4500_readrid(ai, RID_APLIST, aplr, sizeof(*aplr), 1);
L
Linus Torvalds 已提交
1843
}
A
Al Viro 已提交
1844 1845 1846 1847

static int writeAPListRid(struct airo_info *ai, APListRid *aplr, int lock)
{
	return PC4500_writerid(ai, RID_APLIST, aplr, sizeof(*aplr), lock);
L
Linus Torvalds 已提交
1848 1849
}

1850 1851 1852
static int readCapabilityRid(struct airo_info *ai, CapabilityRid *capr, int lock)
{
	return PC4500_readrid(ai, RID_CAPABILITIES, capr, sizeof(*capr), lock);
L
Linus Torvalds 已提交
1853 1854
}

A
Al Viro 已提交
1855 1856 1857
static int readStatsRid(struct airo_info*ai, StatsRid *sr, int rid, int lock)
{
	return PC4500_readrid(ai, rid, sr, sizeof(*sr), lock);
L
Linus Torvalds 已提交
1858 1859
}

1860 1861 1862 1863 1864 1865 1866 1867
static void try_auto_wep(struct airo_info *ai)
{
	if (auto_wep && !(ai->flags & FLAG_RADIO_DOWN)) {
		ai->expires = RUN_AT(3*HZ);
		wake_up_interruptible(&ai->thr_wait);
	}
}

L
Linus Torvalds 已提交
1868
static int airo_open(struct net_device *dev) {
1869 1870
	struct airo_info *ai = dev->priv;
	int rc = 0;
L
Linus Torvalds 已提交
1871

1872
	if (test_bit(FLAG_FLASHING, &ai->flags))
L
Linus Torvalds 已提交
1873 1874 1875 1876 1877 1878
		return -EIO;

	/* Make sure the card is configured.
	 * Wireless Extensions may postpone config changes until the card
	 * is open (to pipeline changes and speed-up card setup). If
	 * those changes are not yet commited, do it now - Jean II */
1879 1880 1881
	if (test_bit(FLAG_COMMIT, &ai->flags)) {
		disable_MAC(ai, 1);
		writeConfigRid(ai, 1);
L
Linus Torvalds 已提交
1882 1883
	}

1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
	if (ai->wifidev != dev) {
		clear_bit(JOB_DIE, &ai->jobs);
		ai->airo_thread_task = kthread_run(airo_thread, dev, dev->name);
		if (IS_ERR(ai->airo_thread_task))
			return (int)PTR_ERR(ai->airo_thread_task);

		rc = request_irq(dev->irq, airo_interrupt, IRQF_SHARED,
			dev->name, dev);
		if (rc) {
			airo_print_err(dev->name,
				"register interrupt %d failed, rc %d",
				dev->irq, rc);
			set_bit(JOB_DIE, &ai->jobs);
			kthread_stop(ai->airo_thread_task);
			return rc;
		}

L
Linus Torvalds 已提交
1901
		/* Power on the MAC controller (which may have been disabled) */
1902 1903 1904 1905
		clear_bit(FLAG_RADIO_DOWN, &ai->flags);
		enable_interrupts(ai);

		try_auto_wep(ai);
L
Linus Torvalds 已提交
1906
	}
1907
	enable_MAC(ai, 1);
L
Linus Torvalds 已提交
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918

	netif_start_queue(dev);
	return 0;
}

static int mpi_start_xmit(struct sk_buff *skb, struct net_device *dev) {
	int npacks, pending;
	unsigned long flags;
	struct airo_info *ai = dev->priv;

	if (!skb) {
1919
		airo_print_err(dev->name, "%s: skb == NULL!",__FUNCTION__);
L
Linus Torvalds 已提交
1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
		return 0;
	}
	npacks = skb_queue_len (&ai->txq);

	if (npacks >= MAXTXQ - 1) {
		netif_stop_queue (dev);
		if (npacks > MAXTXQ) {
			ai->stats.tx_fifo_errors++;
			return 1;
		}
		skb_queue_tail (&ai->txq, skb);
		return 0;
	}

	spin_lock_irqsave(&ai->aux_lock, flags);
	skb_queue_tail (&ai->txq, skb);
	pending = test_bit(FLAG_PENDING_XMIT, &ai->flags);
	spin_unlock_irqrestore(&ai->aux_lock,flags);
	netif_wake_queue (dev);

	if (pending == 0) {
		set_bit(FLAG_PENDING_XMIT, &ai->flags);
		mpi_send_packet (dev);
	}
	return 0;
}

/*
 * @mpi_send_packet
 *
 * Attempt to transmit a packet. Can be called from interrupt
 * or transmit . return number of packets we tried to send
 */

static int mpi_send_packet (struct net_device *dev)
{
	struct sk_buff *skb;
	unsigned char *buffer;
A
Al Viro 已提交
1958 1959
	s16 len;
	__le16 *payloadLen;
L
Linus Torvalds 已提交
1960 1961 1962 1963 1964
	struct airo_info *ai = dev->priv;
	u8 *sendbuf;

	/* get a packet to send */

A
Al Viro 已提交
1965
	if ((skb = skb_dequeue(&ai->txq)) == NULL) {
1966 1967
		airo_print_err(dev->name,
			"%s: Dequeue'd zero in send_packet()",
L
Linus Torvalds 已提交
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
			__FUNCTION__);
		return 0;
	}

	/* check min length*/
	len = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
	buffer = skb->data;

	ai->txfids[0].tx_desc.offset = 0;
	ai->txfids[0].tx_desc.valid = 1;
	ai->txfids[0].tx_desc.eoc = 1;
	ai->txfids[0].tx_desc.len =len+sizeof(WifiHdr);

/*
 * Magic, the cards firmware needs a length count (2 bytes) in the host buffer
 * right after  TXFID_HDR.The TXFID_HDR contains the status short so payloadlen
 * is immediatly after it. ------------------------------------------------
 *                         |TXFIDHDR+STATUS|PAYLOADLEN|802.3HDR|PACKETDATA|
 *                         ------------------------------------------------
 */

	memcpy((char *)ai->txfids[0].virtual_host_addr,
		(char *)&wifictlhdr8023, sizeof(wifictlhdr8023));

A
Al Viro 已提交
1992
	payloadLen = (__le16 *)(ai->txfids[0].virtual_host_addr +
L
Linus Torvalds 已提交
1993 1994 1995 1996 1997 1998 1999 2000 2001
		sizeof(wifictlhdr8023));
	sendbuf = ai->txfids[0].virtual_host_addr +
		sizeof(wifictlhdr8023) + 2 ;

	/*
	 * Firmware automaticly puts 802 header on so
	 * we don't need to account for it in the length
	 */
	if (test_bit(FLAG_MIC_CAPABLE, &ai->flags) && ai->micstats.enabled &&
A
Al Viro 已提交
2002
		(ntohs(((__be16 *)buffer)[6]) != 0x888E)) {
L
Linus Torvalds 已提交
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
		MICBuffer pMic;

		if (encapsulate(ai, (etherHead *)buffer, &pMic, len - sizeof(etherHead)) != SUCCESS)
			return ERROR;

		*payloadLen = cpu_to_le16(len-sizeof(etherHead)+sizeof(pMic));
		ai->txfids[0].tx_desc.len += sizeof(pMic);
		/* copy data into airo dma buffer */
		memcpy (sendbuf, buffer, sizeof(etherHead));
		buffer += sizeof(etherHead);
		sendbuf += sizeof(etherHead);
		memcpy (sendbuf, &pMic, sizeof(pMic));
		sendbuf += sizeof(pMic);
		memcpy (sendbuf, buffer, len - sizeof(etherHead));
2017
	} else {
L
Linus Torvalds 已提交
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
		*payloadLen = cpu_to_le16(len - sizeof(etherHead));

		dev->trans_start = jiffies;

		/* copy data into airo dma buffer */
		memcpy(sendbuf, buffer, len);
	}

	memcpy_toio(ai->txfids[0].card_ram_off,
		&ai->txfids[0].tx_desc, sizeof(TxFid));

	OUT4500(ai, EVACK, 8);

	dev_kfree_skb_any(skb);
	return 1;
}

2035
static void get_tx_error(struct airo_info *ai, s32 fid)
L
Linus Torvalds 已提交
2036
{
A
Al Viro 已提交
2037
	__le16 status;
L
Linus Torvalds 已提交
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067

	if (fid < 0)
		status = ((WifiCtlHdr *)ai->txfids[0].virtual_host_addr)->ctlhdr.status;
	else {
		if (bap_setup(ai, ai->fids[fid] & 0xffff, 4, BAP0) != SUCCESS)
			return;
		bap_read(ai, &status, 2, BAP0);
	}
	if (le16_to_cpu(status) & 2) /* Too many retries */
		ai->stats.tx_aborted_errors++;
	if (le16_to_cpu(status) & 4) /* Transmit lifetime exceeded */
		ai->stats.tx_heartbeat_errors++;
	if (le16_to_cpu(status) & 8) /* Aid fail */
		{ }
	if (le16_to_cpu(status) & 0x10) /* MAC disabled */
		ai->stats.tx_carrier_errors++;
	if (le16_to_cpu(status) & 0x20) /* Association lost */
		{ }
	/* We produce a TXDROP event only for retry or lifetime
	 * exceeded, because that's the only status that really mean
	 * that this particular node went away.
	 * Other errors means that *we* screwed up. - Jean II */
	if ((le16_to_cpu(status) & 2) ||
	     (le16_to_cpu(status) & 4)) {
		union iwreq_data	wrqu;
		char junk[0x18];

		/* Faster to skip over useless data than to do
		 * another bap_setup(). We are at offset 0x6 and
		 * need to go to 0x18 and read 6 bytes - Jean II */
2068
		bap_read(ai, (__le16 *) junk, 0x18, BAP0);
L
Linus Torvalds 已提交
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093

		/* Copy 802.11 dest address.
		 * We use the 802.11 header because the frame may
		 * not be 802.3 or may be mangled...
		 * In Ad-Hoc mode, it will be the node address.
		 * In managed mode, it will be most likely the AP addr
		 * User space will figure out how to convert it to
		 * whatever it needs (IP address or else).
		 * - Jean II */
		memcpy(wrqu.addr.sa_data, junk + 0x12, ETH_ALEN);
		wrqu.addr.sa_family = ARPHRD_ETHER;

		/* Send event to user space */
		wireless_send_event(ai->dev, IWEVTXDROP, &wrqu, NULL);
	}
}

static void airo_end_xmit(struct net_device *dev) {
	u16 status;
	int i;
	struct airo_info *priv = dev->priv;
	struct sk_buff *skb = priv->xmit.skb;
	int fid = priv->xmit.fid;
	u32 *fids = priv->fids;

2094
	clear_bit(JOB_XMIT, &priv->jobs);
L
Linus Torvalds 已提交
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
	clear_bit(FLAG_PENDING_XMIT, &priv->flags);
	status = transmit_802_3_packet (priv, fids[fid], skb->data);
	up(&priv->sem);

	i = 0;
	if ( status == SUCCESS ) {
		dev->trans_start = jiffies;
		for (; i < MAX_FIDS / 2 && (priv->fids[i] & 0xffff0000); i++);
	} else {
		priv->fids[fid] &= 0xffff;
		priv->stats.tx_window_errors++;
	}
	if (i < MAX_FIDS / 2)
		netif_wake_queue(dev);
	dev_kfree_skb(skb);
}

static int airo_start_xmit(struct sk_buff *skb, struct net_device *dev) {
	s16 len;
	int i, j;
	struct airo_info *priv = dev->priv;
	u32 *fids = priv->fids;

	if ( skb == NULL ) {
2119
		airo_print_err(dev->name, "%s: skb == NULL!", __FUNCTION__);
L
Linus Torvalds 已提交
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
		return 0;
	}

	/* Find a vacant FID */
	for( i = 0; i < MAX_FIDS / 2 && (fids[i] & 0xffff0000); i++ );
	for( j = i + 1; j < MAX_FIDS / 2 && (fids[j] & 0xffff0000); j++ );

	if ( j >= MAX_FIDS / 2 ) {
		netif_stop_queue(dev);

		if (i == MAX_FIDS / 2) {
			priv->stats.tx_fifo_errors++;
			return 1;
		}
	}
	/* check min length*/
	len = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
        /* Mark fid as used & save length for later */
	fids[i] |= (len << 16);
	priv->xmit.skb = skb;
	priv->xmit.fid = i;
	if (down_trylock(&priv->sem) != 0) {
		set_bit(FLAG_PENDING_XMIT, &priv->flags);
		netif_stop_queue(dev);
2144
		set_bit(JOB_XMIT, &priv->jobs);
L
Linus Torvalds 已提交
2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
		wake_up_interruptible(&priv->thr_wait);
	} else
		airo_end_xmit(dev);
	return 0;
}

static void airo_end_xmit11(struct net_device *dev) {
	u16 status;
	int i;
	struct airo_info *priv = dev->priv;
	struct sk_buff *skb = priv->xmit11.skb;
	int fid = priv->xmit11.fid;
	u32 *fids = priv->fids;

2159
	clear_bit(JOB_XMIT11, &priv->jobs);
L
Linus Torvalds 已提交
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
	clear_bit(FLAG_PENDING_XMIT11, &priv->flags);
	status = transmit_802_11_packet (priv, fids[fid], skb->data);
	up(&priv->sem);

	i = MAX_FIDS / 2;
	if ( status == SUCCESS ) {
		dev->trans_start = jiffies;
		for (; i < MAX_FIDS && (priv->fids[i] & 0xffff0000); i++);
	} else {
		priv->fids[fid] &= 0xffff;
		priv->stats.tx_window_errors++;
	}
	if (i < MAX_FIDS)
		netif_wake_queue(dev);
	dev_kfree_skb(skb);
}

static int airo_start_xmit11(struct sk_buff *skb, struct net_device *dev) {
	s16 len;
	int i, j;
	struct airo_info *priv = dev->priv;
	u32 *fids = priv->fids;

	if (test_bit(FLAG_MPI, &priv->flags)) {
		/* Not implemented yet for MPI350 */
		netif_stop_queue(dev);
		return -ENETDOWN;
	}

	if ( skb == NULL ) {
2190
		airo_print_err(dev->name, "%s: skb == NULL!", __FUNCTION__);
L
Linus Torvalds 已提交
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
		return 0;
	}

	/* Find a vacant FID */
	for( i = MAX_FIDS / 2; i < MAX_FIDS && (fids[i] & 0xffff0000); i++ );
	for( j = i + 1; j < MAX_FIDS && (fids[j] & 0xffff0000); j++ );

	if ( j >= MAX_FIDS ) {
		netif_stop_queue(dev);

		if (i == MAX_FIDS) {
			priv->stats.tx_fifo_errors++;
			return 1;
		}
	}
	/* check min length*/
	len = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
        /* Mark fid as used & save length for later */
	fids[i] |= (len << 16);
	priv->xmit11.skb = skb;
	priv->xmit11.fid = i;
	if (down_trylock(&priv->sem) != 0) {
		set_bit(FLAG_PENDING_XMIT11, &priv->flags);
		netif_stop_queue(dev);
2215
		set_bit(JOB_XMIT11, &priv->jobs);
L
Linus Torvalds 已提交
2216 2217 2218 2219 2220 2221
		wake_up_interruptible(&priv->thr_wait);
	} else
		airo_end_xmit11(dev);
	return 0;
}

A
Al Viro 已提交
2222 2223
static void airo_read_stats(struct airo_info *ai)
{
L
Linus Torvalds 已提交
2224
	StatsRid stats_rid;
A
Al Viro 已提交
2225
	__le32 *vals = stats_rid.vals;
L
Linus Torvalds 已提交
2226

2227
	clear_bit(JOB_STATS, &ai->jobs);
2228
	if (ai->power.event) {
L
Linus Torvalds 已提交
2229 2230 2231 2232 2233 2234
		up(&ai->sem);
		return;
	}
	readStatsRid(ai, &stats_rid, RID_STATS, 0);
	up(&ai->sem);

A
Al Viro 已提交
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245
	ai->stats.rx_packets = le32_to_cpu(vals[43]) + le32_to_cpu(vals[44]) +
			       le32_to_cpu(vals[45]);
	ai->stats.tx_packets = le32_to_cpu(vals[39]) + le32_to_cpu(vals[40]) +
			       le32_to_cpu(vals[41]);
	ai->stats.rx_bytes = le32_to_cpu(vals[92]);
	ai->stats.tx_bytes = le32_to_cpu(vals[91]);
	ai->stats.rx_errors = le32_to_cpu(vals[0]) + le32_to_cpu(vals[2]) +
			      le32_to_cpu(vals[3]) + le32_to_cpu(vals[4]);
	ai->stats.tx_errors = le32_to_cpu(vals[42]) + ai->stats.tx_fifo_errors;
	ai->stats.multicast = le32_to_cpu(vals[43]);
	ai->stats.collisions = le32_to_cpu(vals[89]);
L
Linus Torvalds 已提交
2246 2247

	/* detailed rx_errors: */
A
Al Viro 已提交
2248 2249 2250 2251
	ai->stats.rx_length_errors = le32_to_cpu(vals[3]);
	ai->stats.rx_crc_errors = le32_to_cpu(vals[4]);
	ai->stats.rx_frame_errors = le32_to_cpu(vals[2]);
	ai->stats.rx_fifo_errors = le32_to_cpu(vals[0]);
L
Linus Torvalds 已提交
2252 2253
}

J
Jouni Malinen 已提交
2254
static struct net_device_stats *airo_get_stats(struct net_device *dev)
L
Linus Torvalds 已提交
2255 2256 2257
{
	struct airo_info *local =  dev->priv;

2258
	if (!test_bit(JOB_STATS, &local->jobs)) {
L
Linus Torvalds 已提交
2259 2260
		/* Get stats out of the card if available */
		if (down_trylock(&local->sem) != 0) {
2261
			set_bit(JOB_STATS, &local->jobs);
L
Linus Torvalds 已提交
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
			wake_up_interruptible(&local->thr_wait);
		} else
			airo_read_stats(local);
	}

	return &local->stats;
}

static void airo_set_promisc(struct airo_info *ai) {
	Cmd cmd;
	Resp rsp;

	memset(&cmd, 0, sizeof(cmd));
	cmd.cmd=CMD_SETMODE;
2276
	clear_bit(JOB_PROMISC, &ai->jobs);
L
Linus Torvalds 已提交
2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
	cmd.parm0=(ai->flags&IFF_PROMISC) ? PROMISC : NOPROMISC;
	issuecommand(ai, &cmd, &rsp);
	up(&ai->sem);
}

static void airo_set_multicast_list(struct net_device *dev) {
	struct airo_info *ai = dev->priv;

	if ((dev->flags ^ ai->flags) & IFF_PROMISC) {
		change_bit(FLAG_PROMISC, &ai->flags);
		if (down_trylock(&ai->sem) != 0) {
2288
			set_bit(JOB_PROMISC, &ai->jobs);
L
Linus Torvalds 已提交
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
			wake_up_interruptible(&ai->thr_wait);
		} else
			airo_set_promisc(ai);
	}

	if ((dev->flags&IFF_ALLMULTI)||dev->mc_count>0) {
		/* Turn on multicast.  (Should be already setup...) */
	}
}

static int airo_set_mac_address(struct net_device *dev, void *p)
{
	struct airo_info *ai = dev->priv;
	struct sockaddr *addr = p;

	readConfigRid(ai, 1);
	memcpy (ai->config.macAddr, addr->sa_data, dev->addr_len);
	set_bit (FLAG_COMMIT, &ai->flags);
	disable_MAC(ai, 1);
	writeConfigRid (ai, 1);
2309
	enable_MAC(ai, 1);
L
Linus Torvalds 已提交
2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323
	memcpy (ai->dev->dev_addr, addr->sa_data, dev->addr_len);
	if (ai->wifidev)
		memcpy (ai->wifidev->dev_addr, addr->sa_data, dev->addr_len);
	return 0;
}

static int airo_change_mtu(struct net_device *dev, int new_mtu)
{
	if ((new_mtu < 68) || (new_mtu > 2400))
		return -EINVAL;
	dev->mtu = new_mtu;
	return 0;
}

2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
static LIST_HEAD(airo_devices);

static void add_airo_dev(struct airo_info *ai)
{
	/* Upper layers already keep track of PCI devices,
	 * so we only need to remember our non-PCI cards. */
	if (!ai->pci)
		list_add_tail(&ai->dev_list, &airo_devices);
}

static void del_airo_dev(struct airo_info *ai)
{
	if (!ai->pci)
		list_del(&ai->dev_list);
}
L
Linus Torvalds 已提交
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355

static int airo_close(struct net_device *dev) {
	struct airo_info *ai = dev->priv;

	netif_stop_queue(dev);

	if (ai->wifidev != dev) {
#ifdef POWER_ON_DOWN
		/* Shut power to the card. The idea is that the user can save
		 * power when he doesn't need the card with "ifconfig down".
		 * That's the method that is most friendly towards the network
		 * stack (i.e. the network stack won't try to broadcast
		 * anything on the interface and routes are gone. Jean II */
		set_bit(FLAG_RADIO_DOWN, &ai->flags);
		disable_MAC(ai, 1);
#endif
		disable_interrupts( ai );
2356 2357 2358 2359 2360

		free_irq(dev->irq, dev);

		set_bit(JOB_DIE, &ai->jobs);
		kthread_stop(ai->airo_thread_task);
L
Linus Torvalds 已提交
2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
	}
	return 0;
}

void stop_airo_card( struct net_device *dev, int freeres )
{
	struct airo_info *ai = dev->priv;

	set_bit(FLAG_RADIO_DOWN, &ai->flags);
	disable_MAC(ai, 1);
	disable_interrupts(ai);
	takedown_proc_entry( dev, ai );
	if (test_bit(FLAG_REGISTERED, &ai->flags)) {
		unregister_netdev( dev );
		if (ai->wifidev) {
			unregister_netdev(ai->wifidev);
			free_netdev(ai->wifidev);
			ai->wifidev = NULL;
		}
		clear_bit(FLAG_REGISTERED, &ai->flags);
	}
	/*
	 * Clean out tx queue
	 */
2385
	if (test_bit(FLAG_MPI, &ai->flags) && !skb_queue_empty(&ai->txq)) {
L
Linus Torvalds 已提交
2386 2387 2388 2389 2390
		struct sk_buff *skb = NULL;
		for (;(skb = skb_dequeue(&ai->txq));)
			dev_kfree_skb(skb);
	}

2391 2392
	airo_networks_free (ai);

2393 2394 2395 2396
	kfree(ai->flash);
	kfree(ai->rssi);
	kfree(ai->APList);
	kfree(ai->SSID);
L
Linus Torvalds 已提交
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410
	if (freeres) {
		/* PCMCIA frees this stuff, so only for PCI and ISA */
	        release_region( dev->base_addr, 64 );
		if (test_bit(FLAG_MPI, &ai->flags)) {
			if (ai->pci)
				mpi_unmap_card(ai->pci);
			if (ai->pcimem)
				iounmap(ai->pcimem);
			if (ai->pciaux)
				iounmap(ai->pciaux);
			pci_free_consistent(ai->pci, PCI_SHARED_LEN,
				ai->shared, ai->shared_dma);
		}
        }
2411
	crypto_free_cipher(ai->tfm);
2412
	del_airo_dev(ai);
L
Linus Torvalds 已提交
2413 2414 2415 2416 2417
	free_netdev( dev );
}

EXPORT_SYMBOL(stop_airo_card);

S
Stephen Hemminger 已提交
2418
static int wll_header_parse(const struct sk_buff *skb, unsigned char *haddr)
L
Linus Torvalds 已提交
2419
{
2420
	memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN);
L
Linus Torvalds 已提交
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460
	return ETH_ALEN;
}

static void mpi_unmap_card(struct pci_dev *pci)
{
	unsigned long mem_start = pci_resource_start(pci, 1);
	unsigned long mem_len = pci_resource_len(pci, 1);
	unsigned long aux_start = pci_resource_start(pci, 2);
	unsigned long aux_len = AUXMEMSIZE;

	release_mem_region(aux_start, aux_len);
	release_mem_region(mem_start, mem_len);
}

/*************************************************************
 *  This routine assumes that descriptors have been setup .
 *  Run at insmod time or after reset  when the decriptors
 *  have been initialized . Returns 0 if all is well nz
 *  otherwise . Does not allocate memory but sets up card
 *  using previously allocated descriptors.
 */
static int mpi_init_descriptors (struct airo_info *ai)
{
	Cmd cmd;
	Resp rsp;
	int i;
	int rc = SUCCESS;

	/* Alloc  card RX descriptors */
	netif_stop_queue(ai->dev);

	memset(&rsp,0,sizeof(rsp));
	memset(&cmd,0,sizeof(cmd));

	cmd.cmd = CMD_ALLOCATEAUX;
	cmd.parm0 = FID_RX;
	cmd.parm1 = (ai->rxfids[0].card_ram_off - ai->pciaux);
	cmd.parm2 = MPI_MAX_FIDS;
	rc=issuecommand(ai, &cmd, &rsp);
	if (rc != SUCCESS) {
2461
		airo_print_err(ai->dev->name, "Couldn't allocate RX FID");
L
Linus Torvalds 已提交
2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
		return rc;
	}

	for (i=0; i<MPI_MAX_FIDS; i++) {
		memcpy_toio(ai->rxfids[i].card_ram_off,
			&ai->rxfids[i].rx_desc, sizeof(RxFid));
	}

	/* Alloc card TX descriptors */

	memset(&rsp,0,sizeof(rsp));
	memset(&cmd,0,sizeof(cmd));

	cmd.cmd = CMD_ALLOCATEAUX;
	cmd.parm0 = FID_TX;
	cmd.parm1 = (ai->txfids[0].card_ram_off - ai->pciaux);
	cmd.parm2 = MPI_MAX_FIDS;

	for (i=0; i<MPI_MAX_FIDS; i++) {
		ai->txfids[i].tx_desc.valid = 1;
		memcpy_toio(ai->txfids[i].card_ram_off,
			&ai->txfids[i].tx_desc, sizeof(TxFid));
	}
	ai->txfids[i-1].tx_desc.eoc = 1; /* Last descriptor has EOC set */

	rc=issuecommand(ai, &cmd, &rsp);
	if (rc != SUCCESS) {
2489
		airo_print_err(ai->dev->name, "Couldn't allocate TX FID");
L
Linus Torvalds 已提交
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502
		return rc;
	}

	/* Alloc card Rid descriptor */
	memset(&rsp,0,sizeof(rsp));
	memset(&cmd,0,sizeof(cmd));

	cmd.cmd = CMD_ALLOCATEAUX;
	cmd.parm0 = RID_RW;
	cmd.parm1 = (ai->config_desc.card_ram_off - ai->pciaux);
	cmd.parm2 = 1; /* Magic number... */
	rc=issuecommand(ai, &cmd, &rsp);
	if (rc != SUCCESS) {
2503
		airo_print_err(ai->dev->name, "Couldn't allocate RID");
L
Linus Torvalds 已提交
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518
		return rc;
	}

	memcpy_toio(ai->config_desc.card_ram_off,
		&ai->config_desc.rid_desc, sizeof(Rid));

	return rc;
}

/*
 * We are setting up three things here:
 * 1) Map AUX memory for descriptors: Rid, TxFid, or RxFid.
 * 2) Map PCI memory for issueing commands.
 * 3) Allocate memory (shared) to send and receive ethernet frames.
 */
2519
static int mpi_map_card(struct airo_info *ai, struct pci_dev *pci)
L
Linus Torvalds 已提交
2520 2521 2522 2523
{
	unsigned long mem_start, mem_len, aux_start, aux_len;
	int rc = -1;
	int i;
2524 2525
	dma_addr_t busaddroff;
	unsigned char *vpackoff;
L
Linus Torvalds 已提交
2526 2527 2528 2529 2530 2531 2532
	unsigned char __iomem *pciaddroff;

	mem_start = pci_resource_start(pci, 1);
	mem_len = pci_resource_len(pci, 1);
	aux_start = pci_resource_start(pci, 2);
	aux_len = AUXMEMSIZE;

2533 2534 2535
	if (!request_mem_region(mem_start, mem_len, DRV_NAME)) {
		airo_print_err("", "Couldn't get region %x[%x]",
			(int)mem_start, (int)mem_len);
L
Linus Torvalds 已提交
2536 2537
		goto out;
	}
2538 2539 2540
	if (!request_mem_region(aux_start, aux_len, DRV_NAME)) {
		airo_print_err("", "Couldn't get region %x[%x]",
			(int)aux_start, (int)aux_len);
L
Linus Torvalds 已提交
2541 2542 2543 2544 2545
		goto free_region1;
	}

	ai->pcimem = ioremap(mem_start, mem_len);
	if (!ai->pcimem) {
2546 2547
		airo_print_err("", "Couldn't map region %x[%x]",
			(int)mem_start, (int)mem_len);
L
Linus Torvalds 已提交
2548 2549 2550 2551
		goto free_region2;
	}
	ai->pciaux = ioremap(aux_start, aux_len);
	if (!ai->pciaux) {
2552 2553
		airo_print_err("", "Couldn't map region %x[%x]",
			(int)aux_start, (int)aux_len);
L
Linus Torvalds 已提交
2554 2555 2556 2557 2558 2559
		goto free_memmap;
	}

	/* Reserve PKTSIZE for each fid and 2K for the Rids */
	ai->shared = pci_alloc_consistent(pci, PCI_SHARED_LEN, &ai->shared_dma);
	if (!ai->shared) {
2560 2561
		airo_print_err("", "Couldn't alloc_consistent %d",
			PCI_SHARED_LEN);
L
Linus Torvalds 已提交
2562 2563 2564 2565 2566 2567
		goto free_auxmap;
	}

	/*
	 * Setup descriptor RX, TX, CONFIG
	 */
2568
	busaddroff = ai->shared_dma;
L
Linus Torvalds 已提交
2569 2570 2571 2572 2573 2574 2575 2576
	pciaddroff = ai->pciaux + AUX_OFFSET;
	vpackoff   = ai->shared;

	/* RX descriptor setup */
	for(i = 0; i < MPI_MAX_FIDS; i++) {
		ai->rxfids[i].pending = 0;
		ai->rxfids[i].card_ram_off = pciaddroff;
		ai->rxfids[i].virtual_host_addr = vpackoff;
2577
		ai->rxfids[i].rx_desc.host_addr = busaddroff;
L
Linus Torvalds 已提交
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591
		ai->rxfids[i].rx_desc.valid = 1;
		ai->rxfids[i].rx_desc.len = PKTSIZE;
		ai->rxfids[i].rx_desc.rdy = 0;

		pciaddroff += sizeof(RxFid);
		busaddroff += PKTSIZE;
		vpackoff   += PKTSIZE;
	}

	/* TX descriptor setup */
	for(i = 0; i < MPI_MAX_FIDS; i++) {
		ai->txfids[i].card_ram_off = pciaddroff;
		ai->txfids[i].virtual_host_addr = vpackoff;
		ai->txfids[i].tx_desc.valid = 1;
2592
		ai->txfids[i].tx_desc.host_addr = busaddroff;
L
Linus Torvalds 已提交
2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
		memcpy(ai->txfids[i].virtual_host_addr,
			&wifictlhdr8023, sizeof(wifictlhdr8023));

		pciaddroff += sizeof(TxFid);
		busaddroff += PKTSIZE;
		vpackoff   += PKTSIZE;
	}
	ai->txfids[i-1].tx_desc.eoc = 1; /* Last descriptor has EOC set */

	/* Rid descriptor setup */
	ai->config_desc.card_ram_off = pciaddroff;
	ai->config_desc.virtual_host_addr = vpackoff;
2605 2606
	ai->config_desc.rid_desc.host_addr = busaddroff;
	ai->ridbus = busaddroff;
L
Linus Torvalds 已提交
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632
	ai->config_desc.rid_desc.rid = 0;
	ai->config_desc.rid_desc.len = RIDSIZE;
	ai->config_desc.rid_desc.valid = 1;
	pciaddroff += sizeof(Rid);
	busaddroff += RIDSIZE;
	vpackoff   += RIDSIZE;

	/* Tell card about descriptors */
	if (mpi_init_descriptors (ai) != SUCCESS)
		goto free_shared;

	return 0;
 free_shared:
	pci_free_consistent(pci, PCI_SHARED_LEN, ai->shared, ai->shared_dma);
 free_auxmap:
	iounmap(ai->pciaux);
 free_memmap:
	iounmap(ai->pcimem);
 free_region2:
	release_mem_region(aux_start, aux_len);
 free_region1:
	release_mem_region(mem_start, mem_len);
 out:
	return rc;
}

2633 2634 2635 2636
static const struct header_ops airo_header_ops = {
	.parse = wll_header_parse,
};

L
Linus Torvalds 已提交
2637 2638
static void wifi_setup(struct net_device *dev)
{
2639
	dev->header_ops = &airo_header_ops;
L
Linus Torvalds 已提交
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650
	dev->hard_start_xmit = &airo_start_xmit11;
	dev->get_stats = &airo_get_stats;
	dev->set_mac_address = &airo_set_mac_address;
	dev->do_ioctl = &airo_ioctl;
	dev->wireless_handlers = &airo_handler_def;
	dev->change_mtu = &airo_change_mtu;
	dev->open = &airo_open;
	dev->stop = &airo_close;

	dev->type               = ARPHRD_IEEE80211;
	dev->hard_header_len    = ETH_HLEN;
2651
	dev->mtu                = AIRO_DEF_MTU;
L
Linus Torvalds 已提交
2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679
	dev->addr_len           = ETH_ALEN;
	dev->tx_queue_len       = 100; 

	memset(dev->broadcast,0xFF, ETH_ALEN);

	dev->flags              = IFF_BROADCAST|IFF_MULTICAST;
}

static struct net_device *init_wifidev(struct airo_info *ai,
					struct net_device *ethdev)
{
	int err;
	struct net_device *dev = alloc_netdev(0, "wifi%d", wifi_setup);
	if (!dev)
		return NULL;
	dev->priv = ethdev->priv;
	dev->irq = ethdev->irq;
	dev->base_addr = ethdev->base_addr;
	dev->wireless_data = ethdev->wireless_data;
	memcpy(dev->dev_addr, ethdev->dev_addr, dev->addr_len);
	err = register_netdev(dev);
	if (err<0) {
		free_netdev(dev);
		return NULL;
	}
	return dev;
}

J
Jouni Malinen 已提交
2680
static int reset_card( struct net_device *dev , int lock) {
L
Linus Torvalds 已提交
2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694
	struct airo_info *ai = dev->priv;

	if (lock && down_interruptible(&ai->sem))
		return -1;
	waitbusy (ai);
	OUT4500(ai,COMMAND,CMD_SOFTRESET);
	msleep(200);
	waitbusy (ai);
	msleep(200);
	if (lock)
		up(&ai->sem);
	return 0;
}

2695
#define AIRO_MAX_NETWORK_COUNT	64
2696 2697 2698 2699 2700 2701
static int airo_networks_allocate(struct airo_info *ai)
{
	if (ai->networks)
		return 0;

	ai->networks =
2702
	    kzalloc(AIRO_MAX_NETWORK_COUNT * sizeof(BSSListElement),
2703 2704
		    GFP_KERNEL);
	if (!ai->networks) {
2705
		airo_print_warn("", "Out of memory allocating beacons");
2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723
		return -ENOMEM;
	}

	return 0;
}

static void airo_networks_free(struct airo_info *ai)
{
	kfree(ai->networks);
	ai->networks = NULL;
}

static void airo_networks_initialize(struct airo_info *ai)
{
	int i;

	INIT_LIST_HEAD(&ai->network_free_list);
	INIT_LIST_HEAD(&ai->network_list);
2724
	for (i = 0; i < AIRO_MAX_NETWORK_COUNT; i++)
2725 2726 2727 2728
		list_add_tail(&ai->networks[i].list,
			      &ai->network_free_list);
}

2729 2730 2731 2732 2733 2734 2735 2736 2737
static int airo_test_wpa_capable(struct airo_info *ai)
{
	int status;
	CapabilityRid cap_rid;

	status = readCapabilityRid(ai, &cap_rid, 1);
	if (status != SUCCESS) return 0;

	/* Only firmware versions 5.30.17 or better can do WPA */
2738 2739 2740
	if (le16_to_cpu(cap_rid.softVer) > 0x530
	  || (le16_to_cpu(cap_rid.softVer) == 0x530
	      && le16_to_cpu(cap_rid.softSubVer) >= 17)) {
2741
		airo_print_info("", "WPA is supported.");
2742 2743 2744 2745
		return 1;
	}

	/* No WPA support */
2746
	airo_print_info("", "WPA unsupported (only firmware versions 5.30.17"
2747 2748 2749 2750
		" and greater support WPA.  Detected %s)", cap_rid.prodVer);
	return 0;
}

J
Jouni Malinen 已提交
2751 2752 2753
static struct net_device *_init_airo_card( unsigned short irq, int port,
					   int is_pcmcia, struct pci_dev *pci,
					   struct device *dmdev )
L
Linus Torvalds 已提交
2754 2755 2756 2757
{
	struct net_device *dev;
	struct airo_info *ai;
	int i, rc;
2758
	DECLARE_MAC_BUF(mac);
L
Linus Torvalds 已提交
2759 2760

	/* Create the network device object. */
2761 2762
	dev = alloc_netdev(sizeof(*ai), "", ether_setup);
	if (!dev) {
2763
		airo_print_err("", "Couldn't alloc_etherdev");
L
Linus Torvalds 已提交
2764 2765 2766 2767 2768
		return NULL;
	}

	ai = dev->priv;
	ai->wifidev = NULL;
2769
	ai->flags = 1 << FLAG_RADIO_DOWN;
2770
	ai->jobs = 0;
2771
	ai->dev = dev;
L
Linus Torvalds 已提交
2772
	if (pci && (pci->device == 0x5000 || pci->device == 0xa504)) {
2773
		airo_print_dbg("", "Found an MPI350 card");
L
Linus Torvalds 已提交
2774 2775 2776 2777 2778 2779 2780 2781
		set_bit(FLAG_MPI, &ai->flags);
	}
	spin_lock_init(&ai->aux_lock);
	sema_init(&ai->sem, 1);
	ai->config.len = 0;
	ai->pci = pci;
	init_waitqueue_head (&ai->thr_wait);
	ai->tfm = NULL;
2782
	add_airo_dev(ai);
L
Linus Torvalds 已提交
2783

2784
	if (airo_networks_allocate (ai))
2785
		goto err_out_free;
2786 2787
	airo_networks_initialize (ai);

L
Linus Torvalds 已提交
2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808
	/* The Airo-specific entries in the device structure. */
	if (test_bit(FLAG_MPI,&ai->flags)) {
		skb_queue_head_init (&ai->txq);
		dev->hard_start_xmit = &mpi_start_xmit;
	} else
		dev->hard_start_xmit = &airo_start_xmit;
	dev->get_stats = &airo_get_stats;
	dev->set_multicast_list = &airo_set_multicast_list;
	dev->set_mac_address = &airo_set_mac_address;
	dev->do_ioctl = &airo_ioctl;
	dev->wireless_handlers = &airo_handler_def;
	ai->wireless_data.spy_data = &ai->spy_data;
	dev->wireless_data = &ai->wireless_data;
	dev->change_mtu = &airo_change_mtu;
	dev->open = &airo_open;
	dev->stop = &airo_close;
	dev->irq = irq;
	dev->base_addr = port;

	SET_NETDEV_DEV(dev, dmdev);

2809 2810
	reset_card (dev, 1);
	msleep(400);
L
Linus Torvalds 已提交
2811 2812

	if (!is_pcmcia) {
2813
		if (!request_region(dev->base_addr, 64, DRV_NAME)) {
L
Linus Torvalds 已提交
2814
			rc = -EBUSY;
2815
			airo_print_err(dev->name, "Couldn't request region");
2816
			goto err_out_nets;
L
Linus Torvalds 已提交
2817 2818 2819 2820
		}
	}

	if (test_bit(FLAG_MPI,&ai->flags)) {
2821 2822
		if (mpi_map_card(ai, pci)) {
			airo_print_err("", "Could not map memory");
L
Linus Torvalds 已提交
2823 2824 2825 2826 2827 2828
			goto err_out_res;
		}
	}

	if (probe) {
		if ( setup_card( ai, dev->dev_addr, 1 ) != SUCCESS ) {
2829
			airo_print_err(dev->name, "MAC could not be enabled" );
L
Linus Torvalds 已提交
2830 2831 2832 2833 2834 2835 2836 2837
			rc = -EIO;
			goto err_out_map;
		}
	} else if (!test_bit(FLAG_MPI,&ai->flags)) {
		ai->bap_read = fast_bap_read;
		set_bit(FLAG_FLASHING, &ai->flags);
	}

2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849
	/* Test for WPA support */
	if (airo_test_wpa_capable(ai)) {
		set_bit(FLAG_WPA_CAPABLE, &ai->flags);
		ai->bssListFirst = RID_WPA_BSSLISTFIRST;
		ai->bssListNext = RID_WPA_BSSLISTNEXT;
		ai->bssListRidLen = sizeof(BSSListRid);
	} else {
		ai->bssListFirst = RID_BSSLISTFIRST;
		ai->bssListNext = RID_BSSLISTNEXT;
		ai->bssListRidLen = sizeof(BSSListRid) - sizeof(BSSListRidExtra);
	}

2850
	strcpy(dev->name, "eth%d");
L
Linus Torvalds 已提交
2851 2852
	rc = register_netdev(dev);
	if (rc) {
2853
		airo_print_err(dev->name, "Couldn't register_netdev");
L
Linus Torvalds 已提交
2854 2855 2856
		goto err_out_map;
	}
	ai->wifidev = init_wifidev(ai, dev);
2857 2858
	if (!ai->wifidev)
		goto err_out_reg;
L
Linus Torvalds 已提交
2859 2860

	set_bit(FLAG_REGISTERED,&ai->flags);
2861 2862
	airo_print_info(dev->name, "MAC enabled %s",
			print_mac(mac, dev->dev_addr));
L
Linus Torvalds 已提交
2863 2864 2865 2866

	/* Allocate the transmit buffers */
	if (probe && !test_bit(FLAG_MPI,&ai->flags))
		for( i = 0; i < MAX_FIDS; i++ )
2867
			ai->fids[i] = transmit_allocate(ai,AIRO_DEF_MTU,i>=MAX_FIDS/2);
L
Linus Torvalds 已提交
2868

2869 2870 2871
	if (setup_proc_entry(dev, dev->priv) < 0)
		goto err_out_wifi;

L
Linus Torvalds 已提交
2872 2873
	return dev;

2874 2875 2876 2877 2878
err_out_wifi:
	unregister_netdev(ai->wifidev);
	free_netdev(ai->wifidev);
err_out_reg:
	unregister_netdev(dev);
L
Linus Torvalds 已提交
2879 2880 2881 2882 2883 2884 2885 2886 2887 2888
err_out_map:
	if (test_bit(FLAG_MPI,&ai->flags) && pci) {
		pci_free_consistent(pci, PCI_SHARED_LEN, ai->shared, ai->shared_dma);
		iounmap(ai->pciaux);
		iounmap(ai->pcimem);
		mpi_unmap_card(ai->pci);
	}
err_out_res:
	if (!is_pcmcia)
	        release_region( dev->base_addr, 64 );
2889 2890
err_out_nets:
	airo_networks_free(ai);
2891
	del_airo_dev(ai);
L
Linus Torvalds 已提交
2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906
err_out_free:
	free_netdev(dev);
	return NULL;
}

struct net_device *init_airo_card( unsigned short irq, int port, int is_pcmcia,
				  struct device *dmdev)
{
	return _init_airo_card ( irq, port, is_pcmcia, NULL, dmdev);
}

EXPORT_SYMBOL(init_airo_card);

static int waitbusy (struct airo_info *ai) {
	int delay = 0;
2907
	while ((IN4500 (ai, COMMAND) & COMMAND_BUSY) && (delay < 10000)) {
L
Linus Torvalds 已提交
2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918
		udelay (10);
		if ((++delay % 20) == 0)
			OUT4500(ai, EVACK, EV_CLEARCOMMANDBUSY);
	}
	return delay < 10000;
}

int reset_airo_card( struct net_device *dev )
{
	int i;
	struct airo_info *ai = dev->priv;
2919
	DECLARE_MAC_BUF(mac);
L
Linus Torvalds 已提交
2920 2921 2922 2923 2924

	if (reset_card (dev, 1))
		return -1;

	if ( setup_card(ai, dev->dev_addr, 1 ) != SUCCESS ) {
2925
		airo_print_err(dev->name, "MAC could not be enabled");
L
Linus Torvalds 已提交
2926 2927
		return -1;
	}
2928 2929
	airo_print_info(dev->name, "MAC enabled %s",
			print_mac(mac, dev->dev_addr));
L
Linus Torvalds 已提交
2930 2931 2932
	/* Allocate the transmit buffers if needed */
	if (!test_bit(FLAG_MPI,&ai->flags))
		for( i = 0; i < MAX_FIDS; i++ )
2933
			ai->fids[i] = transmit_allocate (ai,AIRO_DEF_MTU,i>=MAX_FIDS/2);
L
Linus Torvalds 已提交
2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946

	enable_interrupts( ai );
	netif_wake_queue(dev);
	return 0;
}

EXPORT_SYMBOL(reset_airo_card);

static void airo_send_event(struct net_device *dev) {
	struct airo_info *ai = dev->priv;
	union iwreq_data wrqu;
	StatusRid status_rid;

2947
	clear_bit(JOB_EVENT, &ai->jobs);
L
Linus Torvalds 已提交
2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
	PC4500_readrid(ai, RID_STATUS, &status_rid, sizeof(status_rid), 0);
	up(&ai->sem);
	wrqu.data.length = 0;
	wrqu.data.flags = 0;
	memcpy(wrqu.ap_addr.sa_data, status_rid.bssid[0], ETH_ALEN);
	wrqu.ap_addr.sa_family = ARPHRD_ETHER;

	/* Send event to user space */
	wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
}

2959 2960
static void airo_process_scan_results (struct airo_info *ai) {
	union iwreq_data	wrqu;
2961
	BSSListRid bss;
2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973
	int rc;
	BSSListElement * loop_net;
	BSSListElement * tmp_net;

	/* Blow away current list of scan results */
	list_for_each_entry_safe (loop_net, tmp_net, &ai->network_list, list) {
		list_move_tail (&loop_net->list, &ai->network_free_list);
		/* Don't blow away ->list, just BSS data */
		memset (loop_net, 0, sizeof (loop_net->bss));
	}

	/* Try to read the first entry of the scan result */
2974
	rc = PC4500_readrid(ai, ai->bssListFirst, &bss, ai->bssListRidLen, 0);
A
Al Viro 已提交
2975
	if((rc) || (bss.index == cpu_to_le16(0xffff))) {
2976 2977 2978 2979 2980 2981
		/* No scan results */
		goto out;
	}

	/* Read and parse all entries */
	tmp_net = NULL;
A
Al Viro 已提交
2982
	while((!rc) && (bss.index != cpu_to_le16(0xffff))) {
2983 2984 2985 2986 2987 2988 2989 2990
		/* Grab a network off the free list */
		if (!list_empty(&ai->network_free_list)) {
			tmp_net = list_entry(ai->network_free_list.next,
					    BSSListElement, list);
			list_del(ai->network_free_list.next);
		}

		if (tmp_net != NULL) {
2991
			memcpy(tmp_net, &bss, sizeof(tmp_net->bss));
2992 2993 2994 2995 2996
			list_add_tail(&tmp_net->list, &ai->network_list);
			tmp_net = NULL;
		}

		/* Read next entry */
2997 2998
		rc = PC4500_readrid(ai, ai->bssListNext,
				    &bss, ai->bssListRidLen, 0);
2999 3000 3001 3002
	}

out:
	ai->scan_timeout = 0;
3003
	clear_bit(JOB_SCAN_RESULTS, &ai->jobs);
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017
	up(&ai->sem);

	/* Send an empty event to user space.
	 * We don't send the received data on
	 * the event because it would require
	 * us to do complex transcoding, and
	 * we want to minimise the work done in
	 * the irq handler. Use a request to
	 * extract the data - Jean II */
	wrqu.data.length = 0;
	wrqu.data.flags = 0;
	wireless_send_event(ai->dev, SIOCGIWSCAN, &wrqu, NULL);
}

L
Linus Torvalds 已提交
3018 3019 3020 3021
static int airo_thread(void *data) {
	struct net_device *dev = data;
	struct airo_info *ai = dev->priv;
	int locked;
3022 3023

	set_freezable();
L
Linus Torvalds 已提交
3024 3025
	while(1) {
		/* make swsusp happy with our thread */
3026
		try_to_freeze();
L
Linus Torvalds 已提交
3027

3028
		if (test_bit(JOB_DIE, &ai->jobs))
L
Linus Torvalds 已提交
3029 3030
			break;

3031
		if (ai->jobs) {
L
Linus Torvalds 已提交
3032 3033 3034 3035 3036 3037 3038 3039
			locked = down_interruptible(&ai->sem);
		} else {
			wait_queue_t wait;

			init_waitqueue_entry(&wait, current);
			add_wait_queue(&ai->thr_wait, &wait);
			for (;;) {
				set_current_state(TASK_INTERRUPTIBLE);
3040
				if (ai->jobs)
L
Linus Torvalds 已提交
3041
					break;
3042 3043 3044
				if (ai->expires || ai->scan_timeout) {
					if (ai->scan_timeout &&
							time_after_eq(jiffies,ai->scan_timeout)){
3045
						set_bit(JOB_SCAN_RESULTS, &ai->jobs);
3046 3047 3048
						break;
					} else if (ai->expires &&
							time_after_eq(jiffies,ai->expires)){
3049
						set_bit(JOB_AUTOWEP, &ai->jobs);
L
Linus Torvalds 已提交
3050 3051
						break;
					}
3052 3053
					if (!kthread_should_stop() &&
					    !freezing(current)) {
3054 3055 3056 3057 3058 3059 3060 3061 3062
						unsigned long wake_at;
						if (!ai->expires || !ai->scan_timeout) {
							wake_at = max(ai->expires,
								ai->scan_timeout);
						} else {
							wake_at = min(ai->expires,
								ai->scan_timeout);
						}
						schedule_timeout(wake_at - jiffies);
L
Linus Torvalds 已提交
3063 3064
						continue;
					}
3065 3066
				} else if (!kthread_should_stop() &&
					   !freezing(current)) {
L
Linus Torvalds 已提交
3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079
					schedule();
					continue;
				}
				break;
			}
			current->state = TASK_RUNNING;
			remove_wait_queue(&ai->thr_wait, &wait);
			locked = 1;
		}

		if (locked)
			continue;

3080
		if (test_bit(JOB_DIE, &ai->jobs)) {
L
Linus Torvalds 已提交
3081 3082 3083 3084
			up(&ai->sem);
			break;
		}

3085
		if (ai->power.event || test_bit(FLAG_FLASHING, &ai->flags)) {
L
Linus Torvalds 已提交
3086 3087 3088 3089
			up(&ai->sem);
			continue;
		}

3090
		if (test_bit(JOB_XMIT, &ai->jobs))
L
Linus Torvalds 已提交
3091
			airo_end_xmit(dev);
3092
		else if (test_bit(JOB_XMIT11, &ai->jobs))
L
Linus Torvalds 已提交
3093
			airo_end_xmit11(dev);
3094
		else if (test_bit(JOB_STATS, &ai->jobs))
L
Linus Torvalds 已提交
3095
			airo_read_stats(ai);
3096
		else if (test_bit(JOB_WSTATS, &ai->jobs))
L
Linus Torvalds 已提交
3097
			airo_read_wireless_stats(ai);
3098
		else if (test_bit(JOB_PROMISC, &ai->jobs))
L
Linus Torvalds 已提交
3099
			airo_set_promisc(ai);
3100
		else if (test_bit(JOB_MIC, &ai->jobs))
L
Linus Torvalds 已提交
3101
			micinit(ai);
3102
		else if (test_bit(JOB_EVENT, &ai->jobs))
L
Linus Torvalds 已提交
3103
			airo_send_event(dev);
3104
		else if (test_bit(JOB_AUTOWEP, &ai->jobs))
L
Linus Torvalds 已提交
3105
			timer_func(dev);
3106
		else if (test_bit(JOB_SCAN_RESULTS, &ai->jobs))
3107 3108 3109
			airo_process_scan_results(ai);
		else  /* Shouldn't get here, but we make sure to unlock */
			up(&ai->sem);
L
Linus Torvalds 已提交
3110
	}
S
Sukadev Bhattiprolu 已提交
3111 3112

	return 0;
L
Linus Torvalds 已提交
3113 3114
}

3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129
static int header_len(__le16 ctl)
{
	u16 fc = le16_to_cpu(ctl);
	switch (fc & 0xc) {
	case 4:
		if ((fc & 0xe0) == 0xc0)
			return 10;	/* one-address control packet */
		return 16;	/* two-address control packet */
	case 8:
		if ((fc & 0x300) == 0x300)
			return 30;	/* WDS packet */
	}
	return 24;
}

3130 3131 3132
static irqreturn_t airo_interrupt(int irq, void *dev_id)
{
	struct net_device *dev = dev_id;
L
Linus Torvalds 已提交
3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160
	u16 status;
	u16 fid;
	struct airo_info *apriv = dev->priv;
	u16 savedInterrupts = 0;
	int handled = 0;

	if (!netif_device_present(dev))
		return IRQ_NONE;

	for (;;) {
		status = IN4500( apriv, EVSTAT );
		if ( !(status & STATUS_INTS) || status == 0xffff ) break;

		handled = 1;

		if ( status & EV_AWAKE ) {
			OUT4500( apriv, EVACK, EV_AWAKE );
			OUT4500( apriv, EVACK, EV_AWAKE );
		}

		if (!savedInterrupts) {
			savedInterrupts = IN4500( apriv, EVINTEN );
			OUT4500( apriv, EVINTEN, 0 );
		}

		if ( status & EV_MIC ) {
			OUT4500( apriv, EVACK, EV_MIC );
			if (test_bit(FLAG_MIC_CAPABLE, &apriv->flags)) {
3161
				set_bit(JOB_MIC, &apriv->jobs);
L
Linus Torvalds 已提交
3162 3163 3164 3165 3166
				wake_up_interruptible(&apriv->thr_wait);
			}
		}
		if ( status & EV_LINK ) {
			union iwreq_data	wrqu;
3167
			int scan_forceloss = 0;
L
Linus Torvalds 已提交
3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185
			/* The link status has changed, if you want to put a
			   monitor hook in, do it here.  (Remember that
			   interrupts are still disabled!)
			*/
			u16 newStatus = IN4500(apriv, LINKSTAT);
			OUT4500( apriv, EVACK, EV_LINK);
			/* Here is what newStatus means: */
#define NOBEACON 0x8000 /* Loss of sync - missed beacons */
#define MAXRETRIES 0x8001 /* Loss of sync - max retries */
#define MAXARL 0x8002 /* Loss of sync - average retry level exceeded*/
#define FORCELOSS 0x8003 /* Loss of sync - host request */
#define TSFSYNC 0x8004 /* Loss of sync - TSF synchronization */
#define DEAUTH 0x8100 /* Deauthentication (low byte is reason code) */
#define DISASS 0x8200 /* Disassociation (low byte is reason code) */
#define ASSFAIL 0x8400 /* Association failure (low byte is reason
			  code) */
#define AUTHFAIL 0x0300 /* Authentication failure (low byte is reason
			   code) */
3186 3187
#define ASSOCIATED 0x0400 /* Associated */
#define REASSOCIATED 0x0600 /* Reassociated?  Only on firmware >= 5.30.17 */
L
Linus Torvalds 已提交
3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203
#define RC_RESERVED 0 /* Reserved return code */
#define RC_NOREASON 1 /* Unspecified reason */
#define RC_AUTHINV 2 /* Previous authentication invalid */
#define RC_DEAUTH 3 /* Deauthenticated because sending station is
		       leaving */
#define RC_NOACT 4 /* Disassociated due to inactivity */
#define RC_MAXLOAD 5 /* Disassociated because AP is unable to handle
			all currently associated stations */
#define RC_BADCLASS2 6 /* Class 2 frame received from
			  non-Authenticated station */
#define RC_BADCLASS3 7 /* Class 3 frame received from
			  non-Associated station */
#define RC_STATLEAVE 8 /* Disassociated because sending station is
			  leaving BSS */
#define RC_NOAUTH 9 /* Station requesting (Re)Association is not
		       Authenticated with the responding station */
3204 3205 3206
			if (newStatus == FORCELOSS && apriv->scan_timeout > 0)
				scan_forceloss = 1;
			if(newStatus == ASSOCIATED || newStatus == REASSOCIATED) {
L
Linus Torvalds 已提交
3207 3208
				if (auto_wep)
					apriv->expires = 0;
S
Sukadev Bhattiprolu 已提交
3209 3210
				if (apriv->list_bss_task)
					wake_up_process(apriv->list_bss_task);
L
Linus Torvalds 已提交
3211 3212
				set_bit(FLAG_UPDATE_UNI, &apriv->flags);
				set_bit(FLAG_UPDATE_MULTI, &apriv->flags);
3213

L
Linus Torvalds 已提交
3214
				if (down_trylock(&apriv->sem) != 0) {
3215
					set_bit(JOB_EVENT, &apriv->jobs);
L
Linus Torvalds 已提交
3216 3217 3218
					wake_up_interruptible(&apriv->thr_wait);
				} else
					airo_send_event(dev);
3219 3220 3221 3222 3223
			} else if (!scan_forceloss) {
				if (auto_wep && !apriv->expires) {
					apriv->expires = RUN_AT(3*HZ);
					wake_up_interruptible(&apriv->thr_wait);
				}
L
Linus Torvalds 已提交
3224 3225

				/* Send event to user space */
3226 3227
				memset(wrqu.ap_addr.sa_data, '\0', ETH_ALEN);
				wrqu.ap_addr.sa_family = ARPHRD_ETHER;
L
Linus Torvalds 已提交
3228 3229 3230 3231 3232 3233 3234
				wireless_send_event(dev, SIOCGIWAP, &wrqu,NULL);
			}
		}

		/* Check to see if there is something to receive */
		if ( status & EV_RX  ) {
			struct sk_buff *skb = NULL;
3235 3236
			__le16 fc, v;
			u16 len, hdrlen = 0;
L
Linus Torvalds 已提交
3237 3238
#pragma pack(1)
			struct {
A
Al Viro 已提交
3239
				__le16 status, len;
L
Linus Torvalds 已提交
3240 3241 3242
				u8 rssi[2];
				u8 rate;
				u8 freq;
A
Al Viro 已提交
3243
				__le16 tmp[4];
L
Linus Torvalds 已提交
3244 3245 3246
			} hdr;
#pragma pack()
			u16 gap;
3247 3248
			__le16 tmpbuf[4];
			__le16 *buffer;
L
Linus Torvalds 已提交
3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263

			if (test_bit(FLAG_MPI,&apriv->flags)) {
				if (test_bit(FLAG_802_11, &apriv->flags))
					mpi_receive_802_11(apriv);
				else
					mpi_receive_802_3(apriv);
				OUT4500(apriv, EVACK, EV_RX);
				goto exitrx;
			}

			fid = IN4500( apriv, RXFID );

			/* Get the packet length */
			if (test_bit(FLAG_802_11, &apriv->flags)) {
				bap_setup (apriv, fid, 4, BAP0);
3264
				bap_read (apriv, (__le16*)&hdr, sizeof(hdr), BAP0);
L
Linus Torvalds 已提交
3265 3266 3267 3268 3269 3270 3271
				/* Bad CRC. Ignore packet */
				if (le16_to_cpu(hdr.status) & 2)
					hdr.len = 0;
				if (apriv->wifidev == NULL)
					hdr.len = 0;
			} else {
				bap_setup (apriv, fid, 0x36, BAP0);
3272
				bap_read (apriv, &hdr.len, 2, BAP0);
L
Linus Torvalds 已提交
3273 3274 3275
			}
			len = le16_to_cpu(hdr.len);

3276
			if (len > AIRO_DEF_MTU) {
3277
				airo_print_err(apriv->dev->name, "Bad size %d", len);
L
Linus Torvalds 已提交
3278 3279 3280 3281 3282 3283
				goto badrx;
			}
			if (len == 0)
				goto badrx;

			if (test_bit(FLAG_802_11, &apriv->flags)) {
3284 3285
				bap_read (apriv, &fc, sizeof(fc), BAP0);
				hdrlen = header_len(fc);
L
Linus Torvalds 已提交
3286 3287 3288 3289 3290 3291 3292 3293 3294
			} else
				hdrlen = ETH_ALEN * 2;

			skb = dev_alloc_skb( len + hdrlen + 2 + 2 );
			if ( !skb ) {
				apriv->stats.rx_dropped++;
				goto badrx;
			}
			skb_reserve(skb, 2); /* This way the IP header is aligned */
3295
			buffer = (__le16*)skb_put (skb, len + hdrlen);
L
Linus Torvalds 已提交
3296 3297 3298 3299 3300 3301
			if (test_bit(FLAG_802_11, &apriv->flags)) {
				buffer[0] = fc;
				bap_read (apriv, buffer + 1, hdrlen - 2, BAP0);
				if (hdrlen == 24)
					bap_read (apriv, tmpbuf, 6, BAP0);

3302 3303
				bap_read (apriv, &v, sizeof(v), BAP0);
				gap = le16_to_cpu(v);
L
Linus Torvalds 已提交
3304
				if (gap) {
3305
					if (gap <= 8) {
L
Linus Torvalds 已提交
3306
						bap_read (apriv, tmpbuf, gap, BAP0);
3307 3308 3309 3310
					} else {
						airo_print_err(apriv->dev->name, "gaplen too "
							"big. Problems will follow...");
					}
L
Linus Torvalds 已提交
3311 3312 3313 3314 3315 3316
				}
				bap_read (apriv, buffer + hdrlen/2, len, BAP0);
			} else {
				MICBuffer micbuf;
				bap_read (apriv, buffer, ETH_ALEN*2, BAP0);
				if (apriv->micstats.enabled) {
3317
					bap_read (apriv,(__le16*)&micbuf,sizeof(micbuf),BAP0);
L
Linus Torvalds 已提交
3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344
					if (ntohs(micbuf.typelen) > 0x05DC)
						bap_setup (apriv, fid, 0x44, BAP0);
					else {
						if (len <= sizeof(micbuf))
							goto badmic;

						len -= sizeof(micbuf);
						skb_trim (skb, len + hdrlen);
					}
				}
				bap_read(apriv,buffer+ETH_ALEN,len,BAP0);
				if (decapsulate(apriv,&micbuf,(etherHead*)buffer,len)) {
badmic:
					dev_kfree_skb_irq (skb);
badrx:
					OUT4500( apriv, EVACK, EV_RX);
					goto exitrx;
				}
			}
#ifdef WIRELESS_SPY
			if (apriv->spy_data.spy_number > 0) {
				char *sa;
				struct iw_quality wstats;
				/* Prepare spy data : addr + qual */
				if (!test_bit(FLAG_802_11, &apriv->flags)) {
					sa = (char*)buffer + 6;
					bap_setup (apriv, fid, 8, BAP0);
3345
					bap_read (apriv, (__le16*)hdr.rssi, 2, BAP0);
L
Linus Torvalds 已提交
3346 3347 3348 3349 3350 3351 3352
				} else
					sa = (char*)buffer + 10;
				wstats.qual = hdr.rssi[0];
				if (apriv->rssi)
					wstats.level = 0x100 - apriv->rssi[hdr.rssi[1]].rssidBm;
				else
					wstats.level = (hdr.rssi[1] + 321) / 2;
3353 3354 3355
				wstats.noise = apriv->wstats.qual.noise;
				wstats.updated = IW_QUAL_LEVEL_UPDATED
					| IW_QUAL_QUAL_UPDATED
J
Jean Tourrilhes 已提交
3356
					| IW_QUAL_DBM;
L
Linus Torvalds 已提交
3357 3358 3359 3360 3361 3362 3363
				/* Update spy records */
				wireless_spy_update(dev, sa, &wstats);
			}
#endif /* WIRELESS_SPY */
			OUT4500( apriv, EVACK, EV_RX);

			if (test_bit(FLAG_802_11, &apriv->flags)) {
3364
				skb_reset_mac_header(skb);
L
Linus Torvalds 已提交
3365 3366 3367
				skb->pkt_type = PACKET_OTHERHOST;
				skb->dev = apriv->wifidev;
				skb->protocol = htons(ETH_P_802_2);
3368
			} else
L
Linus Torvalds 已提交
3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388
				skb->protocol = eth_type_trans(skb,dev);
			skb->dev->last_rx = jiffies;
			skb->ip_summed = CHECKSUM_NONE;

			netif_rx( skb );
		}
exitrx:

		/* Check to see if a packet has been transmitted */
		if (  status & ( EV_TX|EV_TXCPY|EV_TXEXC ) ) {
			int i;
			int len = 0;
			int index = -1;

			if (test_bit(FLAG_MPI,&apriv->flags)) {
				unsigned long flags;

				if (status & EV_TXEXC)
					get_tx_error(apriv, -1);
				spin_lock_irqsave(&apriv->aux_lock, flags);
3389
				if (!skb_queue_empty(&apriv->txq)) {
L
Linus Torvalds 已提交
3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424
					spin_unlock_irqrestore(&apriv->aux_lock,flags);
					mpi_send_packet (dev);
				} else {
					clear_bit(FLAG_PENDING_XMIT, &apriv->flags);
					spin_unlock_irqrestore(&apriv->aux_lock,flags);
					netif_wake_queue (dev);
				}
				OUT4500( apriv, EVACK,
					status & (EV_TX|EV_TXCPY|EV_TXEXC));
				goto exittx;
			}

			fid = IN4500(apriv, TXCOMPLFID);

			for( i = 0; i < MAX_FIDS; i++ ) {
				if ( ( apriv->fids[i] & 0xffff ) == fid ) {
					len = apriv->fids[i] >> 16;
					index = i;
				}
			}
			if (index != -1) {
				if (status & EV_TXEXC)
					get_tx_error(apriv, index);
				OUT4500( apriv, EVACK, status & (EV_TX | EV_TXEXC));
				/* Set up to be used again */
				apriv->fids[index] &= 0xffff;
				if (index < MAX_FIDS / 2) {
					if (!test_bit(FLAG_PENDING_XMIT, &apriv->flags))
						netif_wake_queue(dev);
				} else {
					if (!test_bit(FLAG_PENDING_XMIT11, &apriv->flags))
						netif_wake_queue(apriv->wifidev);
				}
			} else {
				OUT4500( apriv, EVACK, status & (EV_TX | EV_TXCPY | EV_TXEXC));
3425 3426
				airo_print_err(apriv->dev->name, "Unallocated FID was "
					"used to xmit" );
L
Linus Torvalds 已提交
3427 3428 3429 3430
			}
		}
exittx:
		if ( status & ~STATUS_INTS & ~IGNORE_INTS )
3431
			airo_print_warn(apriv->dev->name, "Got weird status %x",
L
Linus Torvalds 已提交
3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475
				status & ~STATUS_INTS & ~IGNORE_INTS );
	}

	if (savedInterrupts)
		OUT4500( apriv, EVINTEN, savedInterrupts );

	/* done.. */
	return IRQ_RETVAL(handled);
}

/*
 *  Routines to talk to the card
 */

/*
 *  This was originally written for the 4500, hence the name
 *  NOTE:  If use with 8bit mode and SMP bad things will happen!
 *         Why would some one do 8 bit IO in an SMP machine?!?
 */
static void OUT4500( struct airo_info *ai, u16 reg, u16 val ) {
	if (test_bit(FLAG_MPI,&ai->flags))
		reg <<= 1;
	if ( !do8bitIO )
		outw( val, ai->dev->base_addr + reg );
	else {
		outb( val & 0xff, ai->dev->base_addr + reg );
		outb( val >> 8, ai->dev->base_addr + reg + 1 );
	}
}

static u16 IN4500( struct airo_info *ai, u16 reg ) {
	unsigned short rc;

	if (test_bit(FLAG_MPI,&ai->flags))
		reg <<= 1;
	if ( !do8bitIO )
		rc = inw( ai->dev->base_addr + reg );
	else {
		rc = inb( ai->dev->base_addr + reg );
		rc += ((int)inb( ai->dev->base_addr + reg + 1 )) << 8;
	}
	return rc;
}

3476 3477
static int enable_MAC(struct airo_info *ai, int lock)
{
L
Linus Torvalds 已提交
3478
	int rc;
3479 3480
	Cmd cmd;
	Resp rsp;
L
Linus Torvalds 已提交
3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495

	/* FLAG_RADIO_OFF : Radio disabled via /proc or Wireless Extensions
	 * FLAG_RADIO_DOWN : Radio disabled via "ifconfig ethX down"
	 * Note : we could try to use !netif_running(dev) in enable_MAC()
	 * instead of this flag, but I don't trust it *within* the
	 * open/close functions, and testing both flags together is
	 * "cheaper" - Jean II */
	if (ai->flags & FLAG_RADIO_MASK) return SUCCESS;

	if (lock && down_interruptible(&ai->sem))
		return -ERESTARTSYS;

	if (!test_bit(FLAG_ENABLED, &ai->flags)) {
		memset(&cmd, 0, sizeof(cmd));
		cmd.cmd = MAC_ENABLE;
3496
		rc = issuecommand(ai, &cmd, &rsp);
L
Linus Torvalds 已提交
3497 3498 3499 3500 3501 3502 3503 3504 3505
		if (rc == SUCCESS)
			set_bit(FLAG_ENABLED, &ai->flags);
	} else
		rc = SUCCESS;

	if (lock)
	    up(&ai->sem);

	if (rc)
3506 3507 3508 3509 3510 3511
		airo_print_err(ai->dev->name, "Cannot enable MAC");
	else if ((rsp.status & 0xFF00) != 0) {
		airo_print_err(ai->dev->name, "Bad MAC enable reason=%x, "
			"rid=%x, offset=%d", rsp.rsp0, rsp.rsp1, rsp.rsp2);
		rc = ERROR;
	}
L
Linus Torvalds 已提交
3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615
	return rc;
}

static void disable_MAC( struct airo_info *ai, int lock ) {
        Cmd cmd;
	Resp rsp;

	if (lock && down_interruptible(&ai->sem))
		return;

	if (test_bit(FLAG_ENABLED, &ai->flags)) {
		memset(&cmd, 0, sizeof(cmd));
		cmd.cmd = MAC_DISABLE; // disable in case already enabled
		issuecommand(ai, &cmd, &rsp);
		clear_bit(FLAG_ENABLED, &ai->flags);
	}
	if (lock)
		up(&ai->sem);
}

static void enable_interrupts( struct airo_info *ai ) {
	/* Enable the interrupts */
	OUT4500( ai, EVINTEN, STATUS_INTS );
}

static void disable_interrupts( struct airo_info *ai ) {
	OUT4500( ai, EVINTEN, 0 );
}

static void mpi_receive_802_3(struct airo_info *ai)
{
	RxFid rxd;
	int len = 0;
	struct sk_buff *skb;
	char *buffer;
	int off = 0;
	MICBuffer micbuf;

	memcpy_fromio(&rxd, ai->rxfids[0].card_ram_off, sizeof(rxd));
	/* Make sure we got something */
	if (rxd.rdy && rxd.valid == 0) {
		len = rxd.len + 12;
		if (len < 12 || len > 2048)
			goto badrx;

		skb = dev_alloc_skb(len);
		if (!skb) {
			ai->stats.rx_dropped++;
			goto badrx;
		}
		buffer = skb_put(skb,len);
		memcpy(buffer, ai->rxfids[0].virtual_host_addr, ETH_ALEN * 2);
		if (ai->micstats.enabled) {
			memcpy(&micbuf,
				ai->rxfids[0].virtual_host_addr + ETH_ALEN * 2,
				sizeof(micbuf));
			if (ntohs(micbuf.typelen) <= 0x05DC) {
				if (len <= sizeof(micbuf) + ETH_ALEN * 2)
					goto badmic;

				off = sizeof(micbuf);
				skb_trim (skb, len - off);
			}
		}
		memcpy(buffer + ETH_ALEN * 2,
			ai->rxfids[0].virtual_host_addr + ETH_ALEN * 2 + off,
			len - ETH_ALEN * 2 - off);
		if (decapsulate (ai, &micbuf, (etherHead*)buffer, len - off - ETH_ALEN * 2)) {
badmic:
			dev_kfree_skb_irq (skb);
			goto badrx;
		}
#ifdef WIRELESS_SPY
		if (ai->spy_data.spy_number > 0) {
			char *sa;
			struct iw_quality wstats;
			/* Prepare spy data : addr + qual */
			sa = buffer + ETH_ALEN;
			wstats.qual = 0; /* XXX Where do I get that info from ??? */
			wstats.level = 0;
			wstats.updated = 0;
			/* Update spy records */
			wireless_spy_update(ai->dev, sa, &wstats);
		}
#endif /* WIRELESS_SPY */

		skb->ip_summed = CHECKSUM_NONE;
		skb->protocol = eth_type_trans(skb, ai->dev);
		skb->dev->last_rx = jiffies;
		netif_rx(skb);
	}
badrx:
	if (rxd.valid == 0) {
		rxd.valid = 1;
		rxd.rdy = 0;
		rxd.len = PKTSIZE;
		memcpy_toio(ai->rxfids[0].card_ram_off, &rxd, sizeof(rxd));
	}
}

void mpi_receive_802_11 (struct airo_info *ai)
{
	RxFid rxd;
	struct sk_buff *skb = NULL;
3616 3617
	u16 len, hdrlen = 0;
	__le16 fc;
L
Linus Torvalds 已提交
3618 3619
#pragma pack(1)
	struct {
A
Al Viro 已提交
3620
		__le16 status, len;
L
Linus Torvalds 已提交
3621 3622 3623
		u8 rssi[2];
		u8 rate;
		u8 freq;
A
Al Viro 已提交
3624
		__le16 tmp[4];
L
Linus Torvalds 已提交
3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639
	} hdr;
#pragma pack()
	u16 gap;
	u16 *buffer;
	char *ptr = ai->rxfids[0].virtual_host_addr+4;

	memcpy_fromio(&rxd, ai->rxfids[0].card_ram_off, sizeof(rxd));
	memcpy ((char *)&hdr, ptr, sizeof(hdr));
	ptr += sizeof(hdr);
	/* Bad CRC. Ignore packet */
	if (le16_to_cpu(hdr.status) & 2)
		hdr.len = 0;
	if (ai->wifidev == NULL)
		hdr.len = 0;
	len = le16_to_cpu(hdr.len);
3640
	if (len > AIRO_DEF_MTU) {
3641
		airo_print_err(ai->dev->name, "Bad size %d", len);
L
Linus Torvalds 已提交
3642 3643 3644 3645 3646
		goto badrx;
	}
	if (len == 0)
		goto badrx;

3647 3648
	fc = get_unaligned((__le16 *)ptr);
	hdrlen = header_len(fc);
L
Linus Torvalds 已提交
3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659

	skb = dev_alloc_skb( len + hdrlen + 2 );
	if ( !skb ) {
		ai->stats.rx_dropped++;
		goto badrx;
	}
	buffer = (u16*)skb_put (skb, len + hdrlen);
	memcpy ((char *)buffer, ptr, hdrlen);
	ptr += hdrlen;
	if (hdrlen == 24)
		ptr += 6;
3660
	gap = get_unaligned_le16(ptr);
A
Al Viro 已提交
3661
	ptr += sizeof(__le16);
L
Linus Torvalds 已提交
3662 3663 3664 3665
	if (gap) {
		if (gap <= 8)
			ptr += gap;
		else
3666 3667
			airo_print_err(ai->dev->name,
			    "gaplen too big. Problems will follow...");
L
Linus Torvalds 已提交
3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681
	}
	memcpy ((char *)buffer + hdrlen, ptr, len);
	ptr += len;
#ifdef IW_WIRELESS_SPY	  /* defined in iw_handler.h */
	if (ai->spy_data.spy_number > 0) {
		char *sa;
		struct iw_quality wstats;
		/* Prepare spy data : addr + qual */
		sa = (char*)buffer + 10;
		wstats.qual = hdr.rssi[0];
		if (ai->rssi)
			wstats.level = 0x100 - ai->rssi[hdr.rssi[1]].rssidBm;
		else
			wstats.level = (hdr.rssi[1] + 321) / 2;
3682 3683 3684
		wstats.noise = ai->wstats.qual.noise;
		wstats.updated = IW_QUAL_QUAL_UPDATED
			| IW_QUAL_LEVEL_UPDATED
J
Jean Tourrilhes 已提交
3685
			| IW_QUAL_DBM;
L
Linus Torvalds 已提交
3686 3687 3688 3689
		/* Update spy records */
		wireless_spy_update(ai->dev, sa, &wstats);
	}
#endif /* IW_WIRELESS_SPY */
3690
	skb_reset_mac_header(skb);
L
Linus Torvalds 已提交
3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712
	skb->pkt_type = PACKET_OTHERHOST;
	skb->dev = ai->wifidev;
	skb->protocol = htons(ETH_P_802_2);
	skb->dev->last_rx = jiffies;
	skb->ip_summed = CHECKSUM_NONE;
	netif_rx( skb );
badrx:
	if (rxd.valid == 0) {
		rxd.valid = 1;
		rxd.rdy = 0;
		rxd.len = PKTSIZE;
		memcpy_toio(ai->rxfids[0].card_ram_off, &rxd, sizeof(rxd));
	}
}

static u16 setup_card(struct airo_info *ai, u8 *mac, int lock)
{
	Cmd cmd;
	Resp rsp;
	int status;
	int i;
	SsidRid mySsid;
A
Al Viro 已提交
3713
	__le16 lastindex;
L
Linus Torvalds 已提交
3714 3715 3716 3717
	WepKeyRid wkr;
	int rc;

	memset( &mySsid, 0, sizeof( mySsid ) );
3718 3719
	kfree (ai->flash);
	ai->flash = NULL;
L
Linus Torvalds 已提交
3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738

	/* The NOP is the first step in getting the card going */
	cmd.cmd = NOP;
	cmd.parm0 = cmd.parm1 = cmd.parm2 = 0;
	if (lock && down_interruptible(&ai->sem))
		return ERROR;
	if ( issuecommand( ai, &cmd, &rsp ) != SUCCESS ) {
		if (lock)
			up(&ai->sem);
		return ERROR;
	}
	disable_MAC( ai, 0);

	// Let's figure out if we need to use the AUX port
	if (!test_bit(FLAG_MPI,&ai->flags)) {
		cmd.cmd = CMD_ENABLEAUX;
		if (issuecommand(ai, &cmd, &rsp) != SUCCESS) {
			if (lock)
				up(&ai->sem);
3739
			airo_print_err(ai->dev->name, "Error checking for AUX port");
L
Linus Torvalds 已提交
3740 3741 3742 3743
			return ERROR;
		}
		if (!aux_bap || rsp.status & 0xff00) {
			ai->bap_read = fast_bap_read;
3744
			airo_print_dbg(ai->dev->name, "Doing fast bap_reads");
L
Linus Torvalds 已提交
3745 3746
		} else {
			ai->bap_read = aux_bap_read;
3747
			airo_print_dbg(ai->dev->name, "Doing AUX bap_reads");
L
Linus Torvalds 已提交
3748 3749 3750 3751 3752 3753 3754 3755
		}
	}
	if (lock)
		up(&ai->sem);
	if (ai->config.len == 0) {
		tdsRssiRid rssi_rid;
		CapabilityRid cap_rid;

3756 3757 3758 3759
		kfree(ai->APList);
		ai->APList = NULL;
		kfree(ai->SSID);
		ai->SSID = NULL;
L
Linus Torvalds 已提交
3760 3761 3762 3763 3764 3765 3766 3767 3768 3769
		// general configuration (read/modify/write)
		status = readConfigRid(ai, lock);
		if ( status != SUCCESS ) return ERROR;

		status = readCapabilityRid(ai, &cap_rid, lock);
		if ( status != SUCCESS ) return ERROR;

		status = PC4500_readrid(ai,RID_RSSI,&rssi_rid,sizeof(rssi_rid),lock);
		if ( status == SUCCESS ) {
			if (ai->rssi || (ai->rssi = kmalloc(512, GFP_KERNEL)) != NULL)
3770
				memcpy(ai->rssi, (u8*)&rssi_rid + 2, 512); /* Skip RID length member */
L
Linus Torvalds 已提交
3771 3772
		}
		else {
3773 3774
			kfree(ai->rssi);
			ai->rssi = NULL;
3775
			if (cap_rid.softCap & cpu_to_le16(8))
L
Linus Torvalds 已提交
3776 3777
				ai->config.rmode |= RXMODE_NORMALIZED_RSSI;
			else
3778 3779
				airo_print_warn(ai->dev->name, "unknown received signal "
						"level scale");
L
Linus Torvalds 已提交
3780 3781 3782 3783 3784
		}
		ai->config.opmode = adhoc ? MODE_STA_IBSS : MODE_STA_ESS;
		ai->config.authType = AUTH_OPEN;
		ai->config.modulation = MOD_CCK;

3785
		if (le16_to_cpu(cap_rid.len) >= sizeof(cap_rid) &&
3786
		    (cap_rid.extSoftCap & cpu_to_le16(1)) &&
3787
		    micsetup(ai) == SUCCESS) {
L
Linus Torvalds 已提交
3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822
			ai->config.opmode |= MODE_MIC;
			set_bit(FLAG_MIC_CAPABLE, &ai->flags);
		}

		/* Save off the MAC */
		for( i = 0; i < ETH_ALEN; i++ ) {
			mac[i] = ai->config.macAddr[i];
		}

		/* Check to see if there are any insmod configured
		   rates to add */
		if ( rates[0] ) {
			int i = 0;
			memset(ai->config.rates,0,sizeof(ai->config.rates));
			for( i = 0; i < 8 && rates[i]; i++ ) {
				ai->config.rates[i] = rates[i];
			}
		}
		if ( basic_rate > 0 ) {
			int i;
			for( i = 0; i < 8; i++ ) {
				if ( ai->config.rates[i] == basic_rate ||
				     !ai->config.rates ) {
					ai->config.rates[i] = basic_rate | 0x80;
					break;
				}
			}
		}
		set_bit (FLAG_COMMIT, &ai->flags);
	}

	/* Setup the SSIDs if present */
	if ( ssids[0] ) {
		int i;
		for( i = 0; i < 3 && ssids[i]; i++ ) {
A
Al Viro 已提交
3823 3824 3825 3826 3827
			size_t len = strlen(ssids[i]);
			if (len > 32)
				len = 32;
			mySsid.ssids[i].len = cpu_to_le16(len);
			memcpy(mySsid.ssids[i].ssid, ssids[i], len);
L
Linus Torvalds 已提交
3828
		}
A
Al Viro 已提交
3829
		mySsid.len = cpu_to_le16(sizeof(mySsid));
L
Linus Torvalds 已提交
3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840
	}

	status = writeConfigRid(ai, lock);
	if ( status != SUCCESS ) return ERROR;

	/* Set up the SSID list */
	if ( ssids[0] ) {
		status = writeSsidRid(ai, &mySsid, lock);
		if ( status != SUCCESS ) return ERROR;
	}

3841 3842
	status = enable_MAC(ai, lock);
	if (status != SUCCESS)
L
Linus Torvalds 已提交
3843 3844 3845 3846 3847 3848
		return ERROR;

	/* Grab the initial wep key, we gotta save it for auto_wep */
	rc = readWepKeyRid(ai, &wkr, 1, lock);
	if (rc == SUCCESS) do {
		lastindex = wkr.kindex;
A
Al Viro 已提交
3849
		if (wkr.kindex == cpu_to_le16(0xffff)) {
L
Linus Torvalds 已提交
3850 3851 3852 3853 3854
			ai->defindex = wkr.mac[0];
		}
		rc = readWepKeyRid(ai, &wkr, 0, lock);
	} while(lastindex != wkr.kindex);

3855
	try_auto_wep(ai);
L
Linus Torvalds 已提交
3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880

	return SUCCESS;
}

static u16 issuecommand(struct airo_info *ai, Cmd *pCmd, Resp *pRsp) {
        // Im really paranoid about letting it run forever!
	int max_tries = 600000;

	if (IN4500(ai, EVSTAT) & EV_CMD)
		OUT4500(ai, EVACK, EV_CMD);

	OUT4500(ai, PARAM0, pCmd->parm0);
	OUT4500(ai, PARAM1, pCmd->parm1);
	OUT4500(ai, PARAM2, pCmd->parm2);
	OUT4500(ai, COMMAND, pCmd->cmd);

	while (max_tries-- && (IN4500(ai, EVSTAT) & EV_CMD) == 0) {
		if ((IN4500(ai, COMMAND)) == pCmd->cmd)
			// PC4500 didn't notice command, try again
			OUT4500(ai, COMMAND, pCmd->cmd);
		if (!in_atomic() && (max_tries & 255) == 0)
			schedule();
	}

	if ( max_tries == -1 ) {
3881 3882
		airo_print_err(ai->dev->name,
			"Max tries exceeded when issueing command");
L
Linus Torvalds 已提交
3883 3884 3885 3886 3887 3888 3889 3890 3891 3892
		if (IN4500(ai, COMMAND) & COMMAND_BUSY)
			OUT4500(ai, EVACK, EV_CLEARCOMMANDBUSY);
		return ERROR;
	}

	// command completed
	pRsp->status = IN4500(ai, STATUS);
	pRsp->rsp0 = IN4500(ai, RESP0);
	pRsp->rsp1 = IN4500(ai, RESP1);
	pRsp->rsp2 = IN4500(ai, RESP2);
3893 3894 3895 3896 3897
	if ((pRsp->status & 0xff00)!=0 && pCmd->cmd != CMD_SOFTRESET)
		airo_print_err(ai->dev->name,
			"cmd:%x status:%x rsp0:%x rsp1:%x rsp2:%x",
			pCmd->cmd, pRsp->status, pRsp->rsp0, pRsp->rsp1,
			pRsp->rsp2);
L
Linus Torvalds 已提交
3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928

	// clear stuck command busy if necessary
	if (IN4500(ai, COMMAND) & COMMAND_BUSY) {
		OUT4500(ai, EVACK, EV_CLEARCOMMANDBUSY);
	}
	// acknowledge processing the status/response
	OUT4500(ai, EVACK, EV_CMD);

	return SUCCESS;
}

/* Sets up the bap to start exchange data.  whichbap should
 * be one of the BAP0 or BAP1 defines.  Locks should be held before
 * calling! */
static int bap_setup(struct airo_info *ai, u16 rid, u16 offset, int whichbap )
{
	int timeout = 50;
	int max_tries = 3;

	OUT4500(ai, SELECT0+whichbap, rid);
	OUT4500(ai, OFFSET0+whichbap, offset);
	while (1) {
		int status = IN4500(ai, OFFSET0+whichbap);
		if (status & BAP_BUSY) {
                        /* This isn't really a timeout, but its kinda
			   close */
			if (timeout--) {
				continue;
			}
		} else if ( status & BAP_ERR ) {
			/* invalid rid or offset */
3929
			airo_print_err(ai->dev->name, "BAP error %x %d",
L
Linus Torvalds 已提交
3930 3931 3932 3933 3934 3935
				status, whichbap );
			return ERROR;
		} else if (status & BAP_DONE) { // success
			return SUCCESS;
		}
		if ( !(max_tries--) ) {
3936
			airo_print_err(ai->dev->name,
3937
				"BAP setup error too many retries\n");
L
Linus Torvalds 已提交
3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963
			return ERROR;
		}
		// -- PC4500 missed it, try again
		OUT4500(ai, SELECT0+whichbap, rid);
		OUT4500(ai, OFFSET0+whichbap, offset);
		timeout = 50;
	}
}

/* should only be called by aux_bap_read.  This aux function and the
   following use concepts not documented in the developers guide.  I
   got them from a patch given to my by Aironet */
static u16 aux_setup(struct airo_info *ai, u16 page,
		     u16 offset, u16 *len)
{
	u16 next;

	OUT4500(ai, AUXPAGE, page);
	OUT4500(ai, AUXOFF, 0);
	next = IN4500(ai, AUXDATA);
	*len = IN4500(ai, AUXDATA)&0xff;
	if (offset != 4) OUT4500(ai, AUXOFF, offset);
	return next;
}

/* requires call to bap_setup() first */
3964
static int aux_bap_read(struct airo_info *ai, __le16 *pu16Dst,
L
Linus Torvalds 已提交
3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000
			int bytelen, int whichbap)
{
	u16 len;
	u16 page;
	u16 offset;
	u16 next;
	int words;
	int i;
	unsigned long flags;

	spin_lock_irqsave(&ai->aux_lock, flags);
	page = IN4500(ai, SWS0+whichbap);
	offset = IN4500(ai, SWS2+whichbap);
	next = aux_setup(ai, page, offset, &len);
	words = (bytelen+1)>>1;

	for (i=0; i<words;) {
		int count;
		count = (len>>1) < (words-i) ? (len>>1) : (words-i);
		if ( !do8bitIO )
			insw( ai->dev->base_addr+DATA0+whichbap,
			      pu16Dst+i,count );
		else
			insb( ai->dev->base_addr+DATA0+whichbap,
			      pu16Dst+i, count << 1 );
		i += count;
		if (i<words) {
			next = aux_setup(ai, next, 4, &len);
		}
	}
	spin_unlock_irqrestore(&ai->aux_lock, flags);
	return SUCCESS;
}


/* requires call to bap_setup() first */
4001
static int fast_bap_read(struct airo_info *ai, __le16 *pu16Dst,
L
Linus Torvalds 已提交
4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012
			 int bytelen, int whichbap)
{
	bytelen = (bytelen + 1) & (~1); // round up to even value
	if ( !do8bitIO )
		insw( ai->dev->base_addr+DATA0+whichbap, pu16Dst, bytelen>>1 );
	else
		insb( ai->dev->base_addr+DATA0+whichbap, pu16Dst, bytelen );
	return SUCCESS;
}

/* requires call to bap_setup() first */
4013
static int bap_write(struct airo_info *ai, const __le16 *pu16Src,
L
Linus Torvalds 已提交
4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088
		     int bytelen, int whichbap)
{
	bytelen = (bytelen + 1) & (~1); // round up to even value
	if ( !do8bitIO )
		outsw( ai->dev->base_addr+DATA0+whichbap,
		       pu16Src, bytelen>>1 );
	else
		outsb( ai->dev->base_addr+DATA0+whichbap, pu16Src, bytelen );
	return SUCCESS;
}

static int PC4500_accessrid(struct airo_info *ai, u16 rid, u16 accmd)
{
	Cmd cmd; /* for issuing commands */
	Resp rsp; /* response from commands */
	u16 status;

	memset(&cmd, 0, sizeof(cmd));
	cmd.cmd = accmd;
	cmd.parm0 = rid;
	status = issuecommand(ai, &cmd, &rsp);
	if (status != 0) return status;
	if ( (rsp.status & 0x7F00) != 0) {
		return (accmd << 8) + (rsp.rsp0 & 0xFF);
	}
	return 0;
}

/*  Note, that we are using BAP1 which is also used by transmit, so
 *  we must get a lock. */
static int PC4500_readrid(struct airo_info *ai, u16 rid, void *pBuf, int len, int lock)
{
	u16 status;
        int rc = SUCCESS;

	if (lock) {
		if (down_interruptible(&ai->sem))
			return ERROR;
	}
	if (test_bit(FLAG_MPI,&ai->flags)) {
		Cmd cmd;
		Resp rsp;

		memset(&cmd, 0, sizeof(cmd));
		memset(&rsp, 0, sizeof(rsp));
		ai->config_desc.rid_desc.valid = 1;
		ai->config_desc.rid_desc.len = RIDSIZE;
		ai->config_desc.rid_desc.rid = 0;
		ai->config_desc.rid_desc.host_addr = ai->ridbus;

		cmd.cmd = CMD_ACCESS;
		cmd.parm0 = rid;

		memcpy_toio(ai->config_desc.card_ram_off,
			&ai->config_desc.rid_desc, sizeof(Rid));

		rc = issuecommand(ai, &cmd, &rsp);

		if (rsp.status & 0x7f00)
			rc = rsp.rsp0;
		if (!rc)
			memcpy(pBuf, ai->config_desc.virtual_host_addr, len);
		goto done;
	} else {
		if ((status = PC4500_accessrid(ai, rid, CMD_ACCESS))!=SUCCESS) {
	                rc = status;
	                goto done;
	        }
		if (bap_setup(ai, rid, 0, BAP1) != SUCCESS) {
			rc = ERROR;
	                goto done;
	        }
		// read the rid length field
		bap_read(ai, pBuf, 2, BAP1);
		// length for remaining part of rid
A
Al Viro 已提交
4089
		len = min(len, (int)le16_to_cpu(*(__le16*)pBuf)) - 2;
L
Linus Torvalds 已提交
4090 4091

		if ( len <= 2 ) {
4092 4093
			airo_print_err(ai->dev->name,
				"Rid %x has a length of %d which is too short",
L
Linus Torvalds 已提交
4094 4095 4096 4097 4098
				(int)rid, (int)len );
			rc = ERROR;
	                goto done;
		}
		// read remainder of the rid
4099
		rc = bap_read(ai, ((__le16*)pBuf)+1, len, BAP1);
L
Linus Torvalds 已提交
4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114
	}
done:
	if (lock)
		up(&ai->sem);
	return rc;
}

/*  Note, that we are using BAP1 which is also used by transmit, so
 *  make sure this isnt called when a transmit is happening */
static int PC4500_writerid(struct airo_info *ai, u16 rid,
			   const void *pBuf, int len, int lock)
{
	u16 status;
	int rc = SUCCESS;

A
Al Viro 已提交
4115
	*(__le16*)pBuf = cpu_to_le16((u16)len);
L
Linus Torvalds 已提交
4116 4117 4118 4119 4120 4121 4122 4123 4124

	if (lock) {
		if (down_interruptible(&ai->sem))
			return ERROR;
	}
	if (test_bit(FLAG_MPI,&ai->flags)) {
		Cmd cmd;
		Resp rsp;

4125
		if (test_bit(FLAG_ENABLED, &ai->flags) && (RID_WEP_TEMP != rid))
4126 4127
			airo_print_err(ai->dev->name,
				"%s: MAC should be disabled (rid=%04x)",
L
Linus Torvalds 已提交
4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142
				__FUNCTION__, rid);
		memset(&cmd, 0, sizeof(cmd));
		memset(&rsp, 0, sizeof(rsp));

		ai->config_desc.rid_desc.valid = 1;
		ai->config_desc.rid_desc.len = *((u16 *)pBuf);
		ai->config_desc.rid_desc.rid = 0;

		cmd.cmd = CMD_WRITERID;
		cmd.parm0 = rid;

		memcpy_toio(ai->config_desc.card_ram_off,
			&ai->config_desc.rid_desc, sizeof(Rid));

		if (len < 4 || len > 2047) {
4143
			airo_print_err(ai->dev->name, "%s: len=%d", __FUNCTION__, len);
L
Linus Torvalds 已提交
4144 4145 4146 4147 4148 4149 4150
			rc = -1;
		} else {
			memcpy((char *)ai->config_desc.virtual_host_addr,
				pBuf, len);

			rc = issuecommand(ai, &cmd, &rsp);
			if ((rc & 0xff00) != 0) {
4151 4152 4153 4154
				airo_print_err(ai->dev->name, "%s: Write rid Error %d",
						__FUNCTION__, rc);
				airo_print_err(ai->dev->name, "%s: Cmd=%04x",
						__FUNCTION__, cmd.cmd);
L
Linus Torvalds 已提交
4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188
			}

			if ((rsp.status & 0x7f00))
				rc = rsp.rsp0;
		}
	} else {
		// --- first access so that we can write the rid data
		if ( (status = PC4500_accessrid(ai, rid, CMD_ACCESS)) != 0) {
	                rc = status;
	                goto done;
	        }
		// --- now write the rid data
		if (bap_setup(ai, rid, 0, BAP1) != SUCCESS) {
	                rc = ERROR;
	                goto done;
	        }
		bap_write(ai, pBuf, len, BAP1);
		// ---now commit the rid data
		rc = PC4500_accessrid(ai, rid, 0x100|CMD_ACCESS);
	}
done:
	if (lock)
		up(&ai->sem);
        return rc;
}

/* Allocates a FID to be used for transmitting packets.  We only use
   one for now. */
static u16 transmit_allocate(struct airo_info *ai, int lenPayload, int raw)
{
	unsigned int loop = 3000;
	Cmd cmd;
	Resp rsp;
	u16 txFid;
A
Al Viro 已提交
4189
	__le16 txControl;
L
Linus Torvalds 已提交
4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242

	cmd.cmd = CMD_ALLOCATETX;
	cmd.parm0 = lenPayload;
	if (down_interruptible(&ai->sem))
		return ERROR;
	if (issuecommand(ai, &cmd, &rsp) != SUCCESS) {
		txFid = ERROR;
		goto done;
	}
	if ( (rsp.status & 0xFF00) != 0) {
		txFid = ERROR;
		goto done;
	}
	/* wait for the allocate event/indication
	 * It makes me kind of nervous that this can just sit here and spin,
	 * but in practice it only loops like four times. */
	while (((IN4500(ai, EVSTAT) & EV_ALLOC) == 0) && --loop);
	if (!loop) {
		txFid = ERROR;
		goto done;
	}

	// get the allocated fid and acknowledge
	txFid = IN4500(ai, TXALLOCFID);
	OUT4500(ai, EVACK, EV_ALLOC);

	/*  The CARD is pretty cool since it converts the ethernet packet
	 *  into 802.11.  Also note that we don't release the FID since we
	 *  will be using the same one over and over again. */
	/*  We only have to setup the control once since we are not
	 *  releasing the fid. */
	if (raw)
		txControl = cpu_to_le16(TXCTL_TXOK | TXCTL_TXEX | TXCTL_802_11
			| TXCTL_ETHERNET | TXCTL_NORELEASE);
	else
		txControl = cpu_to_le16(TXCTL_TXOK | TXCTL_TXEX | TXCTL_802_3
			| TXCTL_ETHERNET | TXCTL_NORELEASE);
	if (bap_setup(ai, txFid, 0x0008, BAP1) != SUCCESS)
		txFid = ERROR;
	else
		bap_write(ai, &txControl, sizeof(txControl), BAP1);

done:
	up(&ai->sem);

	return txFid;
}

/* In general BAP1 is dedicated to transmiting packets.  However,
   since we need a BAP when accessing RIDs, we also use BAP1 for that.
   Make sure the BAP1 spinlock is held when this is called. */
static int transmit_802_3_packet(struct airo_info *ai, int len, char *pPacket)
{
A
Al Viro 已提交
4243
	__le16 payloadLen;
L
Linus Torvalds 已提交
4244 4245 4246 4247 4248 4249 4250 4251 4252
	Cmd cmd;
	Resp rsp;
	int miclen = 0;
	u16 txFid = len;
	MICBuffer pMic;

	len >>= 16;

	if (len <= ETH_ALEN * 2) {
4253
		airo_print_warn(ai->dev->name, "Short packet %d", len);
L
Linus Torvalds 已提交
4254 4255 4256 4257 4258
		return ERROR;
	}
	len -= ETH_ALEN * 2;

	if (test_bit(FLAG_MIC_CAPABLE, &ai->flags) && ai->micstats.enabled && 
A
Al Viro 已提交
4259
	    (ntohs(((__be16 *)pPacket)[6]) != 0x888E)) {
L
Linus Torvalds 已提交
4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270
		if (encapsulate(ai,(etherHead *)pPacket,&pMic,len) != SUCCESS)
			return ERROR;
		miclen = sizeof(pMic);
	}
	// packet is destination[6], source[6], payload[len-12]
	// write the payload length and dst/src/payload
	if (bap_setup(ai, txFid, 0x0036, BAP1) != SUCCESS) return ERROR;
	/* The hardware addresses aren't counted as part of the payload, so
	 * we have to subtract the 12 bytes for the addresses off */
	payloadLen = cpu_to_le16(len + miclen);
	bap_write(ai, &payloadLen, sizeof(payloadLen),BAP1);
4271
	bap_write(ai, (__le16*)pPacket, sizeof(etherHead), BAP1);
L
Linus Torvalds 已提交
4272
	if (miclen)
4273 4274
		bap_write(ai, (__le16*)&pMic, miclen, BAP1);
	bap_write(ai, (__le16*)(pPacket + sizeof(etherHead)), len, BAP1);
L
Linus Torvalds 已提交
4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285
	// issue the transmit command
	memset( &cmd, 0, sizeof( cmd ) );
	cmd.cmd = CMD_TRANSMIT;
	cmd.parm0 = txFid;
	if (issuecommand(ai, &cmd, &rsp) != SUCCESS) return ERROR;
	if ( (rsp.status & 0xFF00) != 0) return ERROR;
	return SUCCESS;
}

static int transmit_802_11_packet(struct airo_info *ai, int len, char *pPacket)
{
A
Al Viro 已提交
4286
	__le16 fc, payloadLen;
L
Linus Torvalds 已提交
4287 4288 4289
	Cmd cmd;
	Resp rsp;
	int hdrlen;
A
Al Viro 已提交
4290 4291
	static u8 tail[(30-10) + 2 + 6] = {[30-10] = 6};
	/* padding of header to full size + le16 gaplen (6) + gaplen bytes */
L
Linus Torvalds 已提交
4292 4293 4294
	u16 txFid = len;
	len >>= 16;

4295 4296
	fc = *(__le16*)pPacket;
	hdrlen = header_len(fc);
L
Linus Torvalds 已提交
4297 4298

	if (len < hdrlen) {
4299
		airo_print_warn(ai->dev->name, "Short packet %d", len);
L
Linus Torvalds 已提交
4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310
		return ERROR;
	}

	/* packet is 802.11 header +  payload
	 * write the payload length and dst/src/payload */
	if (bap_setup(ai, txFid, 6, BAP1) != SUCCESS) return ERROR;
	/* The 802.11 header aren't counted as part of the payload, so
	 * we have to subtract the header bytes off */
	payloadLen = cpu_to_le16(len-hdrlen);
	bap_write(ai, &payloadLen, sizeof(payloadLen),BAP1);
	if (bap_setup(ai, txFid, 0x0014, BAP1) != SUCCESS) return ERROR;
4311 4312
	bap_write(ai, (__le16 *)pPacket, hdrlen, BAP1);
	bap_write(ai, (__le16 *)(tail + (hdrlen - 10)), 38 - hdrlen, BAP1);
L
Linus Torvalds 已提交
4313

4314
	bap_write(ai, (__le16 *)(pPacket + hdrlen), len - hdrlen, BAP1);
L
Linus Torvalds 已提交
4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348
	// issue the transmit command
	memset( &cmd, 0, sizeof( cmd ) );
	cmd.cmd = CMD_TRANSMIT;
	cmd.parm0 = txFid;
	if (issuecommand(ai, &cmd, &rsp) != SUCCESS) return ERROR;
	if ( (rsp.status & 0xFF00) != 0) return ERROR;
	return SUCCESS;
}

/*
 *  This is the proc_fs routines.  It is a bit messier than I would
 *  like!  Feel free to clean it up!
 */

static ssize_t proc_read( struct file *file,
			  char __user *buffer,
			  size_t len,
			  loff_t *offset);

static ssize_t proc_write( struct file *file,
			   const char __user *buffer,
			   size_t len,
			   loff_t *offset );
static int proc_close( struct inode *inode, struct file *file );

static int proc_stats_open( struct inode *inode, struct file *file );
static int proc_statsdelta_open( struct inode *inode, struct file *file );
static int proc_status_open( struct inode *inode, struct file *file );
static int proc_SSID_open( struct inode *inode, struct file *file );
static int proc_APList_open( struct inode *inode, struct file *file );
static int proc_BSSList_open( struct inode *inode, struct file *file );
static int proc_config_open( struct inode *inode, struct file *file );
static int proc_wepkey_open( struct inode *inode, struct file *file );

4349
static const struct file_operations proc_statsdelta_ops = {
4350
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
4351 4352 4353 4354 4355
	.read		= proc_read,
	.open		= proc_statsdelta_open,
	.release	= proc_close
};

4356
static const struct file_operations proc_stats_ops = {
4357
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
4358 4359 4360 4361 4362
	.read		= proc_read,
	.open		= proc_stats_open,
	.release	= proc_close
};

4363
static const struct file_operations proc_status_ops = {
4364
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
4365 4366 4367 4368 4369
	.read		= proc_read,
	.open		= proc_status_open,
	.release	= proc_close
};

4370
static const struct file_operations proc_SSID_ops = {
4371
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
4372 4373 4374 4375 4376 4377
	.read		= proc_read,
	.write		= proc_write,
	.open		= proc_SSID_open,
	.release	= proc_close
};

4378
static const struct file_operations proc_BSSList_ops = {
4379
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
4380 4381 4382 4383 4384 4385
	.read		= proc_read,
	.write		= proc_write,
	.open		= proc_BSSList_open,
	.release	= proc_close
};

4386
static const struct file_operations proc_APList_ops = {
4387
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
4388 4389 4390 4391 4392 4393
	.read		= proc_read,
	.write		= proc_write,
	.open		= proc_APList_open,
	.release	= proc_close
};

4394
static const struct file_operations proc_config_ops = {
4395
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
4396 4397 4398 4399 4400 4401
	.read		= proc_read,
	.write		= proc_write,
	.open		= proc_config_open,
	.release	= proc_close
};

4402
static const struct file_operations proc_wepkey_ops = {
4403
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429
	.read		= proc_read,
	.write		= proc_write,
	.open		= proc_wepkey_open,
	.release	= proc_close
};

static struct proc_dir_entry *airo_entry;

struct proc_data {
	int release_buffer;
	int readlen;
	char *rbuffer;
	int writelen;
	int maxwritelen;
	char *wbuffer;
	void (*on_close) (struct inode *, struct file *);
};

static int setup_proc_entry( struct net_device *dev,
			     struct airo_info *apriv ) {
	struct proc_dir_entry *entry;
	/* First setup the device directory */
	strcpy(apriv->proc_name,dev->name);
	apriv->proc_entry = create_proc_entry(apriv->proc_name,
					      S_IFDIR|airo_perm,
					      airo_entry);
4430 4431 4432 4433 4434
	if (!apriv->proc_entry)
		goto fail;
	apriv->proc_entry->uid = proc_uid;
	apriv->proc_entry->gid = proc_gid;
	apriv->proc_entry->owner = THIS_MODULE;
L
Linus Torvalds 已提交
4435 4436

	/* Setup the StatsDelta */
4437 4438 4439
	entry = proc_create_data("StatsDelta",
				 S_IFREG | (S_IRUGO&proc_perm),
				 apriv->proc_entry, &proc_statsdelta_ops, dev);
4440 4441 4442 4443
	if (!entry)
		goto fail_stats_delta;
	entry->uid = proc_uid;
	entry->gid = proc_gid;
L
Linus Torvalds 已提交
4444 4445

	/* Setup the Stats */
4446 4447 4448
	entry = proc_create_data("Stats",
				 S_IFREG | (S_IRUGO&proc_perm),
				 apriv->proc_entry, &proc_stats_ops, dev);
4449 4450 4451 4452
	if (!entry)
		goto fail_stats;
	entry->uid = proc_uid;
	entry->gid = proc_gid;
L
Linus Torvalds 已提交
4453 4454

	/* Setup the Status */
4455 4456 4457
	entry = proc_create_data("Status",
				 S_IFREG | (S_IRUGO&proc_perm),
				 apriv->proc_entry, &proc_status_ops, dev);
4458 4459 4460 4461
	if (!entry)
		goto fail_status;
	entry->uid = proc_uid;
	entry->gid = proc_gid;
L
Linus Torvalds 已提交
4462 4463

	/* Setup the Config */
4464 4465 4466
	entry = proc_create_data("Config",
				 S_IFREG | proc_perm,
				 apriv->proc_entry, &proc_config_ops, dev);
4467 4468 4469 4470
	if (!entry)
		goto fail_config;
	entry->uid = proc_uid;
	entry->gid = proc_gid;
L
Linus Torvalds 已提交
4471 4472

	/* Setup the SSID */
4473 4474 4475
	entry = proc_create_data("SSID",
				 S_IFREG | proc_perm,
				 apriv->proc_entry, &proc_SSID_ops, dev);
4476 4477 4478 4479
	if (!entry)
		goto fail_ssid;
	entry->uid = proc_uid;
	entry->gid = proc_gid;
L
Linus Torvalds 已提交
4480 4481

	/* Setup the APList */
4482 4483 4484
	entry = proc_create_data("APList",
				 S_IFREG | proc_perm,
				 apriv->proc_entry, &proc_APList_ops, dev);
4485 4486 4487 4488
	if (!entry)
		goto fail_aplist;
	entry->uid = proc_uid;
	entry->gid = proc_gid;
L
Linus Torvalds 已提交
4489 4490

	/* Setup the BSSList */
4491 4492 4493
	entry = proc_create_data("BSSList",
				 S_IFREG | proc_perm,
				 apriv->proc_entry, &proc_BSSList_ops, dev);
4494 4495
	if (!entry)
		goto fail_bsslist;
L
Linus Torvalds 已提交
4496 4497 4498 4499
	entry->uid = proc_uid;
	entry->gid = proc_gid;

	/* Setup the WepKey */
4500 4501 4502
	entry = proc_create_data("WepKey",
				 S_IFREG | proc_perm,
				 apriv->proc_entry, &proc_wepkey_ops, dev);
4503 4504 4505 4506
	if (!entry)
		goto fail_wepkey;
	entry->uid = proc_uid;
	entry->gid = proc_gid;
L
Linus Torvalds 已提交
4507 4508

	return 0;
4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527

fail_wepkey:
	remove_proc_entry("BSSList", apriv->proc_entry);
fail_bsslist:
	remove_proc_entry("APList", apriv->proc_entry);
fail_aplist:
	remove_proc_entry("SSID", apriv->proc_entry);
fail_ssid:
	remove_proc_entry("Config", apriv->proc_entry);
fail_config:
	remove_proc_entry("Status", apriv->proc_entry);
fail_status:
	remove_proc_entry("Stats", apriv->proc_entry);
fail_stats:
	remove_proc_entry("StatsDelta", apriv->proc_entry);
fail_stats_delta:
	remove_proc_entry(apriv->proc_name, airo_entry);
fail:
	return -ENOMEM;
L
Linus Torvalds 已提交
4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608
}

static int takedown_proc_entry( struct net_device *dev,
				struct airo_info *apriv ) {
	if ( !apriv->proc_entry->namelen ) return 0;
	remove_proc_entry("Stats",apriv->proc_entry);
	remove_proc_entry("StatsDelta",apriv->proc_entry);
	remove_proc_entry("Status",apriv->proc_entry);
	remove_proc_entry("Config",apriv->proc_entry);
	remove_proc_entry("SSID",apriv->proc_entry);
	remove_proc_entry("APList",apriv->proc_entry);
	remove_proc_entry("BSSList",apriv->proc_entry);
	remove_proc_entry("WepKey",apriv->proc_entry);
	remove_proc_entry(apriv->proc_name,airo_entry);
	return 0;
}

/*
 *  What we want from the proc_fs is to be able to efficiently read
 *  and write the configuration.  To do this, we want to read the
 *  configuration when the file is opened and write it when the file is
 *  closed.  So basically we allocate a read buffer at open and fill it
 *  with data, and allocate a write buffer and read it at close.
 */

/*
 *  The read routine is generic, it relies on the preallocated rbuffer
 *  to supply the data.
 */
static ssize_t proc_read( struct file *file,
			  char __user *buffer,
			  size_t len,
			  loff_t *offset )
{
	loff_t pos = *offset;
	struct proc_data *priv = (struct proc_data*)file->private_data;

	if (!priv->rbuffer)
		return -EINVAL;

	if (pos < 0)
		return -EINVAL;
	if (pos >= priv->readlen)
		return 0;
	if (len > priv->readlen - pos)
		len = priv->readlen - pos;
	if (copy_to_user(buffer, priv->rbuffer + pos, len))
		return -EFAULT;
	*offset = pos + len;
	return len;
}

/*
 *  The write routine is generic, it fills in a preallocated rbuffer
 *  to supply the data.
 */
static ssize_t proc_write( struct file *file,
			   const char __user *buffer,
			   size_t len,
			   loff_t *offset )
{
	loff_t pos = *offset;
	struct proc_data *priv = (struct proc_data*)file->private_data;

	if (!priv->wbuffer)
		return -EINVAL;

	if (pos < 0)
		return -EINVAL;
	if (pos >= priv->maxwritelen)
		return 0;
	if (len > priv->maxwritelen - pos)
		len = priv->maxwritelen - pos;
	if (copy_from_user(priv->wbuffer + pos, buffer, len))
		return -EFAULT;
	if ( pos + len > priv->writelen )
		priv->writelen = len + file->f_pos;
	*offset = pos + len;
	return len;
}

A
Al Viro 已提交
4609 4610
static int proc_status_open(struct inode *inode, struct file *file)
{
L
Linus Torvalds 已提交
4611 4612 4613 4614 4615 4616
	struct proc_data *data;
	struct proc_dir_entry *dp = PDE(inode);
	struct net_device *dev = dp->data;
	struct airo_info *apriv = dev->priv;
	CapabilityRid cap_rid;
	StatusRid status_rid;
A
Al Viro 已提交
4617
	u16 mode;
L
Linus Torvalds 已提交
4618 4619
	int i;

4620
	if ((file->private_data = kzalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
L
Linus Torvalds 已提交
4621 4622 4623 4624 4625 4626 4627 4628 4629 4630
		return -ENOMEM;
	data = (struct proc_data *)file->private_data;
	if ((data->rbuffer = kmalloc( 2048, GFP_KERNEL )) == NULL) {
		kfree (file->private_data);
		return -ENOMEM;
	}

	readStatusRid(apriv, &status_rid, 1);
	readCapabilityRid(apriv, &cap_rid, 1);

A
Al Viro 已提交
4631 4632
	mode = le16_to_cpu(status_rid.mode);

L
Linus Torvalds 已提交
4633
        i = sprintf(data->rbuffer, "Status: %s%s%s%s%s%s%s%s%s\n",
A
Al Viro 已提交
4634 4635 4636 4637 4638 4639 4640 4641 4642
                    mode & 1 ? "CFG ": "",
                    mode & 2 ? "ACT ": "",
                    mode & 0x10 ? "SYN ": "",
                    mode & 0x20 ? "LNK ": "",
                    mode & 0x40 ? "LEAP ": "",
                    mode & 0x80 ? "PRIV ": "",
                    mode & 0x100 ? "KEY ": "",
                    mode & 0x200 ? "WEP ": "",
                    mode & 0x8000 ? "ERR ": "");
L
Linus Torvalds 已提交
4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654
	sprintf( data->rbuffer+i, "Mode: %x\n"
		 "Signal Strength: %d\n"
		 "Signal Quality: %d\n"
		 "SSID: %-.*s\n"
		 "AP: %-.16s\n"
		 "Freq: %d\n"
		 "BitRate: %dmbs\n"
		 "Driver Version: %s\n"
		 "Device: %s\nManufacturer: %s\nFirmware Version: %s\n"
		 "Radio type: %x\nCountry: %x\nHardware Version: %x\n"
		 "Software Version: %x\nSoftware Subversion: %x\n"
		 "Boot block version: %x\n",
A
Al Viro 已提交
4655 4656 4657 4658
		 le16_to_cpu(status_rid.mode),
		 le16_to_cpu(status_rid.normalizedSignalStrength),
		 le16_to_cpu(status_rid.signalQuality),
		 le16_to_cpu(status_rid.SSIDlen),
L
Linus Torvalds 已提交
4659 4660
		 status_rid.SSID,
		 status_rid.apName,
A
Al Viro 已提交
4661 4662
		 le16_to_cpu(status_rid.channel),
		 le16_to_cpu(status_rid.currentXmitRate) / 2,
L
Linus Torvalds 已提交
4663 4664 4665 4666
		 version,
		 cap_rid.prodName,
		 cap_rid.manName,
		 cap_rid.prodVer,
4667 4668 4669 4670 4671 4672
		 le16_to_cpu(cap_rid.radioType),
		 le16_to_cpu(cap_rid.country),
		 le16_to_cpu(cap_rid.hardVer),
		 le16_to_cpu(cap_rid.softVer),
		 le16_to_cpu(cap_rid.softSubVer),
		 le16_to_cpu(cap_rid.bootBlockVer));
L
Linus Torvalds 已提交
4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691
	data->readlen = strlen( data->rbuffer );
	return 0;
}

static int proc_stats_rid_open(struct inode*, struct file*, u16);
static int proc_statsdelta_open( struct inode *inode,
				 struct file *file ) {
	if (file->f_mode&FMODE_WRITE) {
		return proc_stats_rid_open(inode, file, RID_STATSDELTACLEAR);
	}
	return proc_stats_rid_open(inode, file, RID_STATSDELTA);
}

static int proc_stats_open( struct inode *inode, struct file *file ) {
	return proc_stats_rid_open(inode, file, RID_STATS);
}

static int proc_stats_rid_open( struct inode *inode,
				struct file *file,
A
Al Viro 已提交
4692 4693
				u16 rid )
{
L
Linus Torvalds 已提交
4694 4695 4696 4697 4698 4699
	struct proc_data *data;
	struct proc_dir_entry *dp = PDE(inode);
	struct net_device *dev = dp->data;
	struct airo_info *apriv = dev->priv;
	StatsRid stats;
	int i, j;
A
Al Viro 已提交
4700 4701
	__le32 *vals = stats.vals;
	int len = le16_to_cpu(stats.len);
L
Linus Torvalds 已提交
4702

4703
	if ((file->private_data = kzalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
L
Linus Torvalds 已提交
4704 4705 4706 4707 4708 4709 4710 4711 4712 4713
		return -ENOMEM;
	data = (struct proc_data *)file->private_data;
	if ((data->rbuffer = kmalloc( 4096, GFP_KERNEL )) == NULL) {
		kfree (file->private_data);
		return -ENOMEM;
	}

	readStatsRid(apriv, &stats, rid, 1);

        j = 0;
A
Al Viro 已提交
4714
	for(i=0; statsLabels[i]!=(char *)-1 && i*4<len; i++) {
L
Linus Torvalds 已提交
4715 4716
		if (!statsLabels[i]) continue;
		if (j+strlen(statsLabels[i])+16>4096) {
4717 4718
			airo_print_warn(apriv->dev->name,
			       "Potentially disasterous buffer overflow averted!");
L
Linus Torvalds 已提交
4719 4720
			break;
		}
A
Al Viro 已提交
4721 4722
		j+=sprintf(data->rbuffer+j, "%s: %u\n", statsLabels[i],
				le32_to_cpu(vals[i]));
L
Linus Torvalds 已提交
4723
	}
A
Al Viro 已提交
4724
	if (i*4 >= len) {
4725
		airo_print_warn(apriv->dev->name, "Got a short rid");
L
Linus Torvalds 已提交
4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748
	}
	data->readlen = j;
	return 0;
}

static int get_dec_u16( char *buffer, int *start, int limit ) {
	u16 value;
	int valid = 0;
	for( value = 0; buffer[*start] >= '0' &&
		     buffer[*start] <= '9' &&
		     *start < limit; (*start)++ ) {
		valid = 1;
		value *= 10;
		value += buffer[*start] - '0';
	}
	if ( !valid ) return -1;
	return value;
}

static int airo_config_commit(struct net_device *dev,
			      struct iw_request_info *info, void *zwrq,
			      char *extra);

A
Al Viro 已提交
4749 4750 4751 4752 4753 4754 4755 4756
static inline int sniffing_mode(struct airo_info *ai)
{
	return le16_to_cpu(ai->config.rmode & RXMODE_MASK) >=
		le16_to_cpu(RXMODE_RFMON);
}

static void proc_config_on_close(struct inode *inode, struct file *file)
{
L
Linus Torvalds 已提交
4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772
	struct proc_data *data = file->private_data;
	struct proc_dir_entry *dp = PDE(inode);
	struct net_device *dev = dp->data;
	struct airo_info *ai = dev->priv;
	char *line;

	if ( !data->writelen ) return;

	readConfigRid(ai, 1);
	set_bit (FLAG_COMMIT, &ai->flags);

	line = data->wbuffer;
	while( line[0] ) {
/*** Mode processing */
		if ( !strncmp( line, "Mode: ", 6 ) ) {
			line += 6;
A
Al Viro 已提交
4773 4774 4775
			if (sniffing_mode(ai))
				set_bit (FLAG_RESET, &ai->flags);
			ai->config.rmode &= ~RXMODE_FULL_MASK;
L
Linus Torvalds 已提交
4776
			clear_bit (FLAG_802_11, &ai->flags);
A
Al Viro 已提交
4777
			ai->config.opmode &= ~MODE_CFG_MASK;
L
Linus Torvalds 已提交
4778 4779
			ai->config.scanMode = SCANMODE_ACTIVE;
			if ( line[0] == 'a' ) {
A
Al Viro 已提交
4780
				ai->config.opmode |= MODE_STA_IBSS;
L
Linus Torvalds 已提交
4781
			} else {
A
Al Viro 已提交
4782
				ai->config.opmode |= MODE_STA_ESS;
L
Linus Torvalds 已提交
4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847
				if ( line[0] == 'r' ) {
					ai->config.rmode |= RXMODE_RFMON | RXMODE_DISABLE_802_3_HEADER;
					ai->config.scanMode = SCANMODE_PASSIVE;
					set_bit (FLAG_802_11, &ai->flags);
				} else if ( line[0] == 'y' ) {
					ai->config.rmode |= RXMODE_RFMON_ANYBSS | RXMODE_DISABLE_802_3_HEADER;
					ai->config.scanMode = SCANMODE_PASSIVE;
					set_bit (FLAG_802_11, &ai->flags);
				} else if ( line[0] == 'l' )
					ai->config.rmode |= RXMODE_LANMON;
			}
			set_bit (FLAG_COMMIT, &ai->flags);
		}

/*** Radio status */
		else if (!strncmp(line,"Radio: ", 7)) {
			line += 7;
			if (!strncmp(line,"off",3)) {
				set_bit (FLAG_RADIO_OFF, &ai->flags);
			} else {
				clear_bit (FLAG_RADIO_OFF, &ai->flags);
			}
		}
/*** NodeName processing */
		else if ( !strncmp( line, "NodeName: ", 10 ) ) {
			int j;

			line += 10;
			memset( ai->config.nodeName, 0, 16 );
/* Do the name, assume a space between the mode and node name */
			for( j = 0; j < 16 && line[j] != '\n'; j++ ) {
				ai->config.nodeName[j] = line[j];
			}
			set_bit (FLAG_COMMIT, &ai->flags);
		}

/*** PowerMode processing */
		else if ( !strncmp( line, "PowerMode: ", 11 ) ) {
			line += 11;
			if ( !strncmp( line, "PSPCAM", 6 ) ) {
				ai->config.powerSaveMode = POWERSAVE_PSPCAM;
				set_bit (FLAG_COMMIT, &ai->flags);
			} else if ( !strncmp( line, "PSP", 3 ) ) {
				ai->config.powerSaveMode = POWERSAVE_PSP;
				set_bit (FLAG_COMMIT, &ai->flags);
			} else {
				ai->config.powerSaveMode = POWERSAVE_CAM;
				set_bit (FLAG_COMMIT, &ai->flags);
			}
		} else if ( !strncmp( line, "DataRates: ", 11 ) ) {
			int v, i = 0, k = 0; /* i is index into line,
						k is index to rates */

			line += 11;
			while((v = get_dec_u16(line, &i, 3))!=-1) {
				ai->config.rates[k++] = (u8)v;
				line += i + 1;
				i = 0;
			}
			set_bit (FLAG_COMMIT, &ai->flags);
		} else if ( !strncmp( line, "Channel: ", 9 ) ) {
			int v, i = 0;
			line += 9;
			v = get_dec_u16(line, &i, i+3);
			if ( v != -1 ) {
A
Al Viro 已提交
4848
				ai->config.channelSet = cpu_to_le16(v);
L
Linus Torvalds 已提交
4849 4850 4851 4852 4853 4854 4855
				set_bit (FLAG_COMMIT, &ai->flags);
			}
		} else if ( !strncmp( line, "XmitPower: ", 11 ) ) {
			int v, i = 0;
			line += 11;
			v = get_dec_u16(line, &i, i+3);
			if ( v != -1 ) {
A
Al Viro 已提交
4856
				ai->config.txPower = cpu_to_le16(v);
L
Linus Torvalds 已提交
4857 4858 4859 4860 4861 4862
				set_bit (FLAG_COMMIT, &ai->flags);
			}
		} else if ( !strncmp( line, "WEP: ", 5 ) ) {
			line += 5;
			switch( line[0] ) {
			case 's':
A
Al Viro 已提交
4863
				ai->config.authType = AUTH_SHAREDKEY;
L
Linus Torvalds 已提交
4864 4865
				break;
			case 'e':
A
Al Viro 已提交
4866
				ai->config.authType = AUTH_ENCRYPT;
L
Linus Torvalds 已提交
4867 4868
				break;
			default:
A
Al Viro 已提交
4869
				ai->config.authType = AUTH_OPEN;
L
Linus Torvalds 已提交
4870 4871 4872 4873 4874 4875 4876 4877 4878
				break;
			}
			set_bit (FLAG_COMMIT, &ai->flags);
		} else if ( !strncmp( line, "LongRetryLimit: ", 16 ) ) {
			int v, i = 0;

			line += 16;
			v = get_dec_u16(line, &i, 3);
			v = (v<0) ? 0 : ((v>255) ? 255 : v);
A
Al Viro 已提交
4879
			ai->config.longRetryLimit = cpu_to_le16(v);
L
Linus Torvalds 已提交
4880 4881 4882 4883 4884 4885 4886
			set_bit (FLAG_COMMIT, &ai->flags);
		} else if ( !strncmp( line, "ShortRetryLimit: ", 17 ) ) {
			int v, i = 0;

			line += 17;
			v = get_dec_u16(line, &i, 3);
			v = (v<0) ? 0 : ((v>255) ? 255 : v);
A
Al Viro 已提交
4887
			ai->config.shortRetryLimit = cpu_to_le16(v);
L
Linus Torvalds 已提交
4888 4889 4890 4891 4892 4893
			set_bit (FLAG_COMMIT, &ai->flags);
		} else if ( !strncmp( line, "RTSThreshold: ", 14 ) ) {
			int v, i = 0;

			line += 14;
			v = get_dec_u16(line, &i, 4);
4894
			v = (v<0) ? 0 : ((v>AIRO_DEF_MTU) ? AIRO_DEF_MTU : v);
A
Al Viro 已提交
4895
			ai->config.rtsThres = cpu_to_le16(v);
L
Linus Torvalds 已提交
4896 4897 4898 4899 4900 4901 4902
			set_bit (FLAG_COMMIT, &ai->flags);
		} else if ( !strncmp( line, "TXMSDULifetime: ", 16 ) ) {
			int v, i = 0;

			line += 16;
			v = get_dec_u16(line, &i, 5);
			v = (v<0) ? 0 : v;
A
Al Viro 已提交
4903
			ai->config.txLifetime = cpu_to_le16(v);
L
Linus Torvalds 已提交
4904 4905 4906 4907 4908 4909 4910
			set_bit (FLAG_COMMIT, &ai->flags);
		} else if ( !strncmp( line, "RXMSDULifetime: ", 16 ) ) {
			int v, i = 0;

			line += 16;
			v = get_dec_u16(line, &i, 5);
			v = (v<0) ? 0 : v;
A
Al Viro 已提交
4911
			ai->config.rxLifetime = cpu_to_le16(v);
L
Linus Torvalds 已提交
4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927
			set_bit (FLAG_COMMIT, &ai->flags);
		} else if ( !strncmp( line, "TXDiversity: ", 13 ) ) {
			ai->config.txDiversity =
				(line[13]=='l') ? 1 :
				((line[13]=='r')? 2: 3);
			set_bit (FLAG_COMMIT, &ai->flags);
		} else if ( !strncmp( line, "RXDiversity: ", 13 ) ) {
			ai->config.rxDiversity =
				(line[13]=='l') ? 1 :
				((line[13]=='r')? 2: 3);
			set_bit (FLAG_COMMIT, &ai->flags);
		} else if ( !strncmp( line, "FragThreshold: ", 15 ) ) {
			int v, i = 0;

			line += 15;
			v = get_dec_u16(line, &i, 4);
4928
			v = (v<256) ? 256 : ((v>AIRO_DEF_MTU) ? AIRO_DEF_MTU : v);
L
Linus Torvalds 已提交
4929
			v = v & 0xfffe; /* Make sure its even */
A
Al Viro 已提交
4930
			ai->config.fragThresh = cpu_to_le16(v);
L
Linus Torvalds 已提交
4931 4932 4933 4934 4935 4936 4937
			set_bit (FLAG_COMMIT, &ai->flags);
		} else if (!strncmp(line, "Modulation: ", 12)) {
			line += 12;
			switch(*line) {
			case 'd':  ai->config.modulation=MOD_DEFAULT; set_bit(FLAG_COMMIT, &ai->flags); break;
			case 'c':  ai->config.modulation=MOD_CCK; set_bit(FLAG_COMMIT, &ai->flags); break;
			case 'm':  ai->config.modulation=MOD_MOK; set_bit(FLAG_COMMIT, &ai->flags); break;
4938
			default: airo_print_warn(ai->dev->name, "Unknown modulation");
L
Linus Torvalds 已提交
4939 4940 4941 4942 4943 4944 4945
			}
		} else if (!strncmp(line, "Preamble: ", 10)) {
			line += 10;
			switch(*line) {
			case 'a': ai->config.preamble=PREAMBLE_AUTO; set_bit(FLAG_COMMIT, &ai->flags); break;
			case 'l': ai->config.preamble=PREAMBLE_LONG; set_bit(FLAG_COMMIT, &ai->flags); break;
			case 's': ai->config.preamble=PREAMBLE_SHORT; set_bit(FLAG_COMMIT, &ai->flags); break;
4946
			default: airo_print_warn(ai->dev->name, "Unknown preamble");
L
Linus Torvalds 已提交
4947 4948
			}
		} else {
4949
			airo_print_warn(ai->dev->name, "Couldn't figure out %s", line);
L
Linus Torvalds 已提交
4950 4951 4952 4953 4954 4955 4956
		}
		while( line[0] && line[0] != '\n' ) line++;
		if ( line[0] ) line++;
	}
	airo_config_commit(dev, NULL, NULL, NULL);
}

A
Al Viro 已提交
4957 4958 4959
static char *get_rmode(__le16 mode)
{
        switch(mode & RXMODE_MASK) {
L
Linus Torvalds 已提交
4960 4961 4962 4963 4964 4965 4966
        case RXMODE_RFMON:  return "rfmon";
        case RXMODE_RFMON_ANYBSS:  return "yna (any) bss rfmon";
        case RXMODE_LANMON:  return "lanmon";
        }
        return "ESS";
}

A
Al Viro 已提交
4967 4968
static int proc_config_open(struct inode *inode, struct file *file)
{
L
Linus Torvalds 已提交
4969 4970 4971 4972 4973
	struct proc_data *data;
	struct proc_dir_entry *dp = PDE(inode);
	struct net_device *dev = dp->data;
	struct airo_info *ai = dev->priv;
	int i;
A
Al Viro 已提交
4974
	__le16 mode;
L
Linus Torvalds 已提交
4975

4976
	if ((file->private_data = kzalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
L
Linus Torvalds 已提交
4977 4978 4979 4980 4981 4982
		return -ENOMEM;
	data = (struct proc_data *)file->private_data;
	if ((data->rbuffer = kmalloc( 2048, GFP_KERNEL )) == NULL) {
		kfree (file->private_data);
		return -ENOMEM;
	}
4983
	if ((data->wbuffer = kzalloc( 2048, GFP_KERNEL )) == NULL) {
L
Linus Torvalds 已提交
4984 4985 4986 4987 4988 4989 4990 4991 4992
		kfree (data->rbuffer);
		kfree (file->private_data);
		return -ENOMEM;
	}
	data->maxwritelen = 2048;
	data->on_close = proc_config_on_close;

	readConfigRid(ai, 1);

A
Al Viro 已提交
4993
	mode = ai->config.opmode & MODE_CFG_MASK;
L
Linus Torvalds 已提交
4994 4995 4996 4997 4998 4999 5000 5001
	i = sprintf( data->rbuffer,
		     "Mode: %s\n"
		     "Radio: %s\n"
		     "NodeName: %-16s\n"
		     "PowerMode: %s\n"
		     "DataRates: %d %d %d %d %d %d %d %d\n"
		     "Channel: %d\n"
		     "XmitPower: %d\n",
A
Al Viro 已提交
5002 5003 5004 5005
		     mode == MODE_STA_IBSS ? "adhoc" :
		     mode == MODE_STA_ESS ? get_rmode(ai->config.rmode):
		     mode == MODE_AP ? "AP" :
		     mode == MODE_AP_RPTR ? "AP RPTR" : "Error",
L
Linus Torvalds 已提交
5006 5007
		     test_bit(FLAG_RADIO_OFF, &ai->flags) ? "off" : "on",
		     ai->config.nodeName,
A
Al Viro 已提交
5008 5009 5010 5011
		     ai->config.powerSaveMode == POWERSAVE_CAM ? "CAM" :
		     ai->config.powerSaveMode == POWERSAVE_PSP ? "PSP" :
		     ai->config.powerSaveMode == POWERSAVE_PSPCAM ? "PSPCAM" :
		     "Error",
L
Linus Torvalds 已提交
5012 5013 5014 5015 5016 5017 5018 5019
		     (int)ai->config.rates[0],
		     (int)ai->config.rates[1],
		     (int)ai->config.rates[2],
		     (int)ai->config.rates[3],
		     (int)ai->config.rates[4],
		     (int)ai->config.rates[5],
		     (int)ai->config.rates[6],
		     (int)ai->config.rates[7],
A
Al Viro 已提交
5020 5021
		     le16_to_cpu(ai->config.channelSet),
		     le16_to_cpu(ai->config.txPower)
L
Linus Torvalds 已提交
5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034
		);
	sprintf( data->rbuffer + i,
		 "LongRetryLimit: %d\n"
		 "ShortRetryLimit: %d\n"
		 "RTSThreshold: %d\n"
		 "TXMSDULifetime: %d\n"
		 "RXMSDULifetime: %d\n"
		 "TXDiversity: %s\n"
		 "RXDiversity: %s\n"
		 "FragThreshold: %d\n"
		 "WEP: %s\n"
		 "Modulation: %s\n"
		 "Preamble: %s\n",
A
Al Viro 已提交
5035 5036 5037 5038 5039
		 le16_to_cpu(ai->config.longRetryLimit),
		 le16_to_cpu(ai->config.shortRetryLimit),
		 le16_to_cpu(ai->config.rtsThres),
		 le16_to_cpu(ai->config.txLifetime),
		 le16_to_cpu(ai->config.rxLifetime),
L
Linus Torvalds 已提交
5040 5041 5042 5043
		 ai->config.txDiversity == 1 ? "left" :
		 ai->config.txDiversity == 2 ? "right" : "both",
		 ai->config.rxDiversity == 1 ? "left" :
		 ai->config.rxDiversity == 2 ? "right" : "both",
A
Al Viro 已提交
5044
		 le16_to_cpu(ai->config.fragThresh),
L
Linus Torvalds 已提交
5045 5046
		 ai->config.authType == AUTH_ENCRYPT ? "encrypt" :
		 ai->config.authType == AUTH_SHAREDKEY ? "shared" : "open",
A
Al Viro 已提交
5047
		 ai->config.modulation == MOD_DEFAULT ? "default" :
L
Linus Torvalds 已提交
5048 5049 5050 5051 5052 5053 5054 5055 5056 5057
		 ai->config.modulation == MOD_CCK ? "cck" :
		 ai->config.modulation == MOD_MOK ? "mok" : "error",
		 ai->config.preamble == PREAMBLE_AUTO ? "auto" :
		 ai->config.preamble == PREAMBLE_LONG ? "long" :
		 ai->config.preamble == PREAMBLE_SHORT ? "short" : "error"
		);
	data->readlen = strlen( data->rbuffer );
	return 0;
}

A
Al Viro 已提交
5058 5059
static void proc_SSID_on_close(struct inode *inode, struct file *file)
{
L
Linus Torvalds 已提交
5060 5061 5062 5063 5064 5065
	struct proc_data *data = (struct proc_data *)file->private_data;
	struct proc_dir_entry *dp = PDE(inode);
	struct net_device *dev = dp->data;
	struct airo_info *ai = dev->priv;
	SsidRid SSID_rid;
	int i;
A
Al Viro 已提交
5066 5067
	char *p = data->wbuffer;
	char *end = p + data->writelen;
L
Linus Torvalds 已提交
5068

A
Al Viro 已提交
5069 5070
	if (!data->writelen)
		return;
L
Linus Torvalds 已提交
5071

A
Al Viro 已提交
5072
	*end = '\n'; /* sentinel; we have space for it */
L
Linus Torvalds 已提交
5073

A
Al Viro 已提交
5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086
	memset(&SSID_rid, 0, sizeof(SSID_rid));

	for (i = 0; i < 3 && p < end; i++) {
		int j = 0;
		/* copy up to 32 characters from this line */
		while (*p != '\n' && j < 32)
			SSID_rid.ssids[i].ssid[j++] = *p++;
		if (j == 0)
			break;
		SSID_rid.ssids[i].len = cpu_to_le16(j);
		/* skip to the beginning of the next line */
		while (*p++ != '\n')
			;
L
Linus Torvalds 已提交
5087 5088
	}
	if (i)
A
Al Viro 已提交
5089
		SSID_rid.len = cpu_to_le16(sizeof(SSID_rid));
L
Linus Torvalds 已提交
5090 5091
	disable_MAC(ai, 1);
	writeSsidRid(ai, &SSID_rid, 1);
5092
	enable_MAC(ai, 1);
L
Linus Torvalds 已提交
5093 5094
}

5095
static inline u8 hexVal(char c) {
L
Linus Torvalds 已提交
5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112
	if (c>='0' && c<='9') return c -= '0';
	if (c>='a' && c<='f') return c -= 'a'-10;
	if (c>='A' && c<='F') return c -= 'A'-10;
	return 0;
}

static void proc_APList_on_close( struct inode *inode, struct file *file ) {
	struct proc_data *data = (struct proc_data *)file->private_data;
	struct proc_dir_entry *dp = PDE(inode);
	struct net_device *dev = dp->data;
	struct airo_info *ai = dev->priv;
	APListRid APList_rid;
	int i;

	if ( !data->writelen ) return;

	memset( &APList_rid, 0, sizeof(APList_rid) );
A
Al Viro 已提交
5113
	APList_rid.len = cpu_to_le16(sizeof(APList_rid));
L
Linus Torvalds 已提交
5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131

	for( i = 0; i < 4 && data->writelen >= (i+1)*6*3; i++ ) {
		int j;
		for( j = 0; j < 6*3 && data->wbuffer[j+i*6*3]; j++ ) {
			switch(j%3) {
			case 0:
				APList_rid.ap[i][j/3]=
					hexVal(data->wbuffer[j+i*6*3])<<4;
				break;
			case 1:
				APList_rid.ap[i][j/3]|=
					hexVal(data->wbuffer[j+i*6*3]);
				break;
			}
		}
	}
	disable_MAC(ai, 1);
	writeAPListRid(ai, &APList_rid, 1);
5132
	enable_MAC(ai, 1);
L
Linus Torvalds 已提交
5133 5134 5135 5136 5137 5138 5139 5140 5141
}

/* This function wraps PC4500_writerid with a MAC disable */
static int do_writerid( struct airo_info *ai, u16 rid, const void *rid_data,
			int len, int dummy ) {
	int rc;

	disable_MAC(ai, 1);
	rc = PC4500_writerid(ai, rid, rid_data, len, 1);
5142
	enable_MAC(ai, 1);
L
Linus Torvalds 已提交
5143 5144 5145 5146 5147 5148 5149 5150 5151 5152
	return rc;
}

/* Returns the length of the key at the index.  If index == 0xffff
 * the index of the transmit key is returned.  If the key doesn't exist,
 * -1 will be returned.
 */
static int get_wep_key(struct airo_info *ai, u16 index) {
	WepKeyRid wkr;
	int rc;
A
Al Viro 已提交
5153
	__le16 lastindex;
L
Linus Torvalds 已提交
5154 5155 5156 5157

	rc = readWepKeyRid(ai, &wkr, 1, 1);
	if (rc == SUCCESS) do {
		lastindex = wkr.kindex;
A
Al Viro 已提交
5158
		if (wkr.kindex == cpu_to_le16(index)) {
L
Linus Torvalds 已提交
5159 5160 5161
			if (index == 0xffff) {
				return wkr.mac[0];
			}
A
Al Viro 已提交
5162
			return le16_to_cpu(wkr.klen);
L
Linus Torvalds 已提交
5163 5164
		}
		readWepKeyRid(ai, &wkr, 0, 1);
A
Al Viro 已提交
5165
	} while (lastindex != wkr.kindex);
L
Linus Torvalds 已提交
5166 5167 5168 5169
	return -1;
}

static int set_wep_key(struct airo_info *ai, u16 index,
A
Al Viro 已提交
5170 5171
		       const char *key, u16 keylen, int perm, int lock )
{
L
Linus Torvalds 已提交
5172 5173 5174 5175 5176 5177
	static const unsigned char macaddr[ETH_ALEN] = { 0x01, 0, 0, 0, 0, 0 };
	WepKeyRid wkr;

	memset(&wkr, 0, sizeof(wkr));
	if (keylen == 0) {
// We are selecting which key to use
A
Al Viro 已提交
5178 5179
		wkr.len = cpu_to_le16(sizeof(wkr));
		wkr.kindex = cpu_to_le16(0xffff);
L
Linus Torvalds 已提交
5180 5181 5182 5183
		wkr.mac[0] = (char)index;
		if (perm) ai->defindex = (char)index;
	} else {
// We are actually setting the key
A
Al Viro 已提交
5184 5185 5186
		wkr.len = cpu_to_le16(sizeof(wkr));
		wkr.kindex = cpu_to_le16(index);
		wkr.klen = cpu_to_le16(keylen);
L
Linus Torvalds 已提交
5187 5188 5189 5190
		memcpy( wkr.key, key, keylen );
		memcpy( wkr.mac, macaddr, ETH_ALEN );
	}

5191
	if (perm) disable_MAC(ai, lock);
L
Linus Torvalds 已提交
5192
	writeWepKeyRid(ai, &wkr, perm, lock);
5193
	if (perm) enable_MAC(ai, lock);
L
Linus Torvalds 已提交
5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220
	return 0;
}

static void proc_wepkey_on_close( struct inode *inode, struct file *file ) {
	struct proc_data *data;
	struct proc_dir_entry *dp = PDE(inode);
	struct net_device *dev = dp->data;
	struct airo_info *ai = dev->priv;
	int i;
	char key[16];
	u16 index = 0;
	int j = 0;

	memset(key, 0, sizeof(key));

	data = (struct proc_data *)file->private_data;
	if ( !data->writelen ) return;

	if (data->wbuffer[0] >= '0' && data->wbuffer[0] <= '3' &&
	    (data->wbuffer[1] == ' ' || data->wbuffer[1] == '\n')) {
		index = data->wbuffer[0] - '0';
		if (data->wbuffer[1] == '\n') {
			set_wep_key(ai, index, NULL, 0, 1, 1);
			return;
		}
		j = 2;
	} else {
5221
		airo_print_err(ai->dev->name, "WepKey passed invalid key index");
L
Linus Torvalds 已提交
5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237
		return;
	}

	for( i = 0; i < 16*3 && data->wbuffer[i+j]; i++ ) {
		switch(i%3) {
		case 0:
			key[i/3] = hexVal(data->wbuffer[i+j])<<4;
			break;
		case 1:
			key[i/3] |= hexVal(data->wbuffer[i+j]);
			break;
		}
	}
	set_wep_key(ai, index, key, i/3, 1, 1);
}

A
Al Viro 已提交
5238 5239
static int proc_wepkey_open( struct inode *inode, struct file *file )
{
L
Linus Torvalds 已提交
5240 5241 5242 5243 5244 5245
	struct proc_data *data;
	struct proc_dir_entry *dp = PDE(inode);
	struct net_device *dev = dp->data;
	struct airo_info *ai = dev->priv;
	char *ptr;
	WepKeyRid wkr;
A
Al Viro 已提交
5246
	__le16 lastindex;
L
Linus Torvalds 已提交
5247 5248 5249
	int j=0;
	int rc;

5250
	if ((file->private_data = kzalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
L
Linus Torvalds 已提交
5251 5252 5253
		return -ENOMEM;
	memset(&wkr, 0, sizeof(wkr));
	data = (struct proc_data *)file->private_data;
5254
	if ((data->rbuffer = kzalloc( 180, GFP_KERNEL )) == NULL) {
L
Linus Torvalds 已提交
5255 5256 5257 5258 5259
		kfree (file->private_data);
		return -ENOMEM;
	}
	data->writelen = 0;
	data->maxwritelen = 80;
5260
	if ((data->wbuffer = kzalloc( 80, GFP_KERNEL )) == NULL) {
L
Linus Torvalds 已提交
5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271
		kfree (data->rbuffer);
		kfree (file->private_data);
		return -ENOMEM;
	}
	data->on_close = proc_wepkey_on_close;

	ptr = data->rbuffer;
	strcpy(ptr, "No wep keys\n");
	rc = readWepKeyRid(ai, &wkr, 1, 1);
	if (rc == SUCCESS) do {
		lastindex = wkr.kindex;
A
Al Viro 已提交
5272
		if (wkr.kindex == cpu_to_le16(0xffff)) {
L
Linus Torvalds 已提交
5273 5274 5275 5276
			j += sprintf(ptr+j, "Tx key = %d\n",
				     (int)wkr.mac[0]);
		} else {
			j += sprintf(ptr+j, "Key %d set with length = %d\n",
A
Al Viro 已提交
5277 5278
				     le16_to_cpu(wkr.kindex),
				     le16_to_cpu(wkr.klen));
L
Linus Torvalds 已提交
5279 5280 5281 5282 5283 5284 5285 5286
		}
		readWepKeyRid(ai, &wkr, 0, 1);
	} while((lastindex != wkr.kindex) && (j < 180-30));

	data->readlen = strlen( data->rbuffer );
	return 0;
}

A
Al Viro 已提交
5287 5288
static int proc_SSID_open(struct inode *inode, struct file *file)
{
L
Linus Torvalds 已提交
5289 5290 5291 5292 5293 5294 5295 5296
	struct proc_data *data;
	struct proc_dir_entry *dp = PDE(inode);
	struct net_device *dev = dp->data;
	struct airo_info *ai = dev->priv;
	int i;
	char *ptr;
	SsidRid SSID_rid;

5297
	if ((file->private_data = kzalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
L
Linus Torvalds 已提交
5298 5299 5300 5301 5302 5303 5304 5305
		return -ENOMEM;
	data = (struct proc_data *)file->private_data;
	if ((data->rbuffer = kmalloc( 104, GFP_KERNEL )) == NULL) {
		kfree (file->private_data);
		return -ENOMEM;
	}
	data->writelen = 0;
	data->maxwritelen = 33*3;
A
Al Viro 已提交
5306 5307
	/* allocate maxwritelen + 1; we'll want a sentinel */
	if ((data->wbuffer = kzalloc(33*3 + 1, GFP_KERNEL)) == NULL) {
L
Linus Torvalds 已提交
5308 5309 5310 5311 5312 5313 5314 5315
		kfree (data->rbuffer);
		kfree (file->private_data);
		return -ENOMEM;
	}
	data->on_close = proc_SSID_on_close;

	readSsidRid(ai, &SSID_rid);
	ptr = data->rbuffer;
A
Al Viro 已提交
5316
	for (i = 0; i < 3; i++) {
L
Linus Torvalds 已提交
5317
		int j;
A
Al Viro 已提交
5318 5319 5320 5321 5322 5323
		size_t len = le16_to_cpu(SSID_rid.ssids[i].len);
		if (!len)
			break;
		if (len > 32)
			len = 32;
		for (j = 0; j < len && SSID_rid.ssids[i].ssid[j]; j++)
L
Linus Torvalds 已提交
5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339
			*ptr++ = SSID_rid.ssids[i].ssid[j];
		*ptr++ = '\n';
	}
	*ptr = '\0';
	data->readlen = strlen( data->rbuffer );
	return 0;
}

static int proc_APList_open( struct inode *inode, struct file *file ) {
	struct proc_data *data;
	struct proc_dir_entry *dp = PDE(inode);
	struct net_device *dev = dp->data;
	struct airo_info *ai = dev->priv;
	int i;
	char *ptr;
	APListRid APList_rid;
5340
	DECLARE_MAC_BUF(mac);
L
Linus Torvalds 已提交
5341

5342
	if ((file->private_data = kzalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
L
Linus Torvalds 已提交
5343 5344 5345 5346 5347 5348 5349 5350
		return -ENOMEM;
	data = (struct proc_data *)file->private_data;
	if ((data->rbuffer = kmalloc( 104, GFP_KERNEL )) == NULL) {
		kfree (file->private_data);
		return -ENOMEM;
	}
	data->writelen = 0;
	data->maxwritelen = 4*6*3;
5351
	if ((data->wbuffer = kzalloc( data->maxwritelen, GFP_KERNEL )) == NULL) {
L
Linus Torvalds 已提交
5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363
		kfree (data->rbuffer);
		kfree (file->private_data);
		return -ENOMEM;
	}
	data->on_close = proc_APList_on_close;

	readAPListRid(ai, &APList_rid);
	ptr = data->rbuffer;
	for( i = 0; i < 4; i++ ) {
// We end when we find a zero MAC
		if ( !*(int*)APList_rid.ap[i] &&
		     !*(int*)&APList_rid.ap[i][2]) break;
5364 5365
		ptr += sprintf(ptr, "%s\n",
			       print_mac(mac, APList_rid.ap[i]));
L
Linus Torvalds 已提交
5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383
	}
	if (i==0) ptr += sprintf(ptr, "Not using specific APs\n");

	*ptr = '\0';
	data->readlen = strlen( data->rbuffer );
	return 0;
}

static int proc_BSSList_open( struct inode *inode, struct file *file ) {
	struct proc_data *data;
	struct proc_dir_entry *dp = PDE(inode);
	struct net_device *dev = dp->data;
	struct airo_info *ai = dev->priv;
	char *ptr;
	BSSListRid BSSList_rid;
	int rc;
	/* If doLoseSync is not 1, we won't do a Lose Sync */
	int doLoseSync = -1;
5384
	DECLARE_MAC_BUF(mac);
L
Linus Torvalds 已提交
5385

5386
	if ((file->private_data = kzalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
L
Linus Torvalds 已提交
5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419
		return -ENOMEM;
	data = (struct proc_data *)file->private_data;
	if ((data->rbuffer = kmalloc( 1024, GFP_KERNEL )) == NULL) {
		kfree (file->private_data);
		return -ENOMEM;
	}
	data->writelen = 0;
	data->maxwritelen = 0;
	data->wbuffer = NULL;
	data->on_close = NULL;

	if (file->f_mode & FMODE_WRITE) {
		if (!(file->f_mode & FMODE_READ)) {
			Cmd cmd;
			Resp rsp;

			if (ai->flags & FLAG_RADIO_MASK) return -ENETDOWN;
			memset(&cmd, 0, sizeof(cmd));
			cmd.cmd=CMD_LISTBSS;
			if (down_interruptible(&ai->sem))
				return -ERESTARTSYS;
			issuecommand(ai, &cmd, &rsp);
			up(&ai->sem);
			data->readlen = 0;
			return 0;
		}
		doLoseSync = 1;
	}
	ptr = data->rbuffer;
	/* There is a race condition here if there are concurrent opens.
           Since it is a rare condition, we'll just live with it, otherwise
           we have to add a spin lock... */
	rc = readBSSListRid(ai, doLoseSync, &BSSList_rid);
A
Al Viro 已提交
5420
	while(rc == 0 && BSSList_rid.index != cpu_to_le16(0xffff)) {
5421 5422
		ptr += sprintf(ptr, "%s %*s rssi = %d",
			       print_mac(mac, BSSList_rid.bssid),
L
Linus Torvalds 已提交
5423 5424
				(int)BSSList_rid.ssidLen,
				BSSList_rid.ssid,
A
Al Viro 已提交
5425
				le16_to_cpu(BSSList_rid.dBm));
L
Linus Torvalds 已提交
5426
		ptr += sprintf(ptr, " channel = %d %s %s %s %s\n",
A
Al Viro 已提交
5427
				le16_to_cpu(BSSList_rid.dsChannel),
L
Linus Torvalds 已提交
5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440
				BSSList_rid.cap & CAP_ESS ? "ESS" : "",
				BSSList_rid.cap & CAP_IBSS ? "adhoc" : "",
				BSSList_rid.cap & CAP_PRIVACY ? "wep" : "",
				BSSList_rid.cap & CAP_SHORTHDR ? "shorthdr" : "");
		rc = readBSSListRid(ai, 0, &BSSList_rid);
	}
	*ptr = '\0';
	data->readlen = strlen( data->rbuffer );
	return 0;
}

static int proc_close( struct inode *inode, struct file *file )
{
5441 5442 5443 5444 5445 5446 5447
	struct proc_data *data = file->private_data;

	if (data->on_close != NULL)
		data->on_close(inode, file);
	kfree(data->rbuffer);
	kfree(data->wbuffer);
	kfree(data);
L
Linus Torvalds 已提交
5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483
	return 0;
}

/* Since the card doesn't automatically switch to the right WEP mode,
   we will make it do it.  If the card isn't associated, every secs we
   will switch WEP modes to see if that will help.  If the card is
   associated we will check every minute to see if anything has
   changed. */
static void timer_func( struct net_device *dev ) {
	struct airo_info *apriv = dev->priv;

/* We don't have a link so try changing the authtype */
	readConfigRid(apriv, 0);
	disable_MAC(apriv, 0);
	switch(apriv->config.authType) {
		case AUTH_ENCRYPT:
/* So drop to OPEN */
			apriv->config.authType = AUTH_OPEN;
			break;
		case AUTH_SHAREDKEY:
			if (apriv->keyindex < auto_wep) {
				set_wep_key(apriv, apriv->keyindex, NULL, 0, 0, 0);
				apriv->config.authType = AUTH_SHAREDKEY;
				apriv->keyindex++;
			} else {
			        /* Drop to ENCRYPT */
				apriv->keyindex = 0;
				set_wep_key(apriv, apriv->defindex, NULL, 0, 0, 0);
				apriv->config.authType = AUTH_ENCRYPT;
			}
			break;
		default:  /* We'll escalate to SHAREDKEY */
			apriv->config.authType = AUTH_SHAREDKEY;
	}
	set_bit (FLAG_COMMIT, &apriv->flags);
	writeConfigRid(apriv, 0);
5484
	enable_MAC(apriv, 0);
L
Linus Torvalds 已提交
5485 5486 5487
	up(&apriv->sem);

/* Schedule check to see if the change worked */
5488
	clear_bit(JOB_AUTOWEP, &apriv->jobs);
L
Linus Torvalds 已提交
5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505
	apriv->expires = RUN_AT(HZ*3);
}

#ifdef CONFIG_PCI
static int __devinit airo_pci_probe(struct pci_dev *pdev,
				    const struct pci_device_id *pent)
{
	struct net_device *dev;

	if (pci_enable_device(pdev))
		return -ENODEV;
	pci_set_master(pdev);

	if (pdev->device == 0x5000 || pdev->device == 0xa504)
			dev = _init_airo_card(pdev->irq, pdev->resource[0].start, 0, pdev, &pdev->dev);
	else
			dev = _init_airo_card(pdev->irq, pdev->resource[2].start, 0, pdev, &pdev->dev);
5506 5507
	if (!dev) {
		pci_disable_device(pdev);
L
Linus Torvalds 已提交
5508
		return -ENODEV;
5509
	}
L
Linus Torvalds 已提交
5510 5511 5512 5513 5514 5515 5516

	pci_set_drvdata(pdev, dev);
	return 0;
}

static void __devexit airo_pci_remove(struct pci_dev *pdev)
{
5517 5518 5519 5520
	struct net_device *dev = pci_get_drvdata(pdev);

	airo_print_info(dev->name, "Unregistering...");
	stop_airo_card(dev, 1);
5521 5522
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);
L
Linus Torvalds 已提交
5523 5524
}

5525
static int airo_pci_suspend(struct pci_dev *pdev, pm_message_t state)
L
Linus Torvalds 已提交
5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549
{
	struct net_device *dev = pci_get_drvdata(pdev);
	struct airo_info *ai = dev->priv;
	Cmd cmd;
	Resp rsp;

	if ((ai->APList == NULL) &&
		(ai->APList = kmalloc(sizeof(APListRid), GFP_KERNEL)) == NULL)
		return -ENOMEM;
	if ((ai->SSID == NULL) &&
		(ai->SSID = kmalloc(sizeof(SsidRid), GFP_KERNEL)) == NULL)
		return -ENOMEM;
	readAPListRid(ai, ai->APList);
	readSsidRid(ai, ai->SSID);
	memset(&cmd, 0, sizeof(cmd));
	/* the lock will be released at the end of the resume callback */
	if (down_interruptible(&ai->sem))
		return -EAGAIN;
	disable_MAC(ai, 0);
	netif_device_detach(dev);
	ai->power = state;
	cmd.cmd=HOSTSLEEP;
	issuecommand(ai, &cmd, &rsp);

5550
	pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
L
Linus Torvalds 已提交
5551
	pci_save_state(pdev);
5552
	return pci_set_power_state(pdev, pci_choose_state(pdev, state));
L
Linus Torvalds 已提交
5553 5554 5555 5556 5557 5558
}

static int airo_pci_resume(struct pci_dev *pdev)
{
	struct net_device *dev = pci_get_drvdata(pdev);
	struct airo_info *ai = dev->priv;
M
Michal Schmidt 已提交
5559
	pci_power_t prev_state = pdev->current_state;
L
Linus Torvalds 已提交
5560

M
Michal Schmidt 已提交
5561
	pci_set_power_state(pdev, PCI_D0);
L
Linus Torvalds 已提交
5562
	pci_restore_state(pdev);
M
Michal Schmidt 已提交
5563
	pci_enable_wake(pdev, PCI_D0, 0);
L
Linus Torvalds 已提交
5564

M
Michal Schmidt 已提交
5565
	if (prev_state != PCI_D1) {
L
Linus Torvalds 已提交
5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590
		reset_card(dev, 0);
		mpi_init_descriptors(ai);
		setup_card(ai, dev->dev_addr, 0);
		clear_bit(FLAG_RADIO_OFF, &ai->flags);
		clear_bit(FLAG_PENDING_XMIT, &ai->flags);
	} else {
		OUT4500(ai, EVACK, EV_AWAKEN);
		OUT4500(ai, EVACK, EV_AWAKEN);
		msleep(100);
	}

	set_bit (FLAG_COMMIT, &ai->flags);
	disable_MAC(ai, 0);
        msleep(200);
	if (ai->SSID) {
		writeSsidRid(ai, ai->SSID, 0);
		kfree(ai->SSID);
		ai->SSID = NULL;
	}
	if (ai->APList) {
		writeAPListRid(ai, ai->APList, 0);
		kfree(ai->APList);
		ai->APList = NULL;
	}
	writeConfigRid(ai, 0);
5591
	enable_MAC(ai, 0);
5592
	ai->power = PMSG_ON;
L
Linus Torvalds 已提交
5593 5594 5595 5596 5597 5598 5599 5600 5601 5602
	netif_device_attach(dev);
	netif_wake_queue(dev);
	enable_interrupts(ai);
	up(&ai->sem);
	return 0;
}
#endif

static int __init airo_init_module( void )
{
5603 5604 5605 5606
	int i;
#if 0
	int have_isa_dev = 0;
#endif
L
Linus Torvalds 已提交
5607

A
Alexey Dobriyan 已提交
5608
	airo_entry = create_proc_entry("driver/aironet",
L
Linus Torvalds 已提交
5609
				       S_IFDIR | airo_perm,
A
Alexey Dobriyan 已提交
5610
				       NULL);
5611 5612 5613 5614 5615

	if (airo_entry) {
		airo_entry->uid = proc_uid;
		airo_entry->gid = proc_gid;
	}
L
Linus Torvalds 已提交
5616 5617

	for( i = 0; i < 4 && io[i] && irq[i]; i++ ) {
5618 5619
		airo_print_info("", "Trying to configure ISA adapter at irq=%d "
			"io=0x%x", irq[i], io[i] );
L
Linus Torvalds 已提交
5620
		if (init_airo_card( irq[i], io[i], 0, NULL ))
5621
#if 0
L
Linus Torvalds 已提交
5622
			have_isa_dev = 1;
5623 5624 5625
#else
			/* do nothing */ ;
#endif
L
Linus Torvalds 已提交
5626 5627 5628
	}

#ifdef CONFIG_PCI
5629
	airo_print_info("", "Probing for PCI adapters");
5630
	i = pci_register_driver(&airo_driver);
5631
	airo_print_info("", "Finished probing for PCI adapters");
5632 5633

	if (i) {
A
Alexey Dobriyan 已提交
5634
		remove_proc_entry("driver/aironet", NULL);
5635 5636
		return i;
	}
L
Linus Torvalds 已提交
5637 5638 5639 5640 5641 5642 5643 5644 5645 5646
#endif

	/* Always exit with success, as we are a library module
	 * as well as a driver module
	 */
	return 0;
}

static void __exit airo_cleanup_module( void )
{
5647 5648 5649 5650 5651
	struct airo_info *ai;
	while(!list_empty(&airo_devices)) {
		ai = list_entry(airo_devices.next, struct airo_info, dev_list);
		airo_print_info(ai->dev->name, "Unregistering...");
		stop_airo_card(ai->dev, 1);
L
Linus Torvalds 已提交
5652 5653 5654 5655
	}
#ifdef CONFIG_PCI
	pci_unregister_driver(&airo_driver);
#endif
A
Alexey Dobriyan 已提交
5656
	remove_proc_entry("driver/aironet", NULL);
L
Linus Torvalds 已提交
5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668
}

/*
 * Initial Wireless Extension code for Aironet driver by :
 *	Jean Tourrilhes <jt@hpl.hp.com> - HPL - 17 November 00
 * Conversion to new driver API by :
 *	Jean Tourrilhes <jt@hpl.hp.com> - HPL - 26 March 02
 * Javier also did a good amount of work here, adding some new extensions
 * and fixing my code. Let's just say that without him this code just
 * would not work at all... - Jean II
 */

5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691
static u8 airo_rssi_to_dbm (tdsRssiEntry *rssi_rid, u8 rssi)
{
	if( !rssi_rid )
		return 0;

	return (0x100 - rssi_rid[rssi].rssidBm);
}

static u8 airo_dbm_to_pct (tdsRssiEntry *rssi_rid, u8 dbm)
{
	int i;

	if( !rssi_rid )
		return 0;

	for( i = 0; i < 256; i++ )
		if (rssi_rid[i].rssidBm == dbm)
			return rssi_rid[i].rssipct;

	return 0;
}


L
Linus Torvalds 已提交
5692 5693 5694
static int airo_get_quality (StatusRid *status_rid, CapabilityRid *cap_rid)
{
	int quality = 0;
A
Al Viro 已提交
5695
	u16 sq;
L
Linus Torvalds 已提交
5696

A
Al Viro 已提交
5697
	if ((status_rid->mode & cpu_to_le16(0x3f)) != cpu_to_le16(0x3f))
5698 5699 5700 5701 5702
		return 0;

	if (!(cap_rid->hardCap & cpu_to_le16(8)))
		return 0;

A
Al Viro 已提交
5703
	sq = le16_to_cpu(status_rid->signalQuality);
5704
	if (memcmp(cap_rid->prodName, "350", 3))
A
Al Viro 已提交
5705
		if (sq > 0x20)
5706
			quality = 0;
L
Linus Torvalds 已提交
5707
		else
A
Al Viro 已提交
5708
			quality = 0x20 - sq;
5709
	else
A
Al Viro 已提交
5710
		if (sq > 0xb0)
5711
			quality = 0;
A
Al Viro 已提交
5712
		else if (sq < 0x10)
5713 5714
			quality = 0xa0;
		else
A
Al Viro 已提交
5715
			quality = 0xb0 - sq;
L
Linus Torvalds 已提交
5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765
	return quality;
}

#define airo_get_max_quality(cap_rid) (memcmp((cap_rid)->prodName, "350", 3) ? 0x20 : 0xa0)
#define airo_get_avg_quality(cap_rid) (memcmp((cap_rid)->prodName, "350", 3) ? 0x10 : 0x50);

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get protocol name
 */
static int airo_get_name(struct net_device *dev,
			 struct iw_request_info *info,
			 char *cwrq,
			 char *extra)
{
	strcpy(cwrq, "IEEE 802.11-DS");
	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set frequency
 */
static int airo_set_freq(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_freq *fwrq,
			 char *extra)
{
	struct airo_info *local = dev->priv;
	int rc = -EINPROGRESS;		/* Call commit handler */

	/* If setting by frequency, convert to a channel */
	if((fwrq->e == 1) &&
	   (fwrq->m >= (int) 2.412e8) &&
	   (fwrq->m <= (int) 2.487e8)) {
		int f = fwrq->m / 100000;
		int c = 0;
		while((c < 14) && (f != frequency_list[c]))
			c++;
		/* Hack to fall through... */
		fwrq->e = 0;
		fwrq->m = c + 1;
	}
	/* Setting by channel number */
	if((fwrq->m > 1000) || (fwrq->e > 0))
		rc = -EOPNOTSUPP;
	else {
		int channel = fwrq->m;
		/* We should do a better check than that,
		 * based on the card capability !!! */
J
Javier Achirica 已提交
5766
		if((channel < 1) || (channel > 14)) {
5767 5768
			airo_print_dbg(dev->name, "New channel value of %d is invalid!",
				fwrq->m);
L
Linus Torvalds 已提交
5769 5770 5771 5772
			rc = -EINVAL;
		} else {
			readConfigRid(local, 1);
			/* Yes ! We can set it !!! */
A
Al Viro 已提交
5773
			local->config.channelSet = cpu_to_le16(channel);
L
Linus Torvalds 已提交
5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790
			set_bit (FLAG_COMMIT, &local->flags);
		}
	}
	return rc;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get frequency
 */
static int airo_get_freq(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_freq *fwrq,
			 char *extra)
{
	struct airo_info *local = dev->priv;
	StatusRid status_rid;		/* Card status info */
J
Javier Achirica 已提交
5791
	int ch;
L
Linus Torvalds 已提交
5792 5793

	readConfigRid(local, 1);
A
Al Viro 已提交
5794 5795
	if ((local->config.opmode & MODE_CFG_MASK) == MODE_STA_ESS)
		status_rid.channel = local->config.channelSet;
L
Linus Torvalds 已提交
5796 5797 5798
	else
		readStatusRid(local, &status_rid, 1);

A
Al Viro 已提交
5799
	ch = le16_to_cpu(status_rid.channel);
J
Javier Achirica 已提交
5800 5801
	if((ch > 0) && (ch < 15)) {
		fwrq->m = frequency_list[ch - 1] * 100000;
L
Linus Torvalds 已提交
5802
		fwrq->e = 1;
J
Javier Achirica 已提交
5803 5804 5805
	} else {
		fwrq->m = ch;
		fwrq->e = 0;
L
Linus Torvalds 已提交
5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833
	}

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set ESSID
 */
static int airo_set_essid(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_point *dwrq,
			  char *extra)
{
	struct airo_info *local = dev->priv;
	SsidRid SSID_rid;		/* SSIDs */

	/* Reload the list of current SSID */
	readSsidRid(local, &SSID_rid);

	/* Check if we asked for `any' */
	if(dwrq->flags == 0) {
		/* Just send an empty SSID list */
		memset(&SSID_rid, 0, sizeof(SSID_rid));
	} else {
		int	index = (dwrq->flags & IW_ENCODE_INDEX) - 1;

		/* Check the size of the string */
J
Jean Tourrilhes 已提交
5834
		if(dwrq->length > IW_ESSID_MAX_SIZE) {
L
Linus Torvalds 已提交
5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845
			return -E2BIG ;
		}
		/* Check if index is valid */
		if((index < 0) || (index >= 4)) {
			return -EINVAL;
		}

		/* Set the SSID */
		memset(SSID_rid.ssids[index].ssid, 0,
		       sizeof(SSID_rid.ssids[index].ssid));
		memcpy(SSID_rid.ssids[index].ssid, extra, dwrq->length);
A
Al Viro 已提交
5846
		SSID_rid.ssids[index].len = cpu_to_le16(dwrq->length);
L
Linus Torvalds 已提交
5847
	}
A
Al Viro 已提交
5848
	SSID_rid.len = cpu_to_le16(sizeof(SSID_rid));
L
Linus Torvalds 已提交
5849 5850 5851
	/* Write it to the card */
	disable_MAC(local, 1);
	writeSsidRid(local, &SSID_rid, 1);
5852
	enable_MAC(local, 1);
L
Linus Torvalds 已提交
5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get ESSID
 */
static int airo_get_essid(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_point *dwrq,
			  char *extra)
{
	struct airo_info *local = dev->priv;
	StatusRid status_rid;		/* Card status info */

	readStatusRid(local, &status_rid, 1);

	/* Note : if dwrq->flags != 0, we should
	 * get the relevant SSID from the SSID list... */

	/* Get the current SSID */
A
Al Viro 已提交
5875
	memcpy(extra, status_rid.SSID, le16_to_cpu(status_rid.SSIDlen));
L
Linus Torvalds 已提交
5876 5877 5878
	/* If none, we may want to get the one that was set */

	/* Push it out ! */
A
Al Viro 已提交
5879
	dwrq->length = le16_to_cpu(status_rid.SSIDlen);
L
Linus Torvalds 已提交
5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897
	dwrq->flags = 1; /* active */

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set AP address
 */
static int airo_set_wap(struct net_device *dev,
			struct iw_request_info *info,
			struct sockaddr *awrq,
			char *extra)
{
	struct airo_info *local = dev->priv;
	Cmd cmd;
	Resp rsp;
	APListRid APList_rid;
5898 5899
	static const u8 any[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
	static const u8 off[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
L
Linus Torvalds 已提交
5900 5901 5902

	if (awrq->sa_family != ARPHRD_ETHER)
		return -EINVAL;
5903 5904
	else if (!memcmp(any, awrq->sa_data, ETH_ALEN) ||
	         !memcmp(off, awrq->sa_data, ETH_ALEN)) {
L
Linus Torvalds 已提交
5905 5906 5907 5908 5909 5910 5911 5912
		memset(&cmd, 0, sizeof(cmd));
		cmd.cmd=CMD_LOSE_SYNC;
		if (down_interruptible(&local->sem))
			return -ERESTARTSYS;
		issuecommand(local, &cmd, &rsp);
		up(&local->sem);
	} else {
		memset(&APList_rid, 0, sizeof(APList_rid));
A
Al Viro 已提交
5913
		APList_rid.len = cpu_to_le16(sizeof(APList_rid));
L
Linus Torvalds 已提交
5914 5915 5916
		memcpy(APList_rid.ap[0], awrq->sa_data, ETH_ALEN);
		disable_MAC(local, 1);
		writeAPListRid(local, &APList_rid, 1);
5917
		enable_MAC(local, 1);
L
Linus Torvalds 已提交
5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954
	}
	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get AP address
 */
static int airo_get_wap(struct net_device *dev,
			struct iw_request_info *info,
			struct sockaddr *awrq,
			char *extra)
{
	struct airo_info *local = dev->priv;
	StatusRid status_rid;		/* Card status info */

	readStatusRid(local, &status_rid, 1);

	/* Tentative. This seems to work, wow, I'm lucky !!! */
	memcpy(awrq->sa_data, status_rid.bssid[0], ETH_ALEN);
	awrq->sa_family = ARPHRD_ETHER;

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set Nickname
 */
static int airo_set_nick(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_point *dwrq,
			 char *extra)
{
	struct airo_info *local = dev->priv;

	/* Check the size of the string */
J
Jean Tourrilhes 已提交
5955
	if(dwrq->length > 16) {
L
Linus Torvalds 已提交
5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979
		return -E2BIG;
	}
	readConfigRid(local, 1);
	memset(local->config.nodeName, 0, sizeof(local->config.nodeName));
	memcpy(local->config.nodeName, extra, dwrq->length);
	set_bit (FLAG_COMMIT, &local->flags);

	return -EINPROGRESS;		/* Call commit handler */
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get Nickname
 */
static int airo_get_nick(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_point *dwrq,
			 char *extra)
{
	struct airo_info *local = dev->priv;

	readConfigRid(local, 1);
	strncpy(extra, local->config.nodeName, 16);
	extra[16] = '\0';
J
Jean Tourrilhes 已提交
5980
	dwrq->length = strlen(extra);
L
Linus Torvalds 已提交
5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set Bit-Rate
 */
static int airo_set_rate(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_param *vwrq,
			 char *extra)
{
	struct airo_info *local = dev->priv;
	CapabilityRid cap_rid;		/* Card capability info */
	u8	brate = 0;
	int	i;

	/* First : get a valid bit rate value */
	readCapabilityRid(local, &cap_rid, 1);

	/* Which type of value ? */
	if((vwrq->value < 8) && (vwrq->value >= 0)) {
		/* Setting by rate index */
		/* Find value in the magic rate table */
		brate = cap_rid.supportedRates[vwrq->value];
	} else {
		/* Setting by frequency value */
		u8	normvalue = (u8) (vwrq->value/500000);

		/* Check if rate is valid */
		for(i = 0 ; i < 8 ; i++) {
			if(normvalue == cap_rid.supportedRates[i]) {
				brate = normvalue;
				break;
			}
		}
	}
	/* -1 designed the max rate (mostly auto mode) */
	if(vwrq->value == -1) {
		/* Get the highest available rate */
		for(i = 0 ; i < 8 ; i++) {
			if(cap_rid.supportedRates[i] == 0)
				break;
		}
		if(i != 0)
			brate = cap_rid.supportedRates[i - 1];
	}
	/* Check that it is valid */
	if(brate == 0) {
		return -EINVAL;
	}

	readConfigRid(local, 1);
	/* Now, check if we want a fixed or auto value */
	if(vwrq->fixed == 0) {
		/* Fill all the rates up to this max rate */
		memset(local->config.rates, 0, 8);
		for(i = 0 ; i < 8 ; i++) {
			local->config.rates[i] = cap_rid.supportedRates[i];
			if(local->config.rates[i] == brate)
				break;
		}
	} else {
		/* Fixed mode */
		/* One rate, fixed */
		memset(local->config.rates, 0, 8);
		local->config.rates[0] = brate;
	}
	set_bit (FLAG_COMMIT, &local->flags);

	return -EINPROGRESS;		/* Call commit handler */
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get Bit-Rate
 */
static int airo_get_rate(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_param *vwrq,
			 char *extra)
{
	struct airo_info *local = dev->priv;
	StatusRid status_rid;		/* Card status info */

	readStatusRid(local, &status_rid, 1);

A
Al Viro 已提交
6069
	vwrq->value = le16_to_cpu(status_rid.currentXmitRate) * 500000;
L
Linus Torvalds 已提交
6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089
	/* If more than one rate, set auto */
	readConfigRid(local, 1);
	vwrq->fixed = (local->config.rates[1] == 0);

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set RTS threshold
 */
static int airo_set_rts(struct net_device *dev,
			struct iw_request_info *info,
			struct iw_param *vwrq,
			char *extra)
{
	struct airo_info *local = dev->priv;
	int rthr = vwrq->value;

	if(vwrq->disabled)
6090 6091
		rthr = AIRO_DEF_MTU;
	if((rthr < 0) || (rthr > AIRO_DEF_MTU)) {
L
Linus Torvalds 已提交
6092 6093 6094
		return -EINVAL;
	}
	readConfigRid(local, 1);
A
Al Viro 已提交
6095
	local->config.rtsThres = cpu_to_le16(rthr);
L
Linus Torvalds 已提交
6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112
	set_bit (FLAG_COMMIT, &local->flags);

	return -EINPROGRESS;		/* Call commit handler */
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get RTS threshold
 */
static int airo_get_rts(struct net_device *dev,
			struct iw_request_info *info,
			struct iw_param *vwrq,
			char *extra)
{
	struct airo_info *local = dev->priv;

	readConfigRid(local, 1);
A
Al Viro 已提交
6113
	vwrq->value = le16_to_cpu(local->config.rtsThres);
6114
	vwrq->disabled = (vwrq->value >= AIRO_DEF_MTU);
L
Linus Torvalds 已提交
6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132
	vwrq->fixed = 1;

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set Fragmentation threshold
 */
static int airo_set_frag(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_param *vwrq,
			 char *extra)
{
	struct airo_info *local = dev->priv;
	int fthr = vwrq->value;

	if(vwrq->disabled)
6133 6134
		fthr = AIRO_DEF_MTU;
	if((fthr < 256) || (fthr > AIRO_DEF_MTU)) {
L
Linus Torvalds 已提交
6135 6136 6137 6138
		return -EINVAL;
	}
	fthr &= ~0x1;	/* Get an even value - is it really needed ??? */
	readConfigRid(local, 1);
A
Al Viro 已提交
6139
	local->config.fragThresh = cpu_to_le16(fthr);
L
Linus Torvalds 已提交
6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156
	set_bit (FLAG_COMMIT, &local->flags);

	return -EINPROGRESS;		/* Call commit handler */
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get Fragmentation threshold
 */
static int airo_get_frag(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_param *vwrq,
			 char *extra)
{
	struct airo_info *local = dev->priv;

	readConfigRid(local, 1);
A
Al Viro 已提交
6157
	vwrq->value = le16_to_cpu(local->config.fragThresh);
6158
	vwrq->disabled = (vwrq->value >= AIRO_DEF_MTU);
L
Linus Torvalds 已提交
6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176
	vwrq->fixed = 1;

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set Mode of Operation
 */
static int airo_set_mode(struct net_device *dev,
			 struct iw_request_info *info,
			 __u32 *uwrq,
			 char *extra)
{
	struct airo_info *local = dev->priv;
	int reset = 0;

	readConfigRid(local, 1);
A
Al Viro 已提交
6177
	if (sniffing_mode(local))
L
Linus Torvalds 已提交
6178 6179 6180 6181
		reset = 1;

	switch(*uwrq) {
		case IW_MODE_ADHOC:
A
Al Viro 已提交
6182
			local->config.opmode &= ~MODE_CFG_MASK;
L
Linus Torvalds 已提交
6183
			local->config.opmode |= MODE_STA_IBSS;
A
Al Viro 已提交
6184
			local->config.rmode &= ~RXMODE_FULL_MASK;
L
Linus Torvalds 已提交
6185 6186 6187 6188
			local->config.scanMode = SCANMODE_ACTIVE;
			clear_bit (FLAG_802_11, &local->flags);
			break;
		case IW_MODE_INFRA:
A
Al Viro 已提交
6189
			local->config.opmode &= ~MODE_CFG_MASK;
L
Linus Torvalds 已提交
6190
			local->config.opmode |= MODE_STA_ESS;
A
Al Viro 已提交
6191
			local->config.rmode &= ~RXMODE_FULL_MASK;
L
Linus Torvalds 已提交
6192 6193 6194 6195
			local->config.scanMode = SCANMODE_ACTIVE;
			clear_bit (FLAG_802_11, &local->flags);
			break;
		case IW_MODE_MASTER:
A
Al Viro 已提交
6196
			local->config.opmode &= ~MODE_CFG_MASK;
L
Linus Torvalds 已提交
6197
			local->config.opmode |= MODE_AP;
A
Al Viro 已提交
6198
			local->config.rmode &= ~RXMODE_FULL_MASK;
L
Linus Torvalds 已提交
6199 6200 6201 6202
			local->config.scanMode = SCANMODE_ACTIVE;
			clear_bit (FLAG_802_11, &local->flags);
			break;
		case IW_MODE_REPEAT:
A
Al Viro 已提交
6203
			local->config.opmode &= ~MODE_CFG_MASK;
L
Linus Torvalds 已提交
6204
			local->config.opmode |= MODE_AP_RPTR;
A
Al Viro 已提交
6205
			local->config.rmode &= ~RXMODE_FULL_MASK;
L
Linus Torvalds 已提交
6206 6207 6208 6209
			local->config.scanMode = SCANMODE_ACTIVE;
			clear_bit (FLAG_802_11, &local->flags);
			break;
		case IW_MODE_MONITOR:
A
Al Viro 已提交
6210
			local->config.opmode &= ~MODE_CFG_MASK;
L
Linus Torvalds 已提交
6211
			local->config.opmode |= MODE_STA_ESS;
A
Al Viro 已提交
6212
			local->config.rmode &= ~RXMODE_FULL_MASK;
L
Linus Torvalds 已提交
6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239
			local->config.rmode |= RXMODE_RFMON | RXMODE_DISABLE_802_3_HEADER;
			local->config.scanMode = SCANMODE_PASSIVE;
			set_bit (FLAG_802_11, &local->flags);
			break;
		default:
			return -EINVAL;
	}
	if (reset)
		set_bit (FLAG_RESET, &local->flags);
	set_bit (FLAG_COMMIT, &local->flags);

	return -EINPROGRESS;		/* Call commit handler */
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get Mode of Operation
 */
static int airo_get_mode(struct net_device *dev,
			 struct iw_request_info *info,
			 __u32 *uwrq,
			 char *extra)
{
	struct airo_info *local = dev->priv;

	readConfigRid(local, 1);
	/* If not managed, assume it's ad-hoc */
A
Al Viro 已提交
6240
	switch (local->config.opmode & MODE_CFG_MASK) {
L
Linus Torvalds 已提交
6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256
		case MODE_STA_ESS:
			*uwrq = IW_MODE_INFRA;
			break;
		case MODE_AP:
			*uwrq = IW_MODE_MASTER;
			break;
		case MODE_AP_RPTR:
			*uwrq = IW_MODE_REPEAT;
			break;
		default:
			*uwrq = IW_MODE_ADHOC;
	}

	return 0;
}

6257 6258 6259 6260 6261 6262 6263
static inline int valid_index(CapabilityRid *p, int index)
{
	if (index < 0)
		return 0;
	return index < (p->softCap & cpu_to_le16(0x80) ? 4 : 1);
}

L
Linus Torvalds 已提交
6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274
/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set Encryption Key
 */
static int airo_set_encode(struct net_device *dev,
			   struct iw_request_info *info,
			   struct iw_point *dwrq,
			   char *extra)
{
	struct airo_info *local = dev->priv;
	CapabilityRid cap_rid;		/* Card capability info */
6275
	int perm = ( dwrq->flags & IW_ENCODE_TEMP ? 0 : 1 );
A
Al Viro 已提交
6276
	__le16 currentAuthType = local->config.authType;
L
Linus Torvalds 已提交
6277 6278 6279 6280

	/* Is WEP supported ? */
	readCapabilityRid(local, &cap_rid, 1);
	/* Older firmware doesn't support this...
6281
	if(!(cap_rid.softCap & cpu_to_le16(2))) {
L
Linus Torvalds 已提交
6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300
		return -EOPNOTSUPP;
	} */
	readConfigRid(local, 1);

	/* Basic checking: do we have a key to set ?
	 * Note : with the new API, it's impossible to get a NULL pointer.
	 * Therefore, we need to check a key size == 0 instead.
	 * New version of iwconfig properly set the IW_ENCODE_NOKEY flag
	 * when no key is present (only change flags), but older versions
	 * don't do it. - Jean II */
	if (dwrq->length > 0) {
		wep_key_t key;
		int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
		int current_index = get_wep_key(local, 0xffff);
		/* Check the size of the key */
		if (dwrq->length > MAX_KEY_SIZE) {
			return -EINVAL;
		}
		/* Check the index (none -> use current) */
6301
		if (!valid_index(&cap_rid, index))
L
Linus Torvalds 已提交
6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318
			index = current_index;
		/* Set the length */
		if (dwrq->length > MIN_KEY_SIZE)
			key.len = MAX_KEY_SIZE;
		else
			if (dwrq->length > 0)
				key.len = MIN_KEY_SIZE;
			else
				/* Disable the key */
				key.len = 0;
		/* Check if the key is not marked as invalid */
		if(!(dwrq->flags & IW_ENCODE_NOKEY)) {
			/* Cleanup */
			memset(key.key, 0, MAX_KEY_SIZE);
			/* Copy the key in the driver */
			memcpy(key.key, extra, dwrq->length);
			/* Send the key to the card */
6319
			set_wep_key(local, index, key.key, key.len, perm, 1);
L
Linus Torvalds 已提交
6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330
		}
		/* WE specify that if a valid key is set, encryption
		 * should be enabled (user may turn it off later)
		 * This is also how "iwconfig ethX key on" works */
		if((index == current_index) && (key.len > 0) &&
		   (local->config.authType == AUTH_OPEN)) {
			local->config.authType = AUTH_ENCRYPT;
		}
	} else {
		/* Do we want to just set the transmit key index ? */
		int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
6331
		if (valid_index(&cap_rid, index)) {
6332
			set_wep_key(local, index, NULL, 0, perm, 1);
L
Linus Torvalds 已提交
6333 6334
		} else
			/* Don't complain if only change the mode */
J
Jeff Garzik 已提交
6335
			if (!(dwrq->flags & IW_ENCODE_MODE))
L
Linus Torvalds 已提交
6336 6337 6338 6339 6340 6341 6342 6343 6344 6345
				return -EINVAL;
	}
	/* Read the flags */
	if(dwrq->flags & IW_ENCODE_DISABLED)
		local->config.authType = AUTH_OPEN;	// disable encryption
	if(dwrq->flags & IW_ENCODE_RESTRICTED)
		local->config.authType = AUTH_SHAREDKEY;	// Only Both
	if(dwrq->flags & IW_ENCODE_OPEN)
		local->config.authType = AUTH_ENCRYPT;	// Only Wep
	/* Commit the changes to flags if needed */
6346
	if (local->config.authType != currentAuthType)
L
Linus Torvalds 已提交
6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365
		set_bit (FLAG_COMMIT, &local->flags);
	return -EINPROGRESS;		/* Call commit handler */
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get Encryption Key
 */
static int airo_get_encode(struct net_device *dev,
			   struct iw_request_info *info,
			   struct iw_point *dwrq,
			   char *extra)
{
	struct airo_info *local = dev->priv;
	int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
	CapabilityRid cap_rid;		/* Card capability info */

	/* Is it supported ? */
	readCapabilityRid(local, &cap_rid, 1);
6366
	if(!(cap_rid.softCap & cpu_to_le16(2))) {
L
Linus Torvalds 已提交
6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387
		return -EOPNOTSUPP;
	}
	readConfigRid(local, 1);
	/* Check encryption mode */
	switch(local->config.authType)	{
		case AUTH_ENCRYPT:
			dwrq->flags = IW_ENCODE_OPEN;
			break;
		case AUTH_SHAREDKEY:
			dwrq->flags = IW_ENCODE_RESTRICTED;
			break;
		default:
		case AUTH_OPEN:
			dwrq->flags = IW_ENCODE_DISABLED;
			break;
	}
	/* We can't return the key, so set the proper flag and return zero */
	dwrq->flags |= IW_ENCODE_NOKEY;
	memset(extra, 0, 16);

	/* Which key do we want ? -1 -> tx index */
6388
	if (!valid_index(&cap_rid, index))
L
Linus Torvalds 已提交
6389 6390 6391 6392 6393 6394 6395 6396 6397 6398
		index = get_wep_key(local, 0xffff);
	dwrq->flags |= index + 1;
	/* Copy the key to the user buffer */
	dwrq->length = get_wep_key(local, index);
	if (dwrq->length > 16) {
		dwrq->length=0;
	}
	return 0;
}

6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412
/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set extended Encryption parameters
 */
static int airo_set_encodeext(struct net_device *dev,
			   struct iw_request_info *info,
			    union iwreq_data *wrqu,
			    char *extra)
{
	struct airo_info *local = dev->priv;
	struct iw_point *encoding = &wrqu->encoding;
	struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
	CapabilityRid cap_rid;		/* Card capability info */
	int perm = ( encoding->flags & IW_ENCODE_TEMP ? 0 : 1 );
A
Al Viro 已提交
6413
	__le16 currentAuthType = local->config.authType;
6414
	int idx, key_len, alg = ext->alg, set_key = 1;
6415 6416 6417 6418 6419
	wep_key_t key;

	/* Is WEP supported ? */
	readCapabilityRid(local, &cap_rid, 1);
	/* Older firmware doesn't support this...
6420
	if(!(cap_rid.softCap & cpu_to_le16(2))) {
6421 6422 6423 6424 6425 6426 6427
		return -EOPNOTSUPP;
	} */
	readConfigRid(local, 1);

	/* Determine and validate the key index */
	idx = encoding->flags & IW_ENCODE_INDEX;
	if (idx) {
6428
		if (!valid_index(&cap_rid, idx - 1))
6429 6430 6431 6432 6433 6434 6435 6436 6437
			return -EINVAL;
		idx--;
	} else
		idx = get_wep_key(local, 0xffff);

	if (encoding->flags & IW_ENCODE_DISABLED)
		alg = IW_ENCODE_ALG_NONE;

	if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
6438 6439 6440
		/* Only set transmit key index here, actual
		 * key is set below if needed.
		 */
6441
		set_wep_key(local, idx, NULL, 0, perm, 1);
6442 6443 6444 6445
		set_key = ext->key_len > 0 ? 1 : 0;
	}

	if (set_key) {
6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501
		/* Set the requested key first */
		memset(key.key, 0, MAX_KEY_SIZE);
		switch (alg) {
		case IW_ENCODE_ALG_NONE:
			key.len = 0;
			break;
		case IW_ENCODE_ALG_WEP:
			if (ext->key_len > MIN_KEY_SIZE) {
				key.len = MAX_KEY_SIZE;
			} else if (ext->key_len > 0) {
				key.len = MIN_KEY_SIZE;
			} else {
				return -EINVAL;
			}
			key_len = min (ext->key_len, key.len);
			memcpy(key.key, ext->key, key_len);
			break;
		default:
			return -EINVAL;
		}
		/* Send the key to the card */
		set_wep_key(local, idx, key.key, key.len, perm, 1);
	}

	/* Read the flags */
	if(encoding->flags & IW_ENCODE_DISABLED)
		local->config.authType = AUTH_OPEN;	// disable encryption
	if(encoding->flags & IW_ENCODE_RESTRICTED)
		local->config.authType = AUTH_SHAREDKEY;	// Only Both
	if(encoding->flags & IW_ENCODE_OPEN)
		local->config.authType = AUTH_ENCRYPT;	// Only Wep
	/* Commit the changes to flags if needed */
	if (local->config.authType != currentAuthType)
		set_bit (FLAG_COMMIT, &local->flags);

	return -EINPROGRESS;
}


/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get extended Encryption parameters
 */
static int airo_get_encodeext(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu,
			    char *extra)
{
	struct airo_info *local = dev->priv;
	struct iw_point *encoding = &wrqu->encoding;
	struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
	CapabilityRid cap_rid;		/* Card capability info */
	int idx, max_key_len;

	/* Is it supported ? */
	readCapabilityRid(local, &cap_rid, 1);
6502
	if(!(cap_rid.softCap & cpu_to_le16(2))) {
6503 6504 6505 6506 6507 6508 6509 6510 6511 6512
		return -EOPNOTSUPP;
	}
	readConfigRid(local, 1);

	max_key_len = encoding->length - sizeof(*ext);
	if (max_key_len < 0)
		return -EINVAL;

	idx = encoding->flags & IW_ENCODE_INDEX;
	if (idx) {
6513
		if (!valid_index(&cap_rid, idx - 1))
6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558
			return -EINVAL;
		idx--;
	} else
		idx = get_wep_key(local, 0xffff);

	encoding->flags = idx + 1;
	memset(ext, 0, sizeof(*ext));

	/* Check encryption mode */
	switch(local->config.authType) {
		case AUTH_ENCRYPT:
			encoding->flags = IW_ENCODE_ALG_WEP | IW_ENCODE_ENABLED;
			break;
		case AUTH_SHAREDKEY:
			encoding->flags = IW_ENCODE_ALG_WEP | IW_ENCODE_ENABLED;
			break;
		default:
		case AUTH_OPEN:
			encoding->flags = IW_ENCODE_ALG_NONE | IW_ENCODE_DISABLED;
			break;
	}
	/* We can't return the key, so set the proper flag and return zero */
	encoding->flags |= IW_ENCODE_NOKEY;
	memset(extra, 0, 16);
	
	/* Copy the key to the user buffer */
	ext->key_len = get_wep_key(local, idx);
	if (ext->key_len > 16) {
		ext->key_len=0;
	}

	return 0;
}


/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set extended authentication parameters
 */
static int airo_set_auth(struct net_device *dev,
			       struct iw_request_info *info,
			       union iwreq_data *wrqu, char *extra)
{
	struct airo_info *local = dev->priv;
	struct iw_param *param = &wrqu->param;
A
Al Viro 已提交
6559
	__le16 currentAuthType = local->config.authType;
6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626

	switch (param->flags & IW_AUTH_INDEX) {
	case IW_AUTH_WPA_VERSION:
	case IW_AUTH_CIPHER_PAIRWISE:
	case IW_AUTH_CIPHER_GROUP:
	case IW_AUTH_KEY_MGMT:
	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
	case IW_AUTH_PRIVACY_INVOKED:
		/*
		 * airo does not use these parameters
		 */
		break;

	case IW_AUTH_DROP_UNENCRYPTED:
		if (param->value) {
			/* Only change auth type if unencrypted */
			if (currentAuthType == AUTH_OPEN)
				local->config.authType = AUTH_ENCRYPT;
		} else {
			local->config.authType = AUTH_OPEN;
		}

		/* Commit the changes to flags if needed */
		if (local->config.authType != currentAuthType)
			set_bit (FLAG_COMMIT, &local->flags);
		break;

	case IW_AUTH_80211_AUTH_ALG: {
			/* FIXME: What about AUTH_OPEN?  This API seems to
			 * disallow setting our auth to AUTH_OPEN.
			 */
			if (param->value & IW_AUTH_ALG_SHARED_KEY) {
				local->config.authType = AUTH_SHAREDKEY;
			} else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM) {
				local->config.authType = AUTH_ENCRYPT;
			} else
				return -EINVAL;
			break;

			/* Commit the changes to flags if needed */
			if (local->config.authType != currentAuthType)
				set_bit (FLAG_COMMIT, &local->flags);
		}

	case IW_AUTH_WPA_ENABLED:
		/* Silently accept disable of WPA */
		if (param->value > 0)
			return -EOPNOTSUPP;
		break;

	default:
		return -EOPNOTSUPP;
	}
	return -EINPROGRESS;
}


/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get extended authentication parameters
 */
static int airo_get_auth(struct net_device *dev,
			       struct iw_request_info *info,
			       union iwreq_data *wrqu, char *extra)
{
	struct airo_info *local = dev->priv;
	struct iw_param *param = &wrqu->param;
A
Al Viro 已提交
6627
	__le16 currentAuthType = local->config.authType;
6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664

	switch (param->flags & IW_AUTH_INDEX) {
	case IW_AUTH_DROP_UNENCRYPTED:
		switch (currentAuthType) {
		case AUTH_SHAREDKEY:
		case AUTH_ENCRYPT:
			param->value = 1;
			break;
		default:
			param->value = 0;
			break;
		}
		break;

	case IW_AUTH_80211_AUTH_ALG:
		switch (currentAuthType) {
		case AUTH_SHAREDKEY:
			param->value = IW_AUTH_ALG_SHARED_KEY;
			break;
		case AUTH_ENCRYPT:
		default:
			param->value = IW_AUTH_ALG_OPEN_SYSTEM;
			break;
		}
		break;

	case IW_AUTH_WPA_ENABLED:
		param->value = 0;
		break;

	default:
		return -EOPNOTSUPP;
	}
	return 0;
}


L
Linus Torvalds 已提交
6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677
/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set Tx-Power
 */
static int airo_set_txpow(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_param *vwrq,
			  char *extra)
{
	struct airo_info *local = dev->priv;
	CapabilityRid cap_rid;		/* Card capability info */
	int i;
	int rc = -EINVAL;
A
Al Viro 已提交
6678
	__le16 v = cpu_to_le16(vwrq->value);
L
Linus Torvalds 已提交
6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691

	readCapabilityRid(local, &cap_rid, 1);

	if (vwrq->disabled) {
		set_bit (FLAG_RADIO_OFF, &local->flags);
		set_bit (FLAG_COMMIT, &local->flags);
		return -EINPROGRESS;		/* Call commit handler */
	}
	if (vwrq->flags != IW_TXPOW_MWATT) {
		return -EINVAL;
	}
	clear_bit (FLAG_RADIO_OFF, &local->flags);
	for (i = 0; cap_rid.txPowerLevels[i] && (i < 8); i++)
A
Al Viro 已提交
6692
		if (v == cap_rid.txPowerLevels[i]) {
L
Linus Torvalds 已提交
6693
			readConfigRid(local, 1);
A
Al Viro 已提交
6694
			local->config.txPower = v;
L
Linus Torvalds 已提交
6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713
			set_bit (FLAG_COMMIT, &local->flags);
			rc = -EINPROGRESS;	/* Call commit handler */
			break;
		}
	return rc;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get Tx-Power
 */
static int airo_get_txpow(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_param *vwrq,
			  char *extra)
{
	struct airo_info *local = dev->priv;

	readConfigRid(local, 1);
A
Al Viro 已提交
6714
	vwrq->value = le16_to_cpu(local->config.txPower);
L
Linus Torvalds 已提交
6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738
	vwrq->fixed = 1;	/* No power control */
	vwrq->disabled = test_bit(FLAG_RADIO_OFF, &local->flags);
	vwrq->flags = IW_TXPOW_MWATT;

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set Retry limits
 */
static int airo_set_retry(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_param *vwrq,
			  char *extra)
{
	struct airo_info *local = dev->priv;
	int rc = -EINVAL;

	if(vwrq->disabled) {
		return -EINVAL;
	}
	readConfigRid(local, 1);
	if(vwrq->flags & IW_RETRY_LIMIT) {
A
Al Viro 已提交
6739
		__le16 v = cpu_to_le16(vwrq->value);
J
Jean Tourrilhes 已提交
6740
		if(vwrq->flags & IW_RETRY_LONG)
A
Al Viro 已提交
6741
			local->config.longRetryLimit = v;
J
Jean Tourrilhes 已提交
6742
		else if (vwrq->flags & IW_RETRY_SHORT)
A
Al Viro 已提交
6743
			local->config.shortRetryLimit = v;
L
Linus Torvalds 已提交
6744 6745
		else {
			/* No modifier : set both */
A
Al Viro 已提交
6746 6747
			local->config.longRetryLimit = v;
			local->config.shortRetryLimit = v;
L
Linus Torvalds 已提交
6748 6749 6750 6751 6752
		}
		set_bit (FLAG_COMMIT, &local->flags);
		rc = -EINPROGRESS;		/* Call commit handler */
	}
	if(vwrq->flags & IW_RETRY_LIFETIME) {
A
Al Viro 已提交
6753
		local->config.txLifetime = cpu_to_le16(vwrq->value / 1024);
L
Linus Torvalds 已提交
6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776
		set_bit (FLAG_COMMIT, &local->flags);
		rc = -EINPROGRESS;		/* Call commit handler */
	}
	return rc;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get Retry limits
 */
static int airo_get_retry(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_param *vwrq,
			  char *extra)
{
	struct airo_info *local = dev->priv;

	vwrq->disabled = 0;      /* Can't be disabled */

	readConfigRid(local, 1);
	/* Note : by default, display the min retry number */
	if((vwrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
		vwrq->flags = IW_RETRY_LIFETIME;
A
Al Viro 已提交
6777
		vwrq->value = le16_to_cpu(local->config.txLifetime) * 1024;
J
Jean Tourrilhes 已提交
6778 6779
	} else if((vwrq->flags & IW_RETRY_LONG)) {
		vwrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
A
Al Viro 已提交
6780
		vwrq->value = le16_to_cpu(local->config.longRetryLimit);
L
Linus Torvalds 已提交
6781 6782
	} else {
		vwrq->flags = IW_RETRY_LIMIT;
A
Al Viro 已提交
6783 6784
		vwrq->value = le16_to_cpu(local->config.shortRetryLimit);
		if(local->config.shortRetryLimit != local->config.longRetryLimit)
J
Jean Tourrilhes 已提交
6785
			vwrq->flags |= IW_RETRY_SHORT;
L
Linus Torvalds 已提交
6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822
	}

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get range info
 */
static int airo_get_range(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_point *dwrq,
			  char *extra)
{
	struct airo_info *local = dev->priv;
	struct iw_range *range = (struct iw_range *) extra;
	CapabilityRid cap_rid;		/* Card capability info */
	int		i;
	int		k;

	readCapabilityRid(local, &cap_rid, 1);

	dwrq->length = sizeof(struct iw_range);
	memset(range, 0, sizeof(*range));
	range->min_nwid = 0x0000;
	range->max_nwid = 0x0000;
	range->num_channels = 14;
	/* Should be based on cap_rid.country to give only
	 * what the current card support */
	k = 0;
	for(i = 0; i < 14; i++) {
		range->freq[k].i = i + 1; /* List index */
		range->freq[k].m = frequency_list[i] * 100000;
		range->freq[k++].e = 1;	/* Values in table in MHz -> * 10^5 * 10 */
	}
	range->num_frequency = k;

6823 6824
	range->sensitivity = 65535;

L
Linus Torvalds 已提交
6825
	/* Hum... Should put the right values there */
6826 6827 6828 6829
	if (local->rssi)
		range->max_qual.qual = 100;	/* % */
	else
		range->max_qual.qual = airo_get_max_quality(&cap_rid);
J
Jean Tourrilhes 已提交
6830 6831
	range->max_qual.level = 0x100 - 120;	/* -120 dBm */
	range->max_qual.noise = 0x100 - 120;	/* -120 dBm */
6832 6833 6834 6835 6836

	/* Experimental measurements - boundary 11/5.5 Mb/s */
	/* Note : with or without the (local->rssi), results
	 * are somewhat different. - Jean II */
	if (local->rssi) {
J
Jean Tourrilhes 已提交
6837 6838
		range->avg_qual.qual = 50;		/* % */
		range->avg_qual.level = 0x100 - 70;	/* -70 dBm */
6839 6840
	} else {
		range->avg_qual.qual = airo_get_avg_quality(&cap_rid);
J
Jean Tourrilhes 已提交
6841
		range->avg_qual.level = 0x100 - 80;	/* -80 dBm */
6842
	}
J
Jean Tourrilhes 已提交
6843
	range->avg_qual.noise = 0x100 - 85;		/* -85 dBm */
L
Linus Torvalds 已提交
6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860

	for(i = 0 ; i < 8 ; i++) {
		range->bitrate[i] = cap_rid.supportedRates[i] * 500000;
		if(range->bitrate[i] == 0)
			break;
	}
	range->num_bitrates = i;

	/* Set an indication of the max TCP throughput
	 * in bit/s that we can expect using this interface.
	 * May be use for QoS stuff... Jean II */
	if(i > 2)
		range->throughput = 5000 * 1000;
	else
		range->throughput = 1500 * 1000;

	range->min_rts = 0;
6861
	range->max_rts = AIRO_DEF_MTU;
L
Linus Torvalds 已提交
6862
	range->min_frag = 256;
6863
	range->max_frag = AIRO_DEF_MTU;
L
Linus Torvalds 已提交
6864

6865
	if(cap_rid.softCap & cpu_to_le16(2)) {
L
Linus Torvalds 已提交
6866 6867 6868
		// WEP: RC4 40 bits
		range->encoding_size[0] = 5;
		// RC4 ~128 bits
6869
		if (cap_rid.softCap & cpu_to_le16(0x100)) {
L
Linus Torvalds 已提交
6870 6871 6872 6873
			range->encoding_size[1] = 13;
			range->num_encoding_sizes = 2;
		} else
			range->num_encoding_sizes = 1;
6874 6875
		range->max_encoding_tokens =
			cap_rid.softCap & cpu_to_le16(0x80) ? 4 : 1;
L
Linus Torvalds 已提交
6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889
	} else {
		range->num_encoding_sizes = 0;
		range->max_encoding_tokens = 0;
	}
	range->min_pmp = 0;
	range->max_pmp = 5000000;	/* 5 secs */
	range->min_pmt = 0;
	range->max_pmt = 65535 * 1024;	/* ??? */
	range->pmp_flags = IW_POWER_PERIOD;
	range->pmt_flags = IW_POWER_TIMEOUT;
	range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_ALL_R;

	/* Transmit Power - values are in mW */
	for(i = 0 ; i < 8 ; i++) {
6890
		range->txpower[i] = le16_to_cpu(cap_rid.txPowerLevels[i]);
L
Linus Torvalds 已提交
6891 6892 6893 6894 6895
		if(range->txpower[i] == 0)
			break;
	}
	range->num_txpower = i;
	range->txpower_capa = IW_TXPOW_MWATT;
6896
	range->we_version_source = 19;
L
Linus Torvalds 已提交
6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928
	range->we_version_compiled = WIRELESS_EXT;
	range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
	range->retry_flags = IW_RETRY_LIMIT;
	range->r_time_flags = IW_RETRY_LIFETIME;
	range->min_retry = 1;
	range->max_retry = 65535;
	range->min_r_time = 1024;
	range->max_r_time = 65535 * 1024;

	/* Event capability (kernel + driver) */
	range->event_capa[0] = (IW_EVENT_CAPA_K_0 |
				IW_EVENT_CAPA_MASK(SIOCGIWTHRSPY) |
				IW_EVENT_CAPA_MASK(SIOCGIWAP) |
				IW_EVENT_CAPA_MASK(SIOCGIWSCAN));
	range->event_capa[1] = IW_EVENT_CAPA_K_1;
	range->event_capa[4] = IW_EVENT_CAPA_MASK(IWEVTXDROP);
	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set Power Management
 */
static int airo_set_power(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_param *vwrq,
			  char *extra)
{
	struct airo_info *local = dev->priv;

	readConfigRid(local, 1);
	if (vwrq->disabled) {
A
Al Viro 已提交
6929
		if (sniffing_mode(local))
L
Linus Torvalds 已提交
6930 6931
			return -EINVAL;
		local->config.powerSaveMode = POWERSAVE_CAM;
A
Al Viro 已提交
6932
		local->config.rmode &= ~RXMODE_MASK;
L
Linus Torvalds 已提交
6933 6934 6935 6936 6937
		local->config.rmode |= RXMODE_BC_MC_ADDR;
		set_bit (FLAG_COMMIT, &local->flags);
		return -EINPROGRESS;		/* Call commit handler */
	}
	if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
A
Al Viro 已提交
6938
		local->config.fastListenDelay = cpu_to_le16((vwrq->value + 500) / 1024);
L
Linus Torvalds 已提交
6939 6940 6941
		local->config.powerSaveMode = POWERSAVE_PSPCAM;
		set_bit (FLAG_COMMIT, &local->flags);
	} else if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_PERIOD) {
A
Al Viro 已提交
6942 6943 6944
		local->config.fastListenInterval =
		local->config.listenInterval =
			cpu_to_le16((vwrq->value + 500) / 1024);
L
Linus Torvalds 已提交
6945 6946 6947 6948 6949
		local->config.powerSaveMode = POWERSAVE_PSPCAM;
		set_bit (FLAG_COMMIT, &local->flags);
	}
	switch (vwrq->flags & IW_POWER_MODE) {
		case IW_POWER_UNICAST_R:
A
Al Viro 已提交
6950
			if (sniffing_mode(local))
L
Linus Torvalds 已提交
6951
				return -EINVAL;
A
Al Viro 已提交
6952
			local->config.rmode &= ~RXMODE_MASK;
L
Linus Torvalds 已提交
6953 6954 6955 6956
			local->config.rmode |= RXMODE_ADDR;
			set_bit (FLAG_COMMIT, &local->flags);
			break;
		case IW_POWER_ALL_R:
A
Al Viro 已提交
6957
			if (sniffing_mode(local))
L
Linus Torvalds 已提交
6958
				return -EINVAL;
A
Al Viro 已提交
6959
			local->config.rmode &= ~RXMODE_MASK;
L
Linus Torvalds 已提交
6960 6961 6962
			local->config.rmode |= RXMODE_BC_MC_ADDR;
			set_bit (FLAG_COMMIT, &local->flags);
		case IW_POWER_ON:
J
Jean Tourrilhes 已提交
6963
			/* This is broken, fixme ;-) */
L
Linus Torvalds 已提交
6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982
			break;
		default:
			return -EINVAL;
	}
	// Note : we may want to factor local->need_commit here
	// Note2 : may also want to factor RXMODE_RFMON test
	return -EINPROGRESS;		/* Call commit handler */
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get Power Management
 */
static int airo_get_power(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_param *vwrq,
			  char *extra)
{
	struct airo_info *local = dev->priv;
A
Al Viro 已提交
6983
	__le16 mode;
L
Linus Torvalds 已提交
6984 6985 6986 6987 6988 6989

	readConfigRid(local, 1);
	mode = local->config.powerSaveMode;
	if ((vwrq->disabled = (mode == POWERSAVE_CAM)))
		return 0;
	if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
A
Al Viro 已提交
6990
		vwrq->value = le16_to_cpu(local->config.fastListenDelay) * 1024;
L
Linus Torvalds 已提交
6991 6992
		vwrq->flags = IW_POWER_TIMEOUT;
	} else {
A
Al Viro 已提交
6993
		vwrq->value = le16_to_cpu(local->config.fastListenInterval) * 1024;
L
Linus Torvalds 已提交
6994 6995
		vwrq->flags = IW_POWER_PERIOD;
	}
A
Al Viro 已提交
6996
	if ((local->config.rmode & RXMODE_MASK) == RXMODE_ADDR)
L
Linus Torvalds 已提交
6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015
		vwrq->flags |= IW_POWER_UNICAST_R;
	else
		vwrq->flags |= IW_POWER_ALL_R;

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set Sensitivity
 */
static int airo_set_sens(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_param *vwrq,
			 char *extra)
{
	struct airo_info *local = dev->priv;

	readConfigRid(local, 1);
A
Al Viro 已提交
7016 7017
	local->config.rssiThreshold =
		cpu_to_le16(vwrq->disabled ? RSSI_DEFAULT : vwrq->value);
L
Linus Torvalds 已提交
7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034
	set_bit (FLAG_COMMIT, &local->flags);

	return -EINPROGRESS;		/* Call commit handler */
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get Sensitivity
 */
static int airo_get_sens(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_param *vwrq,
			 char *extra)
{
	struct airo_info *local = dev->priv;

	readConfigRid(local, 1);
A
Al Viro 已提交
7035
	vwrq->value = le16_to_cpu(local->config.rssiThreshold);
L
Linus Torvalds 已提交
7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059
	vwrq->disabled = (vwrq->value == 0);
	vwrq->fixed = 1;

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get AP List
 * Note : this is deprecated in favor of IWSCAN
 */
static int airo_get_aplist(struct net_device *dev,
			   struct iw_request_info *info,
			   struct iw_point *dwrq,
			   char *extra)
{
	struct airo_info *local = dev->priv;
	struct sockaddr *address = (struct sockaddr *) extra;
	struct iw_quality qual[IW_MAX_AP];
	BSSListRid BSSList;
	int i;
	int loseSync = capable(CAP_NET_ADMIN) ? 1: -1;

	for (i = 0; i < IW_MAX_AP; i++) {
A
Al Viro 已提交
7060
		u16 dBm;
L
Linus Torvalds 已提交
7061 7062 7063 7064 7065
		if (readBSSListRid(local, loseSync, &BSSList))
			break;
		loseSync = 0;
		memcpy(address[i].sa_data, BSSList.bssid, ETH_ALEN);
		address[i].sa_family = ARPHRD_ETHER;
A
Al Viro 已提交
7066
		dBm = le16_to_cpu(BSSList.dBm);
7067
		if (local->rssi) {
A
Al Viro 已提交
7068 7069
			qual[i].level = 0x100 - dBm;
			qual[i].qual = airo_dbm_to_pct(local->rssi, dBm);
J
Jean Tourrilhes 已提交
7070 7071 7072
			qual[i].updated = IW_QUAL_QUAL_UPDATED
					| IW_QUAL_LEVEL_UPDATED
					| IW_QUAL_DBM;
7073
		} else {
A
Al Viro 已提交
7074
			qual[i].level = (dBm + 321) / 2;
7075
			qual[i].qual = 0;
J
Jean Tourrilhes 已提交
7076 7077 7078
			qual[i].updated = IW_QUAL_QUAL_INVALID
					| IW_QUAL_LEVEL_UPDATED
					| IW_QUAL_DBM;
7079 7080
		}
		qual[i].noise = local->wstats.qual.noise;
A
Al Viro 已提交
7081
		if (BSSList.index == cpu_to_le16(0xffff))
L
Linus Torvalds 已提交
7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127
			break;
	}
	if (!i) {
		StatusRid status_rid;		/* Card status info */
		readStatusRid(local, &status_rid, 1);
		for (i = 0;
		     i < min(IW_MAX_AP, 4) &&
			     (status_rid.bssid[i][0]
			      & status_rid.bssid[i][1]
			      & status_rid.bssid[i][2]
			      & status_rid.bssid[i][3]
			      & status_rid.bssid[i][4]
			      & status_rid.bssid[i][5])!=0xff &&
			     (status_rid.bssid[i][0]
			      | status_rid.bssid[i][1]
			      | status_rid.bssid[i][2]
			      | status_rid.bssid[i][3]
			      | status_rid.bssid[i][4]
			      | status_rid.bssid[i][5]);
		     i++) {
			memcpy(address[i].sa_data,
			       status_rid.bssid[i], ETH_ALEN);
			address[i].sa_family = ARPHRD_ETHER;
		}
	} else {
		dwrq->flags = 1; /* Should be define'd */
		memcpy(extra + sizeof(struct sockaddr)*i,
		       &qual,  sizeof(struct iw_quality)*i);
	}
	dwrq->length = i;

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : Initiate Scan
 */
static int airo_set_scan(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_param *vwrq,
			 char *extra)
{
	struct airo_info *ai = dev->priv;
	Cmd cmd;
	Resp rsp;
7128
	int wake = 0;
L
Linus Torvalds 已提交
7129 7130 7131 7132 7133 7134 7135 7136 7137

	/* Note : you may have realised that, as this is a SET operation,
	 * this is privileged and therefore a normal user can't
	 * perform scanning.
	 * This is not an error, while the device perform scanning,
	 * traffic doesn't flow, so it's a perfect DoS...
	 * Jean II */
	if (ai->flags & FLAG_RADIO_MASK) return -ENETDOWN;

7138 7139 7140 7141 7142 7143 7144 7145
	if (down_interruptible(&ai->sem))
		return -ERESTARTSYS;

	/* If there's already a scan in progress, don't
	 * trigger another one. */
	if (ai->scan_timeout > 0)
		goto out;

L
Linus Torvalds 已提交
7146
	/* Initiate a scan command */
7147
	ai->scan_timeout = RUN_AT(3*HZ);
L
Linus Torvalds 已提交
7148 7149 7150
	memset(&cmd, 0, sizeof(cmd));
	cmd.cmd=CMD_LISTBSS;
	issuecommand(ai, &cmd, &rsp);
7151
	wake = 1;
L
Linus Torvalds 已提交
7152

7153 7154 7155 7156
out:
	up(&ai->sem);
	if (wake)
		wake_up_interruptible(&ai->thr_wait);
L
Linus Torvalds 已提交
7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167
	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Translate scan data returned from the card to a card independent
 * format that the Wireless Tools will understand - Jean II
 */
static inline char *airo_translate_scan(struct net_device *dev,
					char *current_ev,
					char *end_buf,
7168
					BSSListRid *bss)
L
Linus Torvalds 已提交
7169 7170 7171
{
	struct airo_info *ai = dev->priv;
	struct iw_event		iwe;		/* Temporary buffer */
A
Al Viro 已提交
7172
	__le16			capabilities;
L
Linus Torvalds 已提交
7173 7174
	char *			current_val;	/* For rates */
	int			i;
7175
	char *		buf;
7176
	u16 dBm;
L
Linus Torvalds 已提交
7177 7178 7179 7180

	/* First entry *MUST* be the AP MAC address */
	iwe.cmd = SIOCGIWAP;
	iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
7181
	memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
L
Linus Torvalds 已提交
7182 7183 7184 7185 7186
	current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_ADDR_LEN);

	/* Other entries will be displayed in the order we give them */

	/* Add the ESSID */
7187
	iwe.u.data.length = bss->ssidLen;
L
Linus Torvalds 已提交
7188 7189 7190 7191
	if(iwe.u.data.length > 32)
		iwe.u.data.length = 32;
	iwe.cmd = SIOCGIWESSID;
	iwe.u.data.flags = 1;
7192
	current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, bss->ssid);
L
Linus Torvalds 已提交
7193 7194 7195

	/* Add mode */
	iwe.cmd = SIOCGIWMODE;
A
Al Viro 已提交
7196
	capabilities = bss->cap;
L
Linus Torvalds 已提交
7197 7198 7199 7200 7201 7202 7203 7204 7205 7206
	if(capabilities & (CAP_ESS | CAP_IBSS)) {
		if(capabilities & CAP_ESS)
			iwe.u.mode = IW_MODE_MASTER;
		else
			iwe.u.mode = IW_MODE_ADHOC;
		current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_UINT_LEN);
	}

	/* Add frequency */
	iwe.cmd = SIOCGIWFREQ;
7207
	iwe.u.freq.m = le16_to_cpu(bss->dsChannel);
7208 7209 7210 7211
	/* iwe.u.freq.m containt the channel (starting 1), our 
	 * frequency_list array start at index 0...
	 */
	iwe.u.freq.m = frequency_list[iwe.u.freq.m - 1] * 100000;
L
Linus Torvalds 已提交
7212 7213 7214
	iwe.u.freq.e = 1;
	current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_FREQ_LEN);

7215 7216
	dBm = le16_to_cpu(bss->dBm);

L
Linus Torvalds 已提交
7217 7218
	/* Add quality statistics */
	iwe.cmd = IWEVQUAL;
7219
	if (ai->rssi) {
7220 7221
		iwe.u.qual.level = 0x100 - dBm;
		iwe.u.qual.qual = airo_dbm_to_pct(ai->rssi, dBm);
J
Jean Tourrilhes 已提交
7222 7223 7224
		iwe.u.qual.updated = IW_QUAL_QUAL_UPDATED
				| IW_QUAL_LEVEL_UPDATED
				| IW_QUAL_DBM;
7225
	} else {
7226
		iwe.u.qual.level = (dBm + 321) / 2;
7227
		iwe.u.qual.qual = 0;
7228
		iwe.u.qual.updated = IW_QUAL_QUAL_INVALID
J
Jean Tourrilhes 已提交
7229 7230
				| IW_QUAL_LEVEL_UPDATED
				| IW_QUAL_DBM;
7231 7232
	}
	iwe.u.qual.noise = ai->wstats.qual.noise;
L
Linus Torvalds 已提交
7233 7234 7235 7236 7237 7238 7239 7240 7241
	current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);

	/* Add encryption capability */
	iwe.cmd = SIOCGIWENCODE;
	if(capabilities & CAP_PRIVACY)
		iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
	else
		iwe.u.data.flags = IW_ENCODE_DISABLED;
	iwe.u.data.length = 0;
7242
	current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, bss->ssid);
L
Linus Torvalds 已提交
7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253

	/* Rate : stuffing multiple values in a single event require a bit
	 * more of magic - Jean II */
	current_val = current_ev + IW_EV_LCP_LEN;

	iwe.cmd = SIOCGIWRATE;
	/* Those two flags are ignored... */
	iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
	/* Max 8 values */
	for(i = 0 ; i < 8 ; i++) {
		/* NULL terminated */
7254
		if(bss->rates[i] == 0)
L
Linus Torvalds 已提交
7255 7256
			break;
		/* Bit rate given in 500 kb/s units (+ 0x80) */
7257
		iwe.u.bitrate.value = ((bss->rates[i] & 0x7f) * 500000);
L
Linus Torvalds 已提交
7258 7259 7260 7261 7262 7263 7264
		/* Add new value to event */
		current_val = iwe_stream_add_value(current_ev, current_val, end_buf, &iwe, IW_EV_PARAM_LEN);
	}
	/* Check if we added any event */
	if((current_val - current_ev) > IW_EV_LCP_LEN)
		current_ev = current_val;

7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327
	/* Beacon interval */
	buf = kmalloc(30, GFP_KERNEL);
	if (buf) {
		iwe.cmd = IWEVCUSTOM;
		sprintf(buf, "bcn_int=%d", bss->beaconInterval);
		iwe.u.data.length = strlen(buf);
		current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, buf);
		kfree(buf);
	}

	/* Put WPA/RSN Information Elements into the event stream */
	if (test_bit(FLAG_WPA_CAPABLE, &ai->flags)) {
		unsigned int num_null_ies = 0;
		u16 length = sizeof (bss->extra.iep);
		struct ieee80211_info_element *info_element =
			(struct ieee80211_info_element *) &bss->extra.iep;

		while ((length >= sizeof(*info_element)) && (num_null_ies < 2)) {
			if (sizeof(*info_element) + info_element->len > length) {
				/* Invalid element, don't continue parsing IE */
				break;
			}

			switch (info_element->id) {
			case MFIE_TYPE_SSID:
				/* Two zero-length SSID elements
				 * mean we're done parsing elements */
				if (!info_element->len)
					num_null_ies++;
				break;

			case MFIE_TYPE_GENERIC:
				if (info_element->len >= 4 &&
				    info_element->data[0] == 0x00 &&
				    info_element->data[1] == 0x50 &&
				    info_element->data[2] == 0xf2 &&
				    info_element->data[3] == 0x01) {
					iwe.cmd = IWEVGENIE;
					iwe.u.data.length = min(info_element->len + 2,
								  MAX_WPA_IE_LEN);
					current_ev = iwe_stream_add_point(current_ev, end_buf,
							&iwe, (char *) info_element);
				}
				break;

			case MFIE_TYPE_RSN:
				iwe.cmd = IWEVGENIE;
				iwe.u.data.length = min(info_element->len + 2,
							  MAX_WPA_IE_LEN);
				current_ev = iwe_stream_add_point(current_ev, end_buf,
						&iwe, (char *) info_element);
				break;

			default:
				break;
			}

			length -= sizeof(*info_element) + info_element->len;
			info_element =
			    (struct ieee80211_info_element *)&info_element->
			    data[info_element->len];
		}
	}
L
Linus Torvalds 已提交
7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340
	return current_ev;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : Read Scan Results
 */
static int airo_get_scan(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_point *dwrq,
			 char *extra)
{
	struct airo_info *ai = dev->priv;
7341 7342
	BSSListElement *net;
	int err = 0;
L
Linus Torvalds 已提交
7343 7344
	char *current_ev = extra;

7345 7346
	/* If a scan is in-progress, return -EAGAIN */
	if (ai->scan_timeout > 0)
L
Linus Torvalds 已提交
7347 7348
		return -EAGAIN;

7349 7350
	if (down_interruptible(&ai->sem))
		return -EAGAIN;
L
Linus Torvalds 已提交
7351

7352
	list_for_each_entry (net, &ai->network_list, list) {
L
Linus Torvalds 已提交
7353 7354 7355
		/* Translate to WE format this entry */
		current_ev = airo_translate_scan(dev, current_ev,
						 extra + dwrq->length,
7356
						 &net->bss);
L
Linus Torvalds 已提交
7357 7358 7359 7360

		/* Check if there is space for one more entry */
		if((extra + dwrq->length - current_ev) <= IW_EV_ADDR_LEN) {
			/* Ask user space to try again with a bigger buffer */
7361 7362
			err = -E2BIG;
			goto out;
L
Linus Torvalds 已提交
7363 7364
		}
	}
7365

L
Linus Torvalds 已提交
7366 7367 7368 7369
	/* Length of data */
	dwrq->length = (current_ev - extra);
	dwrq->flags = 0;	/* todo */

7370 7371 7372
out:
	up(&ai->sem);
	return err;
L
Linus Torvalds 已提交
7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408
}

/*------------------------------------------------------------------*/
/*
 * Commit handler : called after a bunch of SET operations
 */
static int airo_config_commit(struct net_device *dev,
			      struct iw_request_info *info,	/* NULL */
			      void *zwrq,			/* NULL */
			      char *extra)			/* NULL */
{
	struct airo_info *local = dev->priv;

	if (!test_bit (FLAG_COMMIT, &local->flags))
		return 0;

	/* Some of the "SET" function may have modified some of the
	 * parameters. It's now time to commit them in the card */
	disable_MAC(local, 1);
	if (test_bit (FLAG_RESET, &local->flags)) {
		APListRid APList_rid;
		SsidRid SSID_rid;

		readAPListRid(local, &APList_rid);
		readSsidRid(local, &SSID_rid);
		if (test_bit(FLAG_MPI,&local->flags))
			setup_card(local, dev->dev_addr, 1 );
		else
			reset_airo_card(dev);
		disable_MAC(local, 1);
		writeSsidRid(local, &SSID_rid, 1);
		writeAPListRid(local, &APList_rid, 1);
	}
	if (down_interruptible(&local->sem))
		return -ERESTARTSYS;
	writeConfigRid(local, 0);
7409
	enable_MAC(local, 0);
L
Linus Torvalds 已提交
7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478
	if (test_bit (FLAG_RESET, &local->flags))
		airo_set_promisc(local);
	else
		up(&local->sem);

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Structures to export the Wireless Handlers
 */

static const struct iw_priv_args airo_private_args[] = {
/*{ cmd,         set_args,                            get_args, name } */
  { AIROIOCTL, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | sizeof (aironet_ioctl),
    IW_PRIV_TYPE_BYTE | 2047, "airoioctl" },
  { AIROIDIFC, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | sizeof (aironet_ioctl),
    IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "airoidifc" },
};

static const iw_handler		airo_handler[] =
{
	(iw_handler) airo_config_commit,	/* SIOCSIWCOMMIT */
	(iw_handler) airo_get_name,		/* SIOCGIWNAME */
	(iw_handler) NULL,			/* SIOCSIWNWID */
	(iw_handler) NULL,			/* SIOCGIWNWID */
	(iw_handler) airo_set_freq,		/* SIOCSIWFREQ */
	(iw_handler) airo_get_freq,		/* SIOCGIWFREQ */
	(iw_handler) airo_set_mode,		/* SIOCSIWMODE */
	(iw_handler) airo_get_mode,		/* SIOCGIWMODE */
	(iw_handler) airo_set_sens,		/* SIOCSIWSENS */
	(iw_handler) airo_get_sens,		/* SIOCGIWSENS */
	(iw_handler) NULL,			/* SIOCSIWRANGE */
	(iw_handler) airo_get_range,		/* SIOCGIWRANGE */
	(iw_handler) NULL,			/* SIOCSIWPRIV */
	(iw_handler) NULL,			/* SIOCGIWPRIV */
	(iw_handler) NULL,			/* SIOCSIWSTATS */
	(iw_handler) NULL,			/* SIOCGIWSTATS */
	iw_handler_set_spy,			/* SIOCSIWSPY */
	iw_handler_get_spy,			/* SIOCGIWSPY */
	iw_handler_set_thrspy,			/* SIOCSIWTHRSPY */
	iw_handler_get_thrspy,			/* SIOCGIWTHRSPY */
	(iw_handler) airo_set_wap,		/* SIOCSIWAP */
	(iw_handler) airo_get_wap,		/* SIOCGIWAP */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) airo_get_aplist,		/* SIOCGIWAPLIST */
	(iw_handler) airo_set_scan,		/* SIOCSIWSCAN */
	(iw_handler) airo_get_scan,		/* SIOCGIWSCAN */
	(iw_handler) airo_set_essid,		/* SIOCSIWESSID */
	(iw_handler) airo_get_essid,		/* SIOCGIWESSID */
	(iw_handler) airo_set_nick,		/* SIOCSIWNICKN */
	(iw_handler) airo_get_nick,		/* SIOCGIWNICKN */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) airo_set_rate,		/* SIOCSIWRATE */
	(iw_handler) airo_get_rate,		/* SIOCGIWRATE */
	(iw_handler) airo_set_rts,		/* SIOCSIWRTS */
	(iw_handler) airo_get_rts,		/* SIOCGIWRTS */
	(iw_handler) airo_set_frag,		/* SIOCSIWFRAG */
	(iw_handler) airo_get_frag,		/* SIOCGIWFRAG */
	(iw_handler) airo_set_txpow,		/* SIOCSIWTXPOW */
	(iw_handler) airo_get_txpow,		/* SIOCGIWTXPOW */
	(iw_handler) airo_set_retry,		/* SIOCSIWRETRY */
	(iw_handler) airo_get_retry,		/* SIOCGIWRETRY */
	(iw_handler) airo_set_encode,		/* SIOCSIWENCODE */
	(iw_handler) airo_get_encode,		/* SIOCGIWENCODE */
	(iw_handler) airo_set_power,		/* SIOCSIWPOWER */
	(iw_handler) airo_get_power,		/* SIOCGIWPOWER */
7479 7480 7481 7482 7483 7484 7485 7486 7487
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) NULL,			/* SIOCSIWGENIE */
	(iw_handler) NULL,			/* SIOCGIWGENIE */
	(iw_handler) airo_set_auth,		/* SIOCSIWAUTH */
	(iw_handler) airo_get_auth,		/* SIOCGIWAUTH */
	(iw_handler) airo_set_encodeext,	/* SIOCSIWENCODEEXT */
	(iw_handler) airo_get_encodeext,	/* SIOCGIWENCODEEXT */
	(iw_handler) NULL,			/* SIOCSIWPMKSA */
L
Linus Torvalds 已提交
7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503
};

/* Note : don't describe AIROIDIFC and AIROOLDIDIFC in here.
 * We want to force the use of the ioctl code, because those can't be
 * won't work the iw_handler code (because they simultaneously read
 * and write data and iw_handler can't do that).
 * Note that it's perfectly legal to read/write on a single ioctl command,
 * you just can't use iwpriv and need to force it via the ioctl handler.
 * Jean II */
static const iw_handler		airo_private_handler[] =
{
	NULL,				/* SIOCIWFIRSTPRIV */
};

static const struct iw_handler_def	airo_handler_def =
{
7504 7505 7506
	.num_standard	= ARRAY_SIZE(airo_handler),
	.num_private	= ARRAY_SIZE(airo_private_handler),
	.num_private_args = ARRAY_SIZE(airo_private_args),
L
Linus Torvalds 已提交
7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530
	.standard	= airo_handler,
	.private	= airo_private_handler,
	.private_args	= airo_private_args,
	.get_wireless_stats = airo_get_wireless_stats,
};

/*
 * This defines the configuration part of the Wireless Extensions
 * Note : irq and spinlock protection will occur in the subroutines
 *
 * TODO :
 *	o Check input value more carefully and fill correct values in range
 *	o Test and shakeout the bugs (if any)
 *
 * Jean II
 *
 * Javier Achirica did a great job of merging code from the unnamed CISCO
 * developer that added support for flashing the card.
 */
static int airo_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
	int rc = 0;
	struct airo_info *ai = (struct airo_info *)dev->priv;

7531
	if (ai->power.event)
L
Linus Torvalds 已提交
7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604
		return 0;

	switch (cmd) {
#ifdef CISCO_EXT
	case AIROIDIFC:
#ifdef AIROOLDIDIFC
	case AIROOLDIDIFC:
#endif
	{
		int val = AIROMAGIC;
		aironet_ioctl com;
		if (copy_from_user(&com,rq->ifr_data,sizeof(com)))
			rc = -EFAULT;
		else if (copy_to_user(com.data,(char *)&val,sizeof(val)))
			rc = -EFAULT;
	}
	break;

	case AIROIOCTL:
#ifdef AIROOLDIOCTL
	case AIROOLDIOCTL:
#endif
		/* Get the command struct and hand it off for evaluation by
		 * the proper subfunction
		 */
	{
		aironet_ioctl com;
		if (copy_from_user(&com,rq->ifr_data,sizeof(com))) {
			rc = -EFAULT;
			break;
		}

		/* Separate R/W functions bracket legality here
		 */
		if ( com.command == AIRORSWVERSION ) {
			if (copy_to_user(com.data, swversion, sizeof(swversion)))
				rc = -EFAULT;
			else
				rc = 0;
		}
		else if ( com.command <= AIRORRID)
			rc = readrids(dev,&com);
		else if ( com.command >= AIROPCAP && com.command <= (AIROPLEAPUSR+2) )
			rc = writerids(dev,&com);
		else if ( com.command >= AIROFLSHRST && com.command <= AIRORESTART )
			rc = flashcard(dev,&com);
		else
			rc = -EINVAL;      /* Bad command in ioctl */
	}
	break;
#endif /* CISCO_EXT */

	// All other calls are currently unsupported
	default:
		rc = -EOPNOTSUPP;
	}
	return rc;
}

/*
 * Get the Wireless stats out of the driver
 * Note : irq and spinlock protection will occur in the subroutines
 *
 * TODO :
 *	o Check if work in Ad-Hoc mode (otherwise, use SPY, as in wvlan_cs)
 *
 * Jean
 */
static void airo_read_wireless_stats(struct airo_info *local)
{
	StatusRid status_rid;
	StatsRid stats_rid;
	CapabilityRid cap_rid;
A
Al Viro 已提交
7605
	__le32 *vals = stats_rid.vals;
L
Linus Torvalds 已提交
7606 7607

	/* Get stats out of the card */
7608
	clear_bit(JOB_WSTATS, &local->jobs);
7609
	if (local->power.event) {
L
Linus Torvalds 已提交
7610 7611 7612 7613 7614 7615 7616 7617 7618
		up(&local->sem);
		return;
	}
	readCapabilityRid(local, &cap_rid, 0);
	readStatusRid(local, &status_rid, 0);
	readStatsRid(local, &stats_rid, RID_STATS, 0);
	up(&local->sem);

	/* The status */
A
Al Viro 已提交
7619
	local->wstats.status = le16_to_cpu(status_rid.mode);
L
Linus Torvalds 已提交
7620

7621 7622
	/* Signal quality and co */
	if (local->rssi) {
A
Al Viro 已提交
7623 7624 7625
		local->wstats.qual.level =
			airo_rssi_to_dbm(local->rssi,
					 le16_to_cpu(status_rid.sigQuality));
7626
		/* normalizedSignalStrength appears to be a percentage */
A
Al Viro 已提交
7627 7628
		local->wstats.qual.qual =
			le16_to_cpu(status_rid.normalizedSignalStrength);
7629
	} else {
A
Al Viro 已提交
7630 7631
		local->wstats.qual.level =
			(le16_to_cpu(status_rid.normalizedSignalStrength) + 321) / 2;
7632 7633
		local->wstats.qual.qual = airo_get_quality(&status_rid, &cap_rid);
	}
A
Al Viro 已提交
7634
	if (le16_to_cpu(status_rid.len) >= 124) {
7635
		local->wstats.qual.noise = 0x100 - status_rid.noisedBm;
J
Jean Tourrilhes 已提交
7636
		local->wstats.qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
L
Linus Torvalds 已提交
7637 7638
	} else {
		local->wstats.qual.noise = 0;
J
Jean Tourrilhes 已提交
7639
		local->wstats.qual.updated = IW_QUAL_QUAL_UPDATED | IW_QUAL_LEVEL_UPDATED | IW_QUAL_NOISE_INVALID | IW_QUAL_DBM;
L
Linus Torvalds 已提交
7640 7641 7642 7643
	}

	/* Packets discarded in the wireless adapter due to wireless
	 * specific problems */
A
Al Viro 已提交
7644 7645 7646 7647 7648 7649 7650 7651 7652
	local->wstats.discard.nwid = le32_to_cpu(vals[56]) +
				     le32_to_cpu(vals[57]) +
				     le32_to_cpu(vals[58]); /* SSID Mismatch */
	local->wstats.discard.code = le32_to_cpu(vals[6]);/* RxWepErr */
	local->wstats.discard.fragment = le32_to_cpu(vals[30]);
	local->wstats.discard.retries = le32_to_cpu(vals[10]);
	local->wstats.discard.misc = le32_to_cpu(vals[1]) +
				     le32_to_cpu(vals[32]);
	local->wstats.miss.beacon = le32_to_cpu(vals[34]);
L
Linus Torvalds 已提交
7653 7654
}

J
Jouni Malinen 已提交
7655
static struct iw_statistics *airo_get_wireless_stats(struct net_device *dev)
L
Linus Torvalds 已提交
7656 7657 7658
{
	struct airo_info *local =  dev->priv;

7659
	if (!test_bit(JOB_WSTATS, &local->jobs)) {
L
Linus Torvalds 已提交
7660 7661
		/* Get stats out of the card if available */
		if (down_trylock(&local->sem) != 0) {
7662
			set_bit(JOB_WSTATS, &local->jobs);
L
Linus Torvalds 已提交
7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693
			wake_up_interruptible(&local->thr_wait);
		} else
			airo_read_wireless_stats(local);
	}

	return &local->wstats;
}

#ifdef CISCO_EXT
/*
 * This just translates from driver IOCTL codes to the command codes to
 * feed to the radio's host interface. Things can be added/deleted
 * as needed.  This represents the READ side of control I/O to
 * the card
 */
static int readrids(struct net_device *dev, aironet_ioctl *comp) {
	unsigned short ridcode;
	unsigned char *iobuf;
	int len;
	struct airo_info *ai = dev->priv;

	if (test_bit(FLAG_FLASHING, &ai->flags))
		return -EIO;

	switch(comp->command)
	{
	case AIROGCAP:      ridcode = RID_CAPABILITIES; break;
	case AIROGCFG:      ridcode = RID_CONFIG;
		if (test_bit(FLAG_COMMIT, &ai->flags)) {
			disable_MAC (ai, 1);
			writeConfigRid (ai, 1);
7694
			enable_MAC(ai, 1);
L
Linus Torvalds 已提交
7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783
		}
		break;
	case AIROGSLIST:    ridcode = RID_SSID;         break;
	case AIROGVLIST:    ridcode = RID_APLIST;       break;
	case AIROGDRVNAM:   ridcode = RID_DRVNAME;      break;
	case AIROGEHTENC:   ridcode = RID_ETHERENCAP;   break;
	case AIROGWEPKTMP:  ridcode = RID_WEP_TEMP;
		/* Only super-user can read WEP keys */
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		break;
	case AIROGWEPKNV:   ridcode = RID_WEP_PERM;
		/* Only super-user can read WEP keys */
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		break;
	case AIROGSTAT:     ridcode = RID_STATUS;       break;
	case AIROGSTATSD32: ridcode = RID_STATSDELTA;   break;
	case AIROGSTATSC32: ridcode = RID_STATS;        break;
	case AIROGMICSTATS:
		if (copy_to_user(comp->data, &ai->micstats,
				 min((int)comp->len,(int)sizeof(ai->micstats))))
			return -EFAULT;
		return 0;
	case AIRORRID:      ridcode = comp->ridnum;     break;
	default:
		return -EINVAL;
		break;
	}

	if ((iobuf = kmalloc(RIDSIZE, GFP_KERNEL)) == NULL)
		return -ENOMEM;

	PC4500_readrid(ai,ridcode,iobuf,RIDSIZE, 1);
	/* get the count of bytes in the rid  docs say 1st 2 bytes is it.
	 * then return it to the user
	 * 9/22/2000 Honor user given length
	 */
	len = comp->len;

	if (copy_to_user(comp->data, iobuf, min(len, (int)RIDSIZE))) {
		kfree (iobuf);
		return -EFAULT;
	}
	kfree (iobuf);
	return 0;
}

/*
 * Danger Will Robinson write the rids here
 */

static int writerids(struct net_device *dev, aironet_ioctl *comp) {
	struct airo_info *ai = dev->priv;
	int  ridcode;
        int  enabled;
	static int (* writer)(struct airo_info *, u16 rid, const void *, int, int);
	unsigned char *iobuf;

	/* Only super-user can write RIDs */
	if (!capable(CAP_NET_ADMIN))
		return -EPERM;

	if (test_bit(FLAG_FLASHING, &ai->flags))
		return -EIO;

	ridcode = 0;
	writer = do_writerid;

	switch(comp->command)
	{
	case AIROPSIDS:     ridcode = RID_SSID;         break;
	case AIROPCAP:      ridcode = RID_CAPABILITIES; break;
	case AIROPAPLIST:   ridcode = RID_APLIST;       break;
	case AIROPCFG: ai->config.len = 0;
			    clear_bit(FLAG_COMMIT, &ai->flags);
			    ridcode = RID_CONFIG;       break;
	case AIROPWEPKEYNV: ridcode = RID_WEP_PERM;     break;
	case AIROPLEAPUSR:  ridcode = RID_LEAPUSERNAME; break;
	case AIROPLEAPPWD:  ridcode = RID_LEAPPASSWORD; break;
	case AIROPWEPKEY:   ridcode = RID_WEP_TEMP; writer = PC4500_writerid;
		break;
	case AIROPLEAPUSR+1: ridcode = 0xFF2A;          break;
	case AIROPLEAPUSR+2: ridcode = 0xFF2B;          break;

		/* this is not really a rid but a command given to the card
		 * same with MAC off
		 */
	case AIROPMACON:
7784
		if (enable_MAC(ai, 1) != 0)
L
Linus Torvalds 已提交
7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835
			return -EIO;
		return 0;

		/*
		 * Evidently this code in the airo driver does not get a symbol
		 * as disable_MAC. it's probably so short the compiler does not gen one.
		 */
	case AIROPMACOFF:
		disable_MAC(ai, 1);
		return 0;

		/* This command merely clears the counts does not actually store any data
		 * only reads rid. But as it changes the cards state, I put it in the
		 * writerid routines.
		 */
	case AIROPSTCLR:
		if ((iobuf = kmalloc(RIDSIZE, GFP_KERNEL)) == NULL)
			return -ENOMEM;

		PC4500_readrid(ai,RID_STATSDELTACLEAR,iobuf,RIDSIZE, 1);

		enabled = ai->micstats.enabled;
		memset(&ai->micstats,0,sizeof(ai->micstats));
		ai->micstats.enabled = enabled;

		if (copy_to_user(comp->data, iobuf,
				 min((int)comp->len, (int)RIDSIZE))) {
			kfree (iobuf);
			return -EFAULT;
		}
		kfree (iobuf);
		return 0;

	default:
		return -EOPNOTSUPP;	/* Blarg! */
	}
	if(comp->len > RIDSIZE)
		return -EINVAL;

	if ((iobuf = kmalloc(RIDSIZE, GFP_KERNEL)) == NULL)
		return -ENOMEM;

	if (copy_from_user(iobuf,comp->data,comp->len)) {
		kfree (iobuf);
		return -EFAULT;
	}

	if (comp->command == AIROPCFG) {
		ConfigRid *cfg = (ConfigRid *)iobuf;

		if (test_bit(FLAG_MIC_CAPABLE, &ai->flags))
A
Al Viro 已提交
7836
			cfg->opmode |= MODE_MIC;
L
Linus Torvalds 已提交
7837

A
Al Viro 已提交
7838
		if ((cfg->opmode & MODE_CFG_MASK) == MODE_STA_IBSS)
L
Linus Torvalds 已提交
7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860
			set_bit (FLAG_ADHOC, &ai->flags);
		else
			clear_bit (FLAG_ADHOC, &ai->flags);
	}

	if((*writer)(ai, ridcode, iobuf,comp->len,1)) {
		kfree (iobuf);
		return -EIO;
	}
	kfree (iobuf);
	return 0;
}

/*****************************************************************************
 * Ancillary flash / mod functions much black magic lurkes here              *
 *****************************************************************************
 */

/*
 * Flash command switch table
 */

J
Jouni Malinen 已提交
7861
static int flashcard(struct net_device *dev, aironet_ioctl *comp) {
L
Linus Torvalds 已提交
7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919
	int z;

	/* Only super-user can modify flash */
	if (!capable(CAP_NET_ADMIN))
		return -EPERM;

	switch(comp->command)
	{
	case AIROFLSHRST:
		return cmdreset((struct airo_info *)dev->priv);

	case AIROFLSHSTFL:
		if (!((struct airo_info *)dev->priv)->flash &&
			(((struct airo_info *)dev->priv)->flash = kmalloc (FLASHSIZE, GFP_KERNEL)) == NULL)
			return -ENOMEM;
		return setflashmode((struct airo_info *)dev->priv);

	case AIROFLSHGCHR: /* Get char from aux */
		if(comp->len != sizeof(int))
			return -EINVAL;
		if (copy_from_user(&z,comp->data,comp->len))
			return -EFAULT;
		return flashgchar((struct airo_info *)dev->priv,z,8000);

	case AIROFLSHPCHR: /* Send char to card. */
		if(comp->len != sizeof(int))
			return -EINVAL;
		if (copy_from_user(&z,comp->data,comp->len))
			return -EFAULT;
		return flashpchar((struct airo_info *)dev->priv,z,8000);

	case AIROFLPUTBUF: /* Send 32k to card */
		if (!((struct airo_info *)dev->priv)->flash)
			return -ENOMEM;
		if(comp->len > FLASHSIZE)
			return -EINVAL;
		if(copy_from_user(((struct airo_info *)dev->priv)->flash,comp->data,comp->len))
			return -EFAULT;

		flashputbuf((struct airo_info *)dev->priv);
		return 0;

	case AIRORESTART:
		if(flashrestart((struct airo_info *)dev->priv,dev))
			return -EIO;
		return 0;
	}
	return -EINVAL;
}

#define FLASH_COMMAND  0x7e7e

/*
 * STEP 1)
 * Disable MAC and do soft reset on
 * card.
 */

J
Jouni Malinen 已提交
7920
static int cmdreset(struct airo_info *ai) {
L
Linus Torvalds 已提交
7921 7922 7923
	disable_MAC(ai, 1);

	if(!waitbusy (ai)){
7924
		airo_print_info(ai->dev->name, "Waitbusy hang before RESET");
L
Linus Torvalds 已提交
7925 7926 7927 7928 7929 7930 7931 7932
		return -EBUSY;
	}

	OUT4500(ai,COMMAND,CMD_SOFTRESET);

	ssleep(1);			/* WAS 600 12/7/00 */

	if(!waitbusy (ai)){
7933
		airo_print_info(ai->dev->name, "Waitbusy hang AFTER RESET");
L
Linus Torvalds 已提交
7934 7935 7936 7937 7938 7939 7940 7941 7942 7943
		return -EBUSY;
	}
	return 0;
}

/* STEP 2)
 * Put the card in legendary flash
 * mode
 */

J
Jouni Malinen 已提交
7944
static int setflashmode (struct airo_info *ai) {
L
Linus Torvalds 已提交
7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960
	set_bit (FLAG_FLASHING, &ai->flags);

	OUT4500(ai, SWS0, FLASH_COMMAND);
	OUT4500(ai, SWS1, FLASH_COMMAND);
	if (probe) {
		OUT4500(ai, SWS0, FLASH_COMMAND);
		OUT4500(ai, COMMAND,0x10);
	} else {
		OUT4500(ai, SWS2, FLASH_COMMAND);
		OUT4500(ai, SWS3, FLASH_COMMAND);
		OUT4500(ai, COMMAND,0);
	}
	msleep(500);		/* 500ms delay */

	if(!waitbusy(ai)) {
		clear_bit (FLAG_FLASHING, &ai->flags);
7961
		airo_print_info(ai->dev->name, "Waitbusy hang after setflash mode");
L
Linus Torvalds 已提交
7962 7963 7964 7965 7966 7967 7968 7969 7970
		return -EIO;
	}
	return 0;
}

/* Put character to SWS0 wait for dwelltime
 * x 50us for  echo .
 */

J
Jouni Malinen 已提交
7971
static int flashpchar(struct airo_info *ai,int byte,int dwelltime) {
L
Linus Torvalds 已提交
7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989
	int echo;
	int waittime;

	byte |= 0x8000;

	if(dwelltime == 0 )
		dwelltime = 200;

	waittime=dwelltime;

	/* Wait for busy bit d15 to go false indicating buffer empty */
	while ((IN4500 (ai, SWS0) & 0x8000) && waittime > 0) {
		udelay (50);
		waittime -= 50;
	}

	/* timeout for busy clear wait */
	if(waittime <= 0 ){
7990
		airo_print_info(ai->dev->name, "flash putchar busywait timeout!");
L
Linus Torvalds 已提交
7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010
		return -EBUSY;
	}

	/* Port is clear now write byte and wait for it to echo back */
	do {
		OUT4500(ai,SWS0,byte);
		udelay(50);
		dwelltime -= 50;
		echo = IN4500(ai,SWS1);
	} while (dwelltime >= 0 && echo != byte);

	OUT4500(ai,SWS1,0);

	return (echo == byte) ? 0 : -EIO;
}

/*
 * Get a character from the card matching matchbyte
 * Step 3)
 */
J
Jouni Malinen 已提交
8011
static int flashgchar(struct airo_info *ai,int matchbyte,int dwelltime){
L
Linus Torvalds 已提交
8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041
	int           rchar;
	unsigned char rbyte=0;

	do {
		rchar = IN4500(ai,SWS1);

		if(dwelltime && !(0x8000 & rchar)){
			dwelltime -= 10;
			mdelay(10);
			continue;
		}
		rbyte = 0xff & rchar;

		if( (rbyte == matchbyte) && (0x8000 & rchar) ){
			OUT4500(ai,SWS1,0);
			return 0;
		}
		if( rbyte == 0x81 || rbyte == 0x82 || rbyte == 0x83 || rbyte == 0x1a || 0xffff == rchar)
			break;
		OUT4500(ai,SWS1,0);

	}while(dwelltime > 0);
	return -EIO;
}

/*
 * Transfer 32k of firmware data from user buffer to our buffer and
 * send to the card
 */

J
Jouni Malinen 已提交
8042
static int flashputbuf(struct airo_info *ai){
L
Linus Torvalds 已提交
8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063
	int            nwords;

	/* Write stuff */
	if (test_bit(FLAG_MPI,&ai->flags))
		memcpy_toio(ai->pciaux + 0x8000, ai->flash, FLASHSIZE);
	else {
		OUT4500(ai,AUXPAGE,0x100);
		OUT4500(ai,AUXOFF,0);

		for(nwords=0;nwords != FLASHSIZE / 2;nwords++){
			OUT4500(ai,AUXDATA,ai->flash[nwords] & 0xffff);
		}
	}
	OUT4500(ai,SWS0,0x8000);

	return 0;
}

/*
 *
 */
J
Jouni Malinen 已提交
8064
static int flashrestart(struct airo_info *ai,struct net_device *dev){
L
Linus Torvalds 已提交
8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078
	int    i,status;

	ssleep(1);			/* Added 12/7/00 */
	clear_bit (FLAG_FLASHING, &ai->flags);
	if (test_bit(FLAG_MPI, &ai->flags)) {
		status = mpi_init_descriptors(ai);
		if (status != SUCCESS)
			return status;
	}
	status = setup_card(ai, dev->dev_addr, 1);

	if (!test_bit(FLAG_MPI,&ai->flags))
		for( i = 0; i < MAX_FIDS; i++ ) {
			ai->fids[i] = transmit_allocate
8079
				( ai, AIRO_DEF_MTU, i >= MAX_FIDS / 2 );
L
Linus Torvalds 已提交
8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127
		}

	ssleep(1);			/* Added 12/7/00 */
	return status;
}
#endif /* CISCO_EXT */

/*
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    In addition:

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
    3. The name of the author may not be used to endorse or promote
       products derived from this software without specific prior written
       permission.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

module_init(airo_init_module);
module_exit(airo_cleanup_module);