airo.c 190 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*======================================================================

    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
15 16
    <achirica@users.sourceforge.net> and Jean Tourrilhes <jt@hpl.hp.com>.
    Code was also integrated from the Cisco Aironet driver for Linux.
Linus Torvalds's avatar
Linus Torvalds committed
17

Linus Torvalds's avatar
Linus Torvalds committed
18 19 20 21 22 23 24 25 26 27 28
======================================================================*/

#include <linux/config.h>
#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>
Linus Torvalds's avatar
Linus Torvalds committed
29
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
30 31 32
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
33
#include <linux/suspend.h>
Linus Torvalds's avatar
Linus Torvalds committed
34 35 36 37 38 39 40 41 42 43 44
#include <linux/in.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/bitops.h>

#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>
Linus Torvalds's avatar
Linus Torvalds committed
45
#include <asm/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
46 47

#ifdef CONFIG_PCI
48
static struct pci_device_id card_ids[] = {
Linus Torvalds's avatar
Linus Torvalds committed
49 50 51 52 53
	{ 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, },
Linus Torvalds's avatar
Linus Torvalds committed
54
	{ 0, }
Linus Torvalds's avatar
Linus Torvalds committed
55 56 57 58 59 60 61
};
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 *);

static struct pci_driver airo_driver = {
62 63 64 65
	.name     = "airo",
	.id_table = card_ids,
	.probe    = airo_pci_probe,
	.remove   = __devexit_p(airo_pci_remove),
Linus Torvalds's avatar
Linus Torvalds committed
66 67 68 69 70
};
#endif /* CONFIG_PCI */

/* Include Wireless Extension definition and check version - Jean II */
#include <linux/wireless.h>
Linus Torvalds's avatar
Linus Torvalds committed
71
#define WIRELESS_SPY		// enable iwspy support
72 73 74
#if WIRELESS_EXT > 12
#include <net/iw_handler.h>	// New driver API
#endif	/* WIRELESS_EXT > 12 */
Linus Torvalds's avatar
Linus Torvalds committed
75

76
#define CISCO_EXT		// enable Cisco extensions
Linus Torvalds's avatar
Linus Torvalds committed
77 78 79 80
#ifdef CISCO_EXT
#include <linux/delay.h>
#endif

81
/* Support Cisco MIC feature */
82 83 84 85
#define MICSUPPORT

#if defined(MICSUPPORT) && !defined(CONFIG_CRYPTO)
#warning MIC support requires Crypto API
86
#undef MICSUPPORT
87
#endif
88

89 90 91
/* Hack to do some power saving */
#define POWER_ON_DOWN

Linus Torvalds's avatar
Linus Torvalds committed
92 93 94 95 96 97
/* 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 */

98
#define IGNLABEL(comment) 0
Linus Torvalds's avatar
Linus Torvalds committed
99 100
static char *statsLabels[] = {
	"RxOverrun",
101 102 103
	IGNLABEL("RxPlcpCrcErr"),
	IGNLABEL("RxPlcpFormatErr"),
	IGNLABEL("RxPlcpLengthErr"),
Linus Torvalds's avatar
Linus Torvalds committed
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
	"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",
147 148 149 150 151 152 153 154 155
	IGNLABEL("HmacTxMc"),
	IGNLABEL("HmacTxBc"),
	IGNLABEL("HmacTxUc"),
	IGNLABEL("HmacTxFail"),
	IGNLABEL("HmacRxMc"),
	IGNLABEL("HmacRxBc"),
	IGNLABEL("HmacRxUc"),
	IGNLABEL("HmacRxDiscard"),
	IGNLABEL("HmacRxAccepted"),
Linus Torvalds's avatar
Linus Torvalds committed
156 157 158 159 160 161 162
	"SsidMismatch",
	"ApMismatch",
	"RatesMismatch",
	"AuthReject",
	"AuthTimeout",
	"AssocReject",
	"AssocTimeout",
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	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"),
Linus Torvalds's avatar
Linus Torvalds committed
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
	"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
Linus Torvalds's avatar
Linus Torvalds committed
223
		    the bap, needed on some older cards and buses. */
Linus Torvalds's avatar
Linus Torvalds committed
224 225
static int adhoc;

226 227
static int probe = 1;

Linus Torvalds's avatar
Linus Torvalds committed
228 229 230 231 232 233 234 235 236 237 238 239
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 \
                   cards.  Direct support for ISA/PCI cards and support \
		   for PCMCIA when used with airo_cs.");
Linus Torvalds's avatar
Linus Torvalds committed
240
MODULE_LICENSE("Dual BSD/GPL");
Linus Torvalds's avatar
Linus Torvalds committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
MODULE_SUPPORTED_DEVICE("Aironet 4500, 4800 and Cisco 340");
MODULE_PARM(io,"1-4i");
MODULE_PARM(irq,"1-4i");
MODULE_PARM(basic_rate,"i");
MODULE_PARM(rates,"1-8i");
MODULE_PARM(ssids,"1-3s");
MODULE_PARM(auto_wep,"i");
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_PARM(aux_bap,"i");
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_PARM(maxencrypt, "i");
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_PARM(adhoc, "i");
MODULE_PARM_DESC(adhoc, "If non-zero, the card will start in adhoc mode.");
262 263
MODULE_PARM(probe, "i");
MODULE_PARM_DESC(probe, "If zero, the driver won't start the card.");
Linus Torvalds's avatar
Linus Torvalds committed
264 265 266 267 268 269 270 271 272 273 274 275

MODULE_PARM(proc_uid, "i");
MODULE_PARM_DESC(proc_uid, "The uid that the /proc files will belong to.");
MODULE_PARM(proc_gid, "i");
MODULE_PARM_DESC(proc_gid, "The gid that the /proc files will belong to.");
MODULE_PARM(airo_perm, "i");
MODULE_PARM_DESC(airo_perm, "The permission bits of /proc/[driver/]aironet.");
MODULE_PARM(proc_perm, "i");
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
276
   doesn't work though!!! */
Linus Torvalds's avatar
Linus Torvalds committed
277 278 279 280 281 282 283 284
static int do8bitIO = 0;

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

/* Commands */
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
#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
300
#define CMD_ALLOCATEAUX 0x0020
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
#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
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

/* 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
Linus Torvalds's avatar
Linus Torvalds committed
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

/* 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

/* 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
405
#define EV_TXCPY 0x400
Linus Torvalds's avatar
Linus Torvalds committed
406
#define EV_UNKNOWN 0x800
407
#define EV_MIC 0x1000 /* Message Integrity Check Interrupt */
408
#define STATUS_INTS ( EV_AWAKE | EV_LINK | EV_TXEXC | EV_TX | EV_RX | EV_MIC )
409 410

#ifdef CHECK_UNKNOWN_INTS
411
#define IGNORE_INTS ( EV_CMD | EV_UNKNOWN)
412 413 414
#else
#define IGNORE_INTS (~STATUS_INTS)
#endif
Linus Torvalds's avatar
Linus Torvalds committed
415 416 417

/* The RIDs */
#define RID_CAPABILITIES 0xFF00
Linus Torvalds's avatar
Linus Torvalds committed
418 419 420
#define RID_APINFO     0xFF01
#define RID_RADIOINFO  0xFF02
#define RID_UNKNOWN3   0xFF03
Linus Torvalds's avatar
Linus Torvalds committed
421
#define RID_RSSI       0xFF04
Linus Torvalds's avatar
Linus Torvalds committed
422 423 424 425 426 427 428 429
#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
Linus Torvalds's avatar
Linus Torvalds committed
430
#define RID_OPTIONS    0xFF18
Linus Torvalds's avatar
Linus Torvalds committed
431
#define RID_ACTUALCONFIG 0xFF20 /*readonly*/
Linus Torvalds's avatar
Linus Torvalds committed
432 433
#define RID_FACTORYCONFIG 0xFF21
#define RID_UNKNOWN22  0xFF22
Linus Torvalds's avatar
Linus Torvalds committed
434 435 436
#define RID_LEAPUSERNAME 0xFF23
#define RID_LEAPPASSWORD 0xFF24
#define RID_STATUS     0xFF50
437 438 439
#define RID_BEACON_HST 0xFF51
#define RID_BUSY_HST   0xFF52
#define RID_RETRIES_HST 0xFF53
Linus Torvalds's avatar
Linus Torvalds committed
440 441 442
#define RID_UNKNOWN54  0xFF54
#define RID_UNKNOWN55  0xFF55
#define RID_UNKNOWN56  0xFF56
443
#define RID_MIC        0xFF57
Linus Torvalds's avatar
Linus Torvalds committed
444 445 446
#define RID_STATS16    0xFF60
#define RID_STATS16DELTA 0xFF61
#define RID_STATS16DELTACLEAR 0xFF62
Linus Torvalds's avatar
Linus Torvalds committed
447 448 449
#define RID_STATS      0xFF68
#define RID_STATSDELTA 0xFF69
#define RID_STATSDELTACLEAR 0xFF6A
450 451
#define RID_ECHOTEST_RID 0xFF70
#define RID_ECHOTEST_RESULTS 0xFF71
Linus Torvalds's avatar
Linus Torvalds committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
#define RID_BSSLISTFIRST 0xFF72
#define RID_BSSLISTNEXT  0xFF73

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

typedef struct {
	u16 status;
	u16 rsp0;
	u16 rsp1;
	u16 rsp2;
} Resp;
Linus Torvalds's avatar
Linus Torvalds committed
468 469 470 471 472 473 474

/*
 * 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.
 */

Linus Torvalds's avatar
Linus Torvalds committed
475 476 477
/* This is redundant for x86 archs, but it seems necessary for ARM */
#pragma pack(1)

Linus Torvalds's avatar
Linus Torvalds committed
478 479 480 481 482
/* This structure came from an email sent to me from an engineer at
   aironet for inclusion into this driver */
typedef struct {
	u16 len;
	u16 kindex;
483
	u8 mac[ETH_ALEN];
Linus Torvalds's avatar
Linus Torvalds committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
	u16 klen;
	u8 key[16];
} WepKeyRid;

/* These structures are from the Aironet's PC4500 Developers Manual */
typedef struct {
	u16 len;
	u8 ssid[32];
} Ssid;

typedef struct {
	u16 len;
	Ssid ssids[3];
} SsidRid;

typedef struct {
        u16 len;
        u16 modulation;
#define MOD_DEFAULT 0
#define MOD_CCK 1
#define MOD_MOK 2
} ModulationRid;

typedef struct {
	u16 len; /* sizeof(ConfigRid) */
	u16 opmode; /* operating mode */
#define MODE_STA_IBSS 0
#define MODE_STA_ESS 1
#define MODE_AP 2
#define MODE_AP_RPTR 3
#define MODE_ETHERNET_HOST (0<<8) /* rx payloads converted */
#define MODE_LLC_HOST (1<<8) /* rx payloads left as is */
#define MODE_AIRONET_EXTEND (1<<9) /* enable Aironet extenstions */
#define MODE_AP_INTERFACE (1<<10) /* enable ap interface extensions */
#define MODE_ANTENNA_ALIGN (1<<11) /* enable antenna alignment */
#define MODE_ETHER_LLC (1<<12) /* enable ethernet LLC */
#define MODE_LEAF_NODE (1<<13) /* enable leaf node bridge */
#define MODE_CF_POLLABLE (1<<14) /* enable CF pollable */
522
#define MODE_MIC (1<<15) /* enable MIC */
Linus Torvalds's avatar
Linus Torvalds committed
523 524 525 526 527 528 529 530 531 532 533
	u16 rmode; /* receive mode */
#define RXMODE_BC_MC_ADDR 0
#define RXMODE_BC_ADDR 1 /* ignore multicasts */
#define RXMODE_ADDR 2 /* ignore multicast and broadcast */
#define RXMODE_RFMON 3 /* wireless monitor mode */
#define RXMODE_RFMON_ANYBSS 4
#define RXMODE_LANMON 5 /* lan style monitor -- data packets only */
#define RXMODE_DISABLE_802_3_HEADER (1<<8) /* disables 802.3 header on rx */
#define RXMODE_NORMALIZED_RSSI (1<<9) /* return normalized RSSI */
	u16 fragThresh;
	u16 rtsThres;
534
	u8 macAddr[ETH_ALEN];
Linus Torvalds's avatar
Linus Torvalds committed
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 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
	u8 rates[8];
	u16 shortRetryLimit;
	u16 longRetryLimit;
	u16 txLifetime; /* in kusec */
	u16 rxLifetime; /* in kusec */
	u16 stationary;
	u16 ordering;
	u16 u16deviceType; /* for overriding device type */
	u16 cfpRate;
	u16 cfpDuration;
	u16 _reserved1[3];
	/*---------- Scanning/Associating ----------*/
	u16 scanMode;
#define SCANMODE_ACTIVE 0
#define SCANMODE_PASSIVE 1
#define SCANMODE_AIROSCAN 2
	u16 probeDelay; /* in kusec */
	u16 probeEnergyTimeout; /* in kusec */
        u16 probeResponseTimeout;
	u16 beaconListenTimeout;
	u16 joinNetTimeout;
	u16 authTimeout;
	u16 authType;
#define AUTH_OPEN 0x1
#define AUTH_ENCRYPT 0x101
#define AUTH_SHAREDKEY 0x102
#define AUTH_ALLOW_UNENCRYPTED 0x200
	u16 associationTimeout;
	u16 specifiedApTimeout;
	u16 offlineScanInterval;
	u16 offlineScanDuration;
	u16 linkLossDelay;
	u16 maxBeaconLostTime;
	u16 refreshInterval;
#define DISABLE_REFRESH 0xFFFF
	u16 _reserved1a[1];
	/*---------- Power save operation ----------*/
	u16 powerSaveMode;
#define POWERSAVE_CAM 0
#define POWERSAVE_PSP 1
#define POWERSAVE_PSPCAM 2
	u16 sleepForDtims;
	u16 listenInterval;
	u16 fastListenInterval;
	u16 listenDecay;
	u16 fastListenDelay;
	u16 _reserved2[2];
	/*---------- Ap/Ibss config items ----------*/
	u16 beaconPeriod;
	u16 atimDuration;
	u16 hopPeriod;
	u16 channelSet;
	u16 channel;
	u16 dtimPeriod;
	u16 bridgeDistance;
	u16 radioID;
	/*---------- Radio configuration ----------*/
	u16 radioType;
#define RADIOTYPE_DEFAULT 0
#define RADIOTYPE_802_11 1
#define RADIOTYPE_LEGACY 2
	u8 rxDiversity;
	u8 txDiversity;
	u16 txPower;
#define TXPOWER_DEFAULT 0
	u16 rssiThreshold;
#define RSSI_DEFAULT 0
        u16 modulation;
Linus Torvalds's avatar
Linus Torvalds committed
603 604 605 606
#define PREAMBLE_AUTO 0
#define PREAMBLE_LONG 1
#define PREAMBLE_SHORT 2
	u16 preamble;
Linus Torvalds's avatar
Linus Torvalds committed
607 608 609 610 611 612 613 614 615
	u16 homeProduct;
	u16 radioSpecific;
	/*---------- Aironet Extensions ----------*/
	u8 nodeName[16];
	u16 arlThreshold;
	u16 arlDecay;
	u16 arlDelay;
	u16 _reserved4[1];
	/*---------- Aironet Extensions ----------*/
616
	u8 magicAction;
Linus Torvalds's avatar
Linus Torvalds committed
617 618 619 620 621 622
#define MAGIC_ACTION_STSCHG 1
#define MACIC_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)
623
	u8 magicControl;
Linus Torvalds's avatar
Linus Torvalds committed
624 625 626 627 628
	u16 autoWake;
} ConfigRid;

typedef struct {
	u16 len;
629
	u8 mac[ETH_ALEN];
Linus Torvalds's avatar
Linus Torvalds committed
630 631 632 633 634 635
	u16 mode;
	u16 errorCode;
	u16 sigQuality;
	u16 SSIDlen;
	char SSID[32];
	char apName[16];
636
	u8 bssid[4][ETH_ALEN];
Linus Torvalds's avatar
Linus Torvalds committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650
	u16 beaconPeriod;
	u16 dimPeriod;
	u16 atimDuration;
	u16 hopPeriod;
	u16 channelSet;
	u16 channel;
	u16 hopsToBackbone;
	u16 apTotalLoad;
	u16 generatedLoad;
	u16 accumulatedArl;
	u16 signalQuality;
	u16 currentXmitRate;
	u16 apDevExtensions;
	u16 normalizedSignalStrength;
651
	u16 shortPreamble;
652
	u8 apIP[4];
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
	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 */
	u16 load;
	u8 carrier[4];
	u16 assocStatus;
#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
Linus Torvalds's avatar
Linus Torvalds committed
683 684 685 686 687 688 689
} StatusRid;

typedef struct {
	u16 len;
	u16 spacer;
	u32 vals[100];
} StatsRid;
Linus Torvalds's avatar
Linus Torvalds committed
690

Linus Torvalds's avatar
Linus Torvalds committed
691 692 693

typedef struct {
	u16 len;
694
	u8 ap[4][ETH_ALEN];
Linus Torvalds's avatar
Linus Torvalds committed
695 696 697 698 699
} APListRid;

typedef struct {
	u16 len;
	char oui[3];
Linus Torvalds's avatar
Linus Torvalds committed
700
	char zero;
Linus Torvalds's avatar
Linus Torvalds committed
701 702 703 704
	u16 prodNum;
	char manName[32];
	char prodName[16];
	char prodVer[8];
705 706
	char factoryAddr[ETH_ALEN];
	char aironetAddr[ETH_ALEN];
Linus Torvalds's avatar
Linus Torvalds committed
707 708
	u16 radioType;
	u16 country;
709
	char callid[ETH_ALEN];
Linus Torvalds's avatar
Linus Torvalds committed
710 711 712 713 714 715 716 717 718 719 720 721 722
	char supportedRates[8];
	char rxDiversity;
	char txDiversity;
	u16 txPowerLevels[8];
	u16 hardVer;
	u16 hardCap;
	u16 tempRange;
	u16 softVer;
	u16 softSubVer;
	u16 interfaceVer;
	u16 softCap;
	u16 bootBlockVer;
	u16 requiredHard;
723
	u16 extSoftCap;
Linus Torvalds's avatar
Linus Torvalds committed
724 725
} CapabilityRid;

Linus Torvalds's avatar
Linus Torvalds committed
726 727 728 729 730 731
typedef struct {
  u16 len;
  u16 index; /* First is 0 and 0xffff means end of list */
#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) */
Linus Torvalds's avatar
Linus Torvalds committed
732
  u16 radioType;
733
  u8 bssid[ETH_ALEN]; /* Mac address of the BSS */
Linus Torvalds's avatar
Linus Torvalds committed
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
  u8 zero;
  u8 ssidLen;
  u8 ssid[32];
  u16 rssi;
#define CAP_ESS (1<<0)
#define CAP_IBSS (1<<1)
#define CAP_PRIVACY (1<<4)
#define CAP_SHORTHDR (1<<5)
  u16 cap;
  u16 beaconInterval;
  u8 rates[8]; /* Same as rates for config rid */
  struct { /* For frequency hopping only */
    u16 dwell;
    u8 hopSet;
    u8 hopPattern;
    u8 hopIndex;
    u8 fill;
  } fh;
  u16 dsChannel;
  u16 atimWindow;
} BSSListRid;

Linus Torvalds's avatar
Linus Torvalds committed
756 757 758 759 760 761 762 763 764 765
typedef struct {
  u8 rssipct;
  u8 rssidBm;
} tdsRssiEntry;

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

766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
typedef struct {
	u16 len;
	u16 state;
	u16 multicastValid;
	u8  multicast[16];
	u16 unicastValid;
	u8  unicast[16];
} MICRid;

typedef struct {
	u16 typelen;

	union {
	    u8 snap[8];
	    struct {
		u8 dsap;
		u8 ssap;
		u8 control;
		u8 orgcode[3];
		u8 fieldtype[2];
	    } llc;
	} u;
	u32 mic;
	u32 seq;
} MICBuffer;

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

Linus Torvalds's avatar
Linus Torvalds committed
797 798
#pragma pack()

Linus Torvalds's avatar
Linus Torvalds committed
799 800 801 802 803 804 805 806 807 808 809 810 811
#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
Linus Torvalds's avatar
Linus Torvalds committed
812 813 814 815 816 817 818 819 820
/* 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 */
821 822 823 824
/* 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 */
Linus Torvalds's avatar
Linus Torvalds committed
825
#define AIROIOCTL	SIOCIWFIRSTPRIV
Linus Torvalds's avatar
Linus Torvalds committed
826 827 828 829 830
#define AIROIDIFC 	AIROIOCTL + 1

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

#define	AIROGCAP  		0	// Capability rid
Linus Torvalds's avatar
Linus Torvalds committed
831 832
#define AIROGCFG		1       // USED A LOT
#define AIROGSLIST		2	// System ID list
Linus Torvalds's avatar
Linus Torvalds committed
833 834 835 836 837 838 839 840
#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
841 842 843
#define AIROGMICRID		11
#define AIROGMICSTATS		12
#define AIROGFLAGS		13
844
#define AIRORRID		15
Linus Torvalds's avatar
Linus Torvalds committed
845 846 847

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

848
#define AIROPCAP               	AIROGSTATSD32 + 40
Linus Torvalds's avatar
Linus Torvalds committed
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
#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

typedef struct aironet_ioctl {
	unsigned short command;	// What to do
	unsigned short len;		// Len of data
	unsigned char *data;		// d-data
} aironet_ioctl;
#endif /* CISCO_EXT */

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
#define NUM_MODULES       2
#define MIC_MSGLEN_MAX    2400
#define EMMH32_MSGLEN_MAX MIC_MSGLEN_MAX

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];
		u32 d32;
	} 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;

Linus Torvalds's avatar
Linus Torvalds committed
919 920
#ifdef WIRELESS_EXT
// Frequency list (map channels to frequencies)
921
static const long frequency_list[] = { 2412, 2417, 2422, 2427, 2432, 2437, 2442,
Linus Torvalds's avatar
Linus Torvalds committed
922 923 924 925 926 927 928 929 930
				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;
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945

/* 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 */

#if WIRELESS_EXT > 12
/* List of Wireless Handlers (new API) */
static const struct iw_handler_def	airo_handler_def;
#else	/* WIRELESS_EXT > 12 */
/* More Wireless Extensions backward compatibility */
/* Part of iw_handler prototype we need (apart that we don't need it) */
struct iw_request_info {};
#endif	/* WIRELESS_EXT > 12 */
Linus Torvalds's avatar
Linus Torvalds committed
946 947
#endif /* WIRELESS_EXT */

948
static const char version[] = "airo.c 0.6 (Ben Reed & Javier Achirica)";
Linus Torvalds's avatar
Linus Torvalds committed
949 950 951 952 953 954

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 );
955
static u16 setup_card(struct airo_info*, u8 *mac);
956 957
static int enable_MAC( struct airo_info *ai, Resp *rsp, int lock );
static void disable_MAC(struct airo_info *ai, int lock);
Linus Torvalds's avatar
Linus Torvalds committed
958 959 960 961
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);
Linus Torvalds's avatar
Linus Torvalds committed
962
static int aux_bap_read(struct airo_info*, u16 *pu16Dst, int bytelen,
Linus Torvalds's avatar
Linus Torvalds committed
963
			int whichbap);
Linus Torvalds's avatar
Linus Torvalds committed
964
static int fast_bap_read(struct airo_info*, u16 *pu16Dst, int bytelen,
Linus Torvalds's avatar
Linus Torvalds committed
965 966 967 968
			 int whichbap);
static int bap_write(struct airo_info*, const u16 *pu16Src, int bytelen,
		     int whichbap);
static int PC4500_accessrid(struct airo_info*, u16 rid, u16 accmd);
969
static int PC4500_readrid(struct airo_info*, u16 rid, void *pBuf, int len, int lock);
Linus Torvalds's avatar
Linus Torvalds committed
970
static int PC4500_writerid(struct airo_info*, u16 rid, const void
971
			   *pBuf, int len, int lock);
Linus Torvalds's avatar
Linus Torvalds committed
972
static int do_writerid( struct airo_info*, u16 rid, const void *rid_data,
973
			int len, int dummy );
974 975 976
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);
Linus Torvalds's avatar
Linus Torvalds committed
977

Jeff Garzik's avatar
Jeff Garzik committed
978
static irqreturn_t airo_interrupt( int irq, void* dev_id, struct pt_regs
Linus Torvalds's avatar
Linus Torvalds committed
979
			    *regs);
980 981
static int airo_thread(void *data);
static void timer_func( struct net_device *dev );
Linus Torvalds's avatar
Linus Torvalds committed
982 983 984
static int airo_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
#ifdef WIRELESS_EXT
struct iw_statistics *airo_get_wireless_stats (struct net_device *dev);
985
static void airo_read_wireless_stats (struct airo_info *local);
Linus Torvalds's avatar
Linus Torvalds committed
986 987 988 989 990 991
#endif /* WIRELESS_EXT */
#ifdef CISCO_EXT
static int readrids(struct net_device *dev, aironet_ioctl *comp);
static int writerids(struct net_device *dev, aironet_ioctl *comp);
int flashcard(struct net_device *dev, aironet_ioctl *comp);
#endif /* CISCO_EXT */
992
#ifdef MICSUPPORT
993 994
static void micinit(struct airo_info *ai);
static int micsetup(struct airo_info *ai);
995 996
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);
997 998

#include <linux/crypto.h>
999
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1000 1001 1002 1003 1004 1005

struct airo_info {
	struct net_device_stats	stats;
	int open;
	struct net_device             *dev;
	/* Note, we can have MAX_FIDS outstanding.  FIDs are 16-bits, so we
1006
	   use the high bit to mark whether it is in use. */
Linus Torvalds's avatar
Linus Torvalds committed
1007 1008 1009 1010
#define MAX_FIDS 6
	int                           fids[MAX_FIDS];
	int registered;
	ConfigRid config;
1011
	int need_commit;	// Need to set config
Linus Torvalds's avatar
Linus Torvalds committed
1012 1013 1014 1015 1016
	char keyindex; // Used with auto wep
	char defindex; // Used with auto wep
	struct proc_dir_entry *proc_entry;
	struct airo_info *next;
        spinlock_t aux_lock;
1017
        unsigned long flags;
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
#define FLAG_PROMISC	8	/* IFF_PROMISC 0x100 - include/linux/if.h */
#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_FLASHING	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
#define FLAG_PENDING_XMIT 9
#define FLAG_PENDING_XMIT11 10
1030
#define JOB_MASK	0x1ff0000
1031 1032 1033 1034 1035 1036 1037 1038
#define JOB_DIE		16
#define JOB_XMIT	17
#define JOB_XMIT11	18
#define JOB_STATS	19
#define JOB_PROMISC	20
#define JOB_MIC		21
#define JOB_EVENT	22
#define JOB_AUTOWEP	23
1039
#define JOB_WSTATS	24
Linus Torvalds's avatar
Linus Torvalds committed
1040
	int (*bap_read)(struct airo_info*, u16 *pu16Dst, int bytelen,
Linus Torvalds's avatar
Linus Torvalds committed
1041
			int whichbap);
Linus Torvalds's avatar
Linus Torvalds committed
1042
	unsigned short *flash;
Linus Torvalds's avatar
Linus Torvalds committed
1043
	tdsRssiEntry *rssi;
1044
	struct task_struct *task;
1045 1046 1047 1048 1049
	struct semaphore sem;
	pid_t thr_pid;
	wait_queue_head_t thr_wait;
	struct completion thr_exited;
	unsigned long expires;
1050 1051 1052 1053 1054
	struct {
		struct sk_buff *skb;
		int fid;
	} xmit, xmit11;
	struct net_device *wifidev;
Linus Torvalds's avatar
Linus Torvalds committed
1055 1056
#ifdef WIRELESS_EXT
	struct iw_statistics	wstats;		// wireless stats
1057
	unsigned long		scan_timestamp;	/* Time started to scan */
1058 1059 1060
#if WIRELESS_EXT > 15
	struct iw_spy_data	spy_data;
#else /* WIRELESS_EXT > 15 */
Linus Torvalds's avatar
Linus Torvalds committed
1061 1062
#ifdef WIRELESS_SPY
	int			spy_number;
1063
	u_char			spy_address[IW_MAX_SPY][ETH_ALEN];
Linus Torvalds's avatar
Linus Torvalds committed
1064 1065
	struct iw_quality	spy_stat[IW_MAX_SPY];
#endif /* WIRELESS_SPY */
1066
#endif /* WIRELESS_EXT > 15 */
Linus Torvalds's avatar
Linus Torvalds committed
1067
#endif /* WIRELESS_EXT */
1068
#ifdef MICSUPPORT
1069
	/* MIC stuff */
1070
	struct crypto_tfm	*tfm;
1071 1072
	mic_module		mod[2];
	mic_statistics		micstats;
1073
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1074 1075
};

Linus Torvalds's avatar
Linus Torvalds committed
1076
static inline int bap_read(struct airo_info *ai, u16 *pu16Dst, int bytelen,
Linus Torvalds's avatar
Linus Torvalds committed
1077 1078 1079 1080 1081 1082 1083 1084 1085
			   int whichbap) {
	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 );

1086
#ifdef MICSUPPORT
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 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 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 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
/***********************************************************************
 *                              MIC ROUTINES                           *
 ***********************************************************************
 */

static int RxSeqValid (struct airo_info *ai,miccntx *context,int mcast,u32 micSeq);
static void MoveWindow(miccntx *context, u32 micSeq);
void emmh32_setseed(emmh32_context *context, u8 *pkey, int keylen, struct crypto_tfm *);
void emmh32_init(emmh32_context *context);
void emmh32_update(emmh32_context *context, u8 *pOctets, int len);
void emmh32_final(emmh32_context *context, u8 digest[4]);

/* micinit - Initialize mic seed */

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

	clear_bit(JOB_MIC, &ai->flags);
	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)
	        ai->tfm = crypto_alloc_tfm("aes", 0);

        if (ai->tfm == NULL) {
                printk(KERN_ERR "airo: failed to load transform for AES\n");
                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;
}

char micsnap[]= {0xAA,0xAA,0x03,0x00,0x40,0x96,0x00,0x02};

/*===========================================================================
 * 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 ((u32)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 */
void emmh32_setseed(emmh32_context *context, u8 *pkey, int keylen, struct crypto_tfm *tfm)
{
  /* 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;
1437
	u8 *cipher, plain[16];
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
	struct scatterlist sg[1];

	crypto_cipher_setkey(tfm, pkey, 16);
	counter = 0;
	for (i = 0; i < (sizeof(context->coeff)/sizeof(context->coeff[0])); ) {
		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++;
1448 1449 1450
		memcpy (plain, aes_counter, 16);
		sg[0].page = virt_to_page(plain);
		sg[0].offset = ((long) plain & ~PAGE_MASK);
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
		sg[0].length = 16;
		crypto_cipher_encrypt(tfm, sg, sg, 16);
		cipher = kmap(sg[0].page) + sg[0].offset;
		for (j=0; (j<16) && (i< (sizeof(context->coeff)/sizeof(context->coeff[0]))); ) {
			context->coeff[i++] = ntohl(*(u32 *)&cipher[j]);
			j += 4;
		}
	}
}

/* prepare for calculation of a new mic */
void emmh32_init(emmh32_context *context)
{
	/* prepare for new mic calculation */
	context->accum = 0;
	context->position = 0;
}

/* add some bytes to the mic calculation */
void emmh32_update(emmh32_context *context, u8 *pOctets, int len)
{
	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);
		MIC_ACCUM(htonl(context->part.d32));
	}

	/* deal with full 32-bit words */
	while (len >= 4) {
		MIC_ACCUM(htonl(*(u32 *)pOctets));
		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 */
void emmh32_final(emmh32_context *context, u8 digest[4])
{
	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 */
		val = htonl(context->part.d32);
		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;
}
1544 1545
#endif

Linus Torvalds's avatar
Linus Torvalds committed
1546 1547 1548 1549 1550 1551 1552 1553 1554
static int readBSSListRid(struct airo_info *ai, int first,
		      BSSListRid *list) {
	int rc;
			Cmd cmd;
			Resp rsp;

	if (first == 1) {
			memset(&cmd, 0, sizeof(cmd));
			cmd.cmd=CMD_LISTBSS;
1555 1556 1557 1558
			if (down_interruptible(&ai->sem))
				return -ERESTARTSYS;
			issuecommand(ai, &cmd, &rsp);
			up(&ai->sem);
Linus Torvalds's avatar
Linus Torvalds committed
1559 1560
			/* Let the command take effect */
			set_current_state (TASK_INTERRUPTIBLE);
1561
			ai->task = current;
Linus Torvalds's avatar
Linus Torvalds committed
1562
			schedule_timeout (3*HZ);
1563
			ai->task = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1564
		}
1565
	rc = PC4500_readrid(ai, first ? RID_BSSLISTFIRST : RID_BSSLISTNEXT,
1566
			    list, sizeof(*list), 1);
Linus Torvalds's avatar
Linus Torvalds committed
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578

	list->len = le16_to_cpu(list->len);
	list->index = le16_to_cpu(list->index);
	list->radioType = le16_to_cpu(list->radioType);
	list->cap = le16_to_cpu(list->cap);
	list->beaconInterval = le16_to_cpu(list->beaconInterval);
	list->fh.dwell = le16_to_cpu(list->fh.dwell);
	list->dsChannel = le16_to_cpu(list->dsChannel);
	list->atimWindow = le16_to_cpu(list->atimWindow);
	return rc;
}

Linus Torvalds's avatar
Linus Torvalds committed
1579
static int readWepKeyRid(struct airo_info*ai, WepKeyRid *wkr, int temp) {
Linus Torvalds's avatar
Linus Torvalds committed
1580
	int rc = PC4500_readrid(ai, temp ? RID_WEP_TEMP : RID_WEP_PERM,
1581
				wkr, sizeof(*wkr), 1);
Linus Torvalds's avatar
Linus Torvalds committed
1582

Linus Torvalds's avatar
Linus Torvalds committed
1583 1584 1585 1586 1587 1588 1589
	wkr->len = le16_to_cpu(wkr->len);
	wkr->kindex = le16_to_cpu(wkr->kindex);
	wkr->klen = le16_to_cpu(wkr->klen);
	return rc;
}
/* In the writeXXXRid routines we copy the rids so that we don't screwup
 * the originals when we endian them... */
1590
static int writeWepKeyRid(struct airo_info*ai, WepKeyRid *pwkr, int perm, int lock) {
Linus Torvalds's avatar
Linus Torvalds committed
1591 1592 1593 1594 1595 1596
	int rc;
	WepKeyRid wkr = *pwkr;

	wkr.len = cpu_to_le16(wkr.len);
	wkr.kindex = cpu_to_le16(wkr.kindex);
	wkr.klen = cpu_to_le16(wkr.klen);
1597
	rc = PC4500_writerid(ai, RID_WEP_TEMP, &wkr, sizeof(wkr), lock);
Linus Torvalds's avatar
Linus Torvalds committed
1598
	if (rc!=SUCCESS) printk(KERN_ERR "airo:  WEP_TEMP set %x\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
1599
	if (perm) {
1600
		rc = PC4500_writerid(ai, RID_WEP_PERM, &wkr, sizeof(wkr), lock);
Linus Torvalds's avatar
Linus Torvalds committed
1601 1602 1603 1604 1605 1606 1607 1608 1609
		if (rc!=SUCCESS) {
			printk(KERN_ERR "airo:  WEP_PERM set %x\n", rc);
		}
	}
	return rc;
}

static int readSsidRid(struct airo_info*ai, SsidRid *ssidr) {
	int i;
1610
	int rc = PC4500_readrid(ai, RID_SSID, ssidr, sizeof(*ssidr), 1);
Linus Torvalds's avatar
Linus Torvalds committed
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626

	ssidr->len = le16_to_cpu(ssidr->len);
	for(i = 0; i < 3; i++) {
		ssidr->ssids[i].len = le16_to_cpu(ssidr->ssids[i].len);
	}
	return rc;
}
static int writeSsidRid(struct airo_info*ai, SsidRid *pssidr) {
	int rc;
	int i;
	SsidRid ssidr = *pssidr;

	ssidr.len = cpu_to_le16(ssidr.len);
	for(i = 0; i < 3; i++) {
		ssidr.ssids[i].len = cpu_to_le16(ssidr.ssids[i].len);
	}
1627
	rc = PC4500_writerid(ai, RID_SSID, &ssidr, sizeof(ssidr), 1);
Linus Torvalds's avatar
Linus Torvalds committed
1628 1629
	return rc;
}
1630
static int readConfigRid(struct airo_info*ai, int lock) {
1631
	int rc;
Linus Torvalds's avatar
Linus Torvalds committed
1632
	u16 *s;
1633 1634 1635 1636 1637
	ConfigRid cfg;

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

1638
	rc = PC4500_readrid(ai, RID_ACTUALCONFIG, &cfg, sizeof(cfg), lock);
1639 1640
	if (rc != SUCCESS)
		return rc;
Linus Torvalds's avatar
Linus Torvalds committed
1641

1642
	for(s = &cfg.len; s <= &cfg.rtsThres; s++) *s = le16_to_cpu(*s);
Linus Torvalds's avatar
Linus Torvalds committed
1643

1644
	for(s = &cfg.shortRetryLimit; s <= &cfg.radioType; s++)
Linus Torvalds's avatar
Linus Torvalds committed
1645 1646
		*s = le16_to_cpu(*s);

1647
	for(s = &cfg.txPower; s <= &cfg.radioSpecific; s++)
Linus Torvalds's avatar
Linus Torvalds committed
1648
		*s = le16_to_cpu(*s);
Linus Torvalds's avatar
Linus Torvalds committed
1649

1650
	for(s = &cfg.arlThreshold; s <= &cfg.autoWake; s++)
Linus Torvalds's avatar
Linus Torvalds committed
1651
		*s = le16_to_cpu(*s);
Linus Torvalds's avatar
Linus Torvalds committed
1652

1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
	ai->config = cfg;
	return SUCCESS;
}
static inline void checkThrottle(struct airo_info *ai) {
	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;
			}
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1666
}
1667
static int writeConfigRid(struct airo_info*ai, int lock) {
Linus Torvalds's avatar
Linus Torvalds committed
1668
	u16 *s;
1669 1670 1671 1672 1673 1674 1675
	ConfigRid cfgr;

	if (!ai->need_commit)
		return SUCCESS;

	ai->need_commit = 0;
	checkThrottle(ai);
1676 1677
	cfgr = ai->config;

1678
	if ((cfgr.opmode & 0xFF) == MODE_STA_IBSS)
1679
		set_bit(FLAG_ADHOC, &ai->flags);
1680
	else
1681
		clear_bit(FLAG_ADHOC, &ai->flags);
1682

Linus Torvalds's avatar
Linus Torvalds committed
1683 1684 1685 1686 1687 1688 1689
	for(s = &cfgr.len; s <= &cfgr.rtsThres; s++) *s = cpu_to_le16(*s);

	for(s = &cfgr.shortRetryLimit; s <= &cfgr.radioType; s++)
		*s = cpu_to_le16(*s);

	for(s = &cfgr.txPower; s <= &cfgr.radioSpecific; s++)
		*s = cpu_to_le16(*s);
Linus Torvalds's avatar
Linus Torvalds committed
1690

Linus Torvalds's avatar
Linus Torvalds committed
1691 1692
	for(s = &cfgr.arlThreshold; s <= &cfgr.autoWake; s++)
		*s = cpu_to_le16(*s);
Linus Torvalds's avatar
Linus Torvalds committed
1693

1694
	return PC4500_writerid( ai, RID_CONFIG, &cfgr, sizeof(cfgr), lock);
Linus Torvalds's avatar
Linus Torvalds committed
1695
}
1696 1697
static int readStatusRid(struct airo_info*ai, StatusRid *statr, int lock) {
	int rc = PC4500_readrid(ai, RID_STATUS, statr, sizeof(*statr), lock);
Linus Torvalds's avatar
Linus Torvalds committed
1698 1699 1700 1701 1702
	u16 *s;

	statr->len = le16_to_cpu(statr->len);
	for(s = &statr->mode; s <= &statr->SSIDlen; s++) *s = le16_to_cpu(*s);

1703
	for(s = &statr->beaconPeriod; s <= &statr->shortPreamble; s++)
Linus Torvalds's avatar
Linus Torvalds committed
1704
		*s = le16_to_cpu(*s);
1705 1706
	statr->load = le16_to_cpu(statr->load);
	statr->assocStatus = le16_to_cpu(statr->assocStatus);
Linus Torvalds's avatar
Linus Torvalds committed
1707 1708 1709
	return rc;
}
static int readAPListRid(struct airo_info*ai, APListRid *aplr) {
1710
	int rc =  PC4500_readrid(ai, RID_APLIST, aplr, sizeof(*aplr), 1);
Linus Torvalds's avatar
Linus Torvalds committed
1711 1712 1713 1714 1715 1716
	aplr->len = le16_to_cpu(aplr->len);
	return rc;
}
static int writeAPListRid(struct airo_info*ai, APListRid *aplr) {
	int rc;
	aplr->len = cpu_to_le16(aplr->len);
1717
	rc = PC4500_writerid(ai, RID_APLIST, aplr, sizeof(*aplr), 1);
Linus Torvalds's avatar
Linus Torvalds committed
1718 1719 1720
	return rc;
}
static int readCapabilityRid(struct airo_info*ai, CapabilityRid *capr) {
1721
	int rc = PC4500_readrid(ai, RID_CAPABILITIES, capr, sizeof(*capr), 1);
Linus Torvalds's avatar
Linus Torvalds committed
1722
	u16 *s;
Linus Torvalds's avatar
Linus Torvalds committed
1723

Linus Torvalds's avatar
Linus Torvalds committed
1724 1725 1726 1727 1728 1729 1730 1731
	capr->len = le16_to_cpu(capr->len);
	capr->prodNum = le16_to_cpu(capr->prodNum);
	capr->radioType = le16_to_cpu(capr->radioType);
	capr->country = le16_to_cpu(capr->country);
	for(s = &capr->txPowerLevels[0]; s <= &capr->requiredHard; s++)
		*s = le16_to_cpu(*s);
	return rc;
}
1732 1733
static int readStatsRid(struct airo_info*ai, StatsRid *sr, int rid, int lock) {
	int rc = PC4500_readrid(ai, rid, sr, sizeof(*sr), lock);
Linus Torvalds's avatar
Linus Torvalds committed
1734 1735 1736 1737 1738 1739 1740 1741 1742
	u32 *i;

	sr->len = le16_to_cpu(sr->len);
	for(i = &sr->vals[0]; i <= &sr->vals[99]; i++) *i = le32_to_cpu(*i);
	return rc;
}

static int airo_open(struct net_device *dev) {
	struct airo_info *info = dev->priv;
1743 1744
	Resp rsp;

1745
	if (test_bit(FLAG_FLASHING, &info->flags))
1746
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
1747

1748 1749 1750 1751 1752
	/* 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 */
	if(info->need_commit) {
1753 1754
		disable_MAC(info, 1);
		writeConfigRid(info, 1);
1755 1756
	}

1757 1758
	if (info->wifidev != dev) {
		/* Power on the MAC controller (which may have been disabled) */
1759
		clear_bit(FLAG_RADIO_DOWN, &info->flags);
1760
		enable_interrupts(info);
1761
	}
1762
	enable_MAC(info, &rsp, 1);
Linus Torvalds's avatar
Linus Torvalds committed
1763 1764 1765 1766 1767

	netif_start_queue(dev);
	return 0;
}

1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
static void get_tx_error(struct airo_info *ai, u32 fid)
{
	u16 status;

	if (bap_setup(ai, ai->fids[fid] & 0xffff, 4, BAP0) == SUCCESS) {
		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 */
			{ }
#if WIRELESS_EXT > 13
		/* 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 */
			bap_read(ai, (u16 *) junk, 0x18, BAP0);

			/* 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);
		}
#endif /* WIRELESS_EXT > 13 */
	}
}

1817
static void airo_end_xmit(struct net_device *dev) {
1818 1819 1820 1821 1822 1823 1824
	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;

1825 1826
	clear_bit(JOB_XMIT, &priv->flags);
	clear_bit(FLAG_PENDING_XMIT, &priv->flags);
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
	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);
}

Linus Torvalds's avatar
Linus Torvalds committed
1843 1844
static int airo_start_xmit(struct sk_buff *skb, struct net_device *dev) {
	s16 len;
1845
	int i, j;
1846
	struct airo_info *priv = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
1847
	u32 *fids = priv->fids;
Linus Torvalds's avatar
Linus Torvalds committed
1848

Linus Torvalds's avatar
Linus Torvalds committed
1849 1850 1851 1852
	if ( skb == NULL ) {
		printk( KERN_ERR "airo:  skb == NULL!!!\n" );
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1853

Linus Torvalds's avatar
Linus Torvalds committed
1854
	/* Find a vacant FID */
1855
	for( i = 0; i < MAX_FIDS / 2 && (fids[i] & 0xffff0000); i++ );
1856
	for( j = i + 1; j < MAX_FIDS / 2 && (fids[j] & 0xffff0000); j++ );
1857

1858 1859 1860 1861 1862 1863 1864
	if ( j >= MAX_FIDS / 2 ) {
		netif_stop_queue(dev);

		if (i == MAX_FIDS / 2) {
			priv->stats.tx_fifo_errors++;
			return 1;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1865
	}
1866 1867 1868 1869 1870 1871
	/* 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;
1872 1873 1874 1875 1876 1877 1878
	if (down_trylock(&priv->sem) != 0) {
		set_bit(FLAG_PENDING_XMIT, &priv->flags);
		netif_stop_queue(dev);
		set_bit(JOB_XMIT, &priv->flags);
		wake_up_interruptible(&priv->thr_wait);
	} else
		airo_end_xmit(dev);
1879 1880 1881
	return 0;
}

1882
static void airo_end_xmit11(struct net_device *dev) {
1883 1884 1885 1886 1887 1888
	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;
Linus Torvalds's avatar
Linus Torvalds committed
1889

1890 1891
	clear_bit(JOB_XMIT11, &priv->flags);
	clear_bit(FLAG_PENDING_XMIT11, &priv->flags);
1892 1893
	status = transmit_802_11_packet (priv, fids[fid], skb->data);
	up(&priv->sem);
Linus Torvalds's avatar
Linus Torvalds committed
1894

1895
	i = MAX_FIDS / 2;
Linus Torvalds's avatar
Linus Torvalds committed
1896 1897
	if ( status == SUCCESS ) {
		dev->trans_start = jiffies;
1898
		for (; i < MAX_FIDS && (priv->fids[i] & 0xffff0000); i++);
Linus Torvalds's avatar
Linus Torvalds committed
1899
	} else {
1900
		priv->fids[fid] &= 0xffff;
Linus Torvalds's avatar
Linus Torvalds committed
1901
		priv->stats.tx_window_errors++;
Linus Torvalds's avatar
Linus Torvalds committed
1902
	}
1903 1904
	if (i < MAX_FIDS)
		netif_wake_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1905
	dev_kfree_skb(skb);
1906 1907 1908 1909
}

static int airo_start_xmit11(struct sk_buff *skb, struct net_device *dev) {
	s16 len;
1910
	int i, j;
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
	struct airo_info *priv = dev->priv;
	u32 *fids = priv->fids;

	if ( skb == NULL ) {
		printk( KERN_ERR "airo:  skb == NULL!!!\n" );
		return 0;
	}

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

1923 1924 1925 1926 1927 1928 1929
	if ( j >= MAX_FIDS ) {
		netif_stop_queue(dev);

		if (i == MAX_FIDS) {
			priv->stats.tx_fifo_errors++;
			return 1;
		}
1930
	}
1931 1932 1933 1934 1935 1936
	/* 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;
1937 1938 1939 1940 1941 1942 1943
	if (down_trylock(&priv->sem) != 0) {
		set_bit(FLAG_PENDING_XMIT11, &priv->flags);
		netif_stop_queue(dev);
		set_bit(JOB_XMIT11, &priv->flags);
		wake_up_interruptible(&priv->thr_wait);
	} else
		airo_end_xmit11(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1944 1945 1946
	return 0;
}

1947 1948 1949 1950
static void airo_read_stats(struct airo_info *ai) {
	StatsRid stats_rid;
	u32 *vals = stats_rid.vals;

1951 1952 1953
	clear_bit(JOB_STATS, &ai->flags);
	readStatsRid(ai, &stats_rid, RID_STATS, 0);
	up(&ai->sem);
1954

1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
	ai->stats.rx_packets = vals[43] + vals[44] + vals[45];
	ai->stats.tx_packets = vals[39] + vals[40] + vals[41];
	ai->stats.rx_bytes = vals[92];
	ai->stats.tx_bytes = vals[91];
	ai->stats.rx_errors = vals[0] + vals[2] + vals[3] + vals[4];
	ai->stats.tx_errors = vals[42] + ai->stats.tx_fifo_errors;
	ai->stats.multicast = vals[43];
	ai->stats.collisions = vals[89];

	/* detailed rx_errors: */
	ai->stats.rx_length_errors = vals[3];
	ai->stats.rx_crc_errors = vals[4];
	ai->stats.rx_frame_errors = vals[2];
	ai->stats.rx_fifo_errors = vals[0];
1969 1970
}

Linus Torvalds's avatar
Linus Torvalds committed
1971 1972
struct net_device_stats *airo_get_stats(struct net_device *dev)
{
1973
	struct airo_info *local =  dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
1974

1975
	/* Get stats out of the card if available */
1976 1977 1978 1979 1980
	if (down_trylock(&local->sem) != 0) {
		set_bit(JOB_STATS, &local->flags);
		wake_up_interruptible(&local->thr_wait);
	} else
		airo_read_stats(local);
Linus Torvalds's avatar
Linus Torvalds committed
1981

1982
	return &local->stats;
Linus Torvalds's avatar
Linus Torvalds committed
1983 1984
}

1985
static void airo_set_promisc(struct airo_info *ai) {
Linus Torvalds's avatar
Linus Torvalds committed
1986
	Cmd cmd;
1987
	Resp rsp;
Linus Torvalds's avatar
Linus Torvalds committed
1988

1989 1990 1991 1992 1993 1994
	memset(&cmd, 0, sizeof(cmd));
	cmd.cmd=CMD_SETMODE;
	clear_bit(JOB_PROMISC, &ai->flags);
	cmd.parm0=(ai->flags&IFF_PROMISC) ? PROMISC : NOPROMISC;
	issuecommand(ai, &cmd, &rsp);
	up(&ai->sem);
1995 1996 1997 1998 1999 2000
}

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

	if ((dev->flags ^ ai->flags) & IFF_PROMISC) {
2001 2002 2003 2004 2005 2006
		change_bit(FLAG_PROMISC, &ai->flags);
		if (down_trylock(&ai->sem) != 0) {
			set_bit(JOB_PROMISC, &ai->flags);
			wake_up_interruptible(&ai->thr_wait);
		} else
			airo_set_promisc(ai);
Linus Torvalds's avatar
Linus Torvalds committed
2007
	}
Linus Torvalds's avatar
Linus Torvalds committed
2008 2009 2010 2011 2012 2013 2014 2015

	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)
{
2016
	struct airo_info *ai = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
2017
	struct sockaddr *addr = p;
2018
	Resp rsp;
Linus Torvalds's avatar
Linus Torvalds committed
2019

2020
	readConfigRid(ai, 1);
2021 2022
	memcpy (ai->config.macAddr, addr->sa_data, dev->addr_len);
	ai->need_commit = 1;
2023 2024 2025
	disable_MAC(ai, 1);
	writeConfigRid (ai, 1);
	enable_MAC(ai, &rsp, 1);
2026 2027 2028
	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);
Linus Torvalds's avatar
Linus Torvalds committed
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
	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;
}


Linus Torvalds's avatar
Linus Torvalds committed
2041
static int airo_close(struct net_device *dev) {
2042
	struct airo_info *ai = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
2043 2044

	netif_stop_queue(dev);
2045

2046 2047 2048 2049 2050 2051 2052
	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 */
2053
		set_bit(FLAG_RADIO_DOWN, &ai->flags);
2054
		disable_MAC(ai, 1);
2055
#endif
2056
		disable_interrupts( ai );
2057
	}
Linus Torvalds's avatar
Linus Torvalds committed
2058 2059 2060 2061 2062
	return 0;
}

static void del_airo_dev( struct net_device *dev );

Linus Torvalds's avatar
Linus Torvalds committed
2063
void stop_airo_card( struct net_device *dev, int freeres )
Linus Torvalds's avatar
Linus Torvalds committed
2064
{
2065
	struct airo_info *ai = dev->priv;
2066 2067
	disable_interrupts(ai);
	free_irq( dev->irq, dev );
Linus Torvalds's avatar
Linus Torvalds committed
2068 2069 2070
	takedown_proc_entry( dev, ai );
	if (ai->registered) {
		unregister_netdev( dev );
2071 2072
		if (ai->wifidev) {
			unregister_netdev(ai->wifidev);
2073
			free_netdev(ai->wifidev);
2074 2075
			ai->wifidev = 0;
		}
Linus Torvalds's avatar
Linus Torvalds committed
2076 2077
		ai->registered = 0;
	}
2078 2079 2080
	set_bit(JOB_DIE, &ai->flags);
	kill_proc(ai->thr_pid, SIGTERM, 1);
	wait_for_completion(&ai->thr_exited);
2081 2082 2083 2084
	if (ai->flash)
		kfree(ai->flash);
	if (ai->rssi)
		kfree(ai->rssi);
Linus Torvalds's avatar
Linus Torvalds committed
2085 2086 2087 2088
	if (freeres) {
		/* PCMCIA frees this stuff, so only for PCI and ISA */
	        release_region( dev->base_addr, 64 );
        }
2089 2090 2091 2092
#ifdef MICSUPPORT
	if (ai->tfm)
		crypto_free_tfm(ai->tfm);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2093
	del_airo_dev( dev );
2094
	free_netdev( dev );
Linus Torvalds's avatar
Linus Torvalds committed
2095 2096
}

Martin Dalecki's avatar
Martin Dalecki committed
2097 2098
EXPORT_SYMBOL(stop_airo_card);

Linus Torvalds's avatar
Linus Torvalds committed
2099
static int add_airo_dev( struct net_device *dev );
Linus Torvalds's avatar
Linus Torvalds committed
2100

2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
int wll_header_parse(struct sk_buff *skb, unsigned char *haddr)
{
	memcpy(haddr, skb->mac.raw + 10, ETH_ALEN);
	return ETH_ALEN;
}

static void wifi_setup(struct net_device *dev, struct net_device *ethdev)
{
	struct airo_info *ai = ethdev->priv;
	dev->priv = ai;
	dev->hard_header        = 0;
	dev->rebuild_header     = 0;
	dev->hard_header_cache  = 0;
	dev->header_cache_update= 0;

	dev->hard_header_parse  = wll_header_parse;
	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;
#ifdef WIRELESS_EXT
	dev->get_wireless_stats = airo_get_wireless_stats;
#if WIRELESS_EXT > 12
	dev->wireless_handlers = (struct iw_handler_def *)&airo_handler_def;
#endif /* WIRELESS_EXT > 12 */
#endif /* WIRELESS_EXT */
	dev->change_mtu = &airo_change_mtu;
	dev->open = &airo_open;
	dev->stop = &airo_close;
	dev->irq = ethdev->irq;
	dev->base_addr = ethdev->base_addr;

	dev->type               = ARPHRD_IEEE80211;
	dev->hard_header_len    = ETH_HLEN;
	dev->mtu                = 2312;
	dev->addr_len           = ETH_ALEN;
	memcpy(dev->dev_addr, ethdev->dev_addr, dev->addr_len);
	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 = (struct net_device*)kmalloc(sizeof *dev,GFP_KERNEL);
	if (!dev) return 0;
	memset(dev, 0, sizeof(*dev));

	strcpy(dev->name, "wifi%d");
	dev->priv = ai;
	wifi_setup(dev, ethdev);
	err = register_netdev(dev);
	if (err<0) {
		kfree(dev);
		return 0;
	}
	return dev;
}

Linus Torvalds's avatar
Linus Torvalds committed
2164 2165 2166 2167 2168
struct net_device *init_airo_card( unsigned short irq, int port, int is_pcmcia )
{
	struct net_device *dev;
	struct airo_info *ai;
	int i, rc;
Linus Torvalds's avatar
Linus Torvalds committed
2169

Linus Torvalds's avatar
Linus Torvalds committed
2170 2171 2172 2173 2174 2175
	/* Create the network device object. */
        dev = alloc_etherdev(sizeof(*ai));
        if (!dev) {
		printk(KERN_ERR "airo:  Couldn't alloc_etherdev\n");
		return NULL;
        }
Linus Torvalds's avatar
Linus Torvalds committed
2176 2177 2178 2179 2180
	if (dev_alloc_name(dev, dev->name) < 0) {
		printk(KERN_ERR "airo:  Couldn't get name!\n");
		goto err_out_free;
	}

Linus Torvalds's avatar
Linus Torvalds committed
2181
	ai = dev->priv;
2182
	ai->wifidev = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2183
	ai->registered = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2184 2185
        ai->dev = dev;
	ai->aux_lock = SPIN_LOCK_UNLOCKED;
2186 2187 2188
	sema_init(&ai->sem, 1);
	ai->need_commit = 0;
	ai->config.len = 0;
2189 2190 2191 2192 2193
	init_waitqueue_head (&ai->thr_wait);
	init_completion (&ai->thr_exited);
	ai->thr_pid = kernel_thread(airo_thread, dev, CLONE_FS | CLONE_FILES);
	if (ai->thr_pid < 0)
		goto err_out_free;
2194 2195 2196
#ifdef MICSUPPORT
	ai->tfm = NULL;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2197 2198
	rc = add_airo_dev( dev );
	if (rc)
2199
		goto err_out_thr;
Linus Torvalds's avatar
Linus Torvalds committed
2200

Linus Torvalds's avatar
Linus Torvalds committed
2201 2202 2203 2204 2205 2206 2207 2208
	/* The Airo-specific entries in the device structure. */
	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;
#ifdef WIRELESS_EXT
	dev->get_wireless_stats = airo_get_wireless_stats;
2209 2210 2211
#if WIRELESS_EXT > 12
	dev->wireless_handlers = (struct iw_handler_def *)&airo_handler_def;
#endif /* WIRELESS_EXT > 12 */
Linus Torvalds's avatar
Linus Torvalds committed
2212 2213 2214 2215 2216 2217
#endif /* WIRELESS_EXT */
	dev->change_mtu = &airo_change_mtu;
	dev->open = &airo_open;
	dev->stop = &airo_close;
	dev->irq = irq;
	dev->base_addr = port;
Linus Torvalds's avatar
Linus Torvalds committed
2218 2219

	rc = request_irq( dev->irq, airo_interrupt, SA_SHIRQ, dev->name, dev );
Linus Torvalds's avatar
Linus Torvalds committed
2220 2221
	if (rc) {
		printk(KERN_ERR "airo: register interrupt %d failed, rc %d\n", irq, rc );
Linus Torvalds's avatar
Linus Torvalds committed
2222
		goto err_out_unlink;
Linus Torvalds's avatar
Linus Torvalds committed
2223 2224 2225 2226 2227 2228 2229
	}
	if (!is_pcmcia) {
		if (!request_region( dev->base_addr, 64, dev->name )) {
			rc = -EBUSY;
			goto err_out_irq;
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
2230

2231 2232 2233 2234 2235 2236 2237
	if (probe) {
		if ( setup_card( ai, dev->dev_addr ) != SUCCESS ) {
			printk( KERN_ERR "airo: MAC could not be enabled\n" );
			rc = -EIO;
			goto err_out_res;
		}
	} else {
2238
		ai->bap_read = fast_bap_read;
2239
		set_bit(FLAG_FLASHING, &ai->flags);
2240
	}
Linus Torvalds's avatar
Linus Torvalds committed
2241

Linus Torvalds's avatar
Linus Torvalds committed
2242 2243 2244
	rc = register_netdev(dev);
	if (rc)
		goto err_out_res;
2245
	ai->wifidev = init_wifidev(ai, dev);
Linus Torvalds's avatar
Linus Torvalds committed
2246 2247

	ai->registered = 1;
Linus Torvalds's avatar
Linus Torvalds committed
2248 2249 2250 2251 2252 2253
	printk( KERN_INFO "airo: MAC enabled %s %x:%x:%x:%x:%x:%x\n",
		dev->name,
		dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
		dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5] );

	/* Allocate the transmit buffers */
2254 2255 2256
	if (probe)
		for( i = 0; i < MAX_FIDS; i++ )
			ai->fids[i] = transmit_allocate(ai,2312,i>=MAX_FIDS/2);
Linus Torvalds's avatar
Linus Torvalds committed
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269

	setup_proc_entry( dev, dev->priv ); /* XXX check for failure */
	netif_start_queue(dev);
	SET_MODULE_OWNER(dev);
	return dev;

err_out_res:
	if (!is_pcmcia)
	        release_region( dev->base_addr, 64 );
err_out_irq:
	free_irq(dev->irq, dev);
err_out_unlink:
	del_airo_dev(dev);
2270 2271 2272 2273
err_out_thr:
	set_bit(JOB_DIE, &ai->flags);
	kill_proc(ai->thr_pid, SIGTERM, 1);
	wait_for_completion(&ai->thr_exited);
Linus Torvalds's avatar
Linus Torvalds committed
2274 2275 2276 2277 2278
err_out_free:
	kfree(dev);
	return NULL;
}

Martin Dalecki's avatar
Martin Dalecki committed
2279 2280 2281
EXPORT_SYMBOL(init_airo_card);

static int waitbusy (struct airo_info *ai) {
Linus Torvalds's avatar
Linus Torvalds committed
2282 2283 2284 2285 2286
	int delay = 0;
	while ((IN4500 (ai, COMMAND) & COMMAND_BUSY) & (delay < 10000)) {
		udelay (10);
		if (++delay % 20)
			OUT4500(ai, EVACK, EV_CLEARCOMMANDBUSY);
Linus Torvalds's avatar
Linus Torvalds committed
2287
	}
Linus Torvalds's avatar
Linus Torvalds committed
2288 2289 2290 2291
	return delay < 10000;
}

int reset_airo_card( struct net_device *dev ) {
Linus Torvalds's avatar
Linus Torvalds committed
2292
	int i;
2293
	struct airo_info *ai = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
2294

2295 2296 2297

	if (down_interruptible(&ai->sem))
		return -1;
Linus Torvalds's avatar
Linus Torvalds committed
2298 2299 2300 2301 2302 2303 2304
	waitbusy (ai);
	OUT4500(ai,COMMAND,CMD_SOFTRESET);
	set_current_state (TASK_UNINTERRUPTIBLE);
	schedule_timeout (HZ/5);
	waitbusy (ai);
	set_current_state (TASK_UNINTERRUPTIBLE);
	schedule_timeout (HZ/5);
2305
	if ( setup_card(ai, dev->dev_addr ) != SUCCESS ) {
Linus Torvalds's avatar
Linus Torvalds committed
2306
		printk( KERN_ERR "airo: MAC could not be enabled\n" );
2307
		up(&ai->sem);
Linus Torvalds's avatar
Linus Torvalds committed
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320
		return -1;
	} else {
		printk( KERN_INFO "airo: MAC enabled %s %x:%x:%x:%x:%x:%x\n",
			dev->name,
			dev->dev_addr[0],
			dev->dev_addr[1],
			dev->dev_addr[2],
			dev->dev_addr[3],
			dev->dev_addr[4],
			dev->dev_addr[5]
			);
		/* Allocate the transmit buffers */
		for( i = 0; i < MAX_FIDS; i++ )
2321
			ai->fids[i] = transmit_allocate(ai,2312,i>=MAX_FIDS/2);
Linus Torvalds's avatar
Linus Torvalds committed
2322 2323 2324
	}
	enable_interrupts( ai );
	netif_wake_queue(dev);
2325
	up(&ai->sem);
Linus Torvalds's avatar
Linus Torvalds committed
2326 2327 2328
	return 0;
}

Martin Dalecki's avatar
Martin Dalecki committed
2329 2330
EXPORT_SYMBOL(reset_airo_card);

2331 2332 2333 2334 2335 2336
#if WIRELESS_EXT > 13
static void airo_send_event(struct net_device *dev) {
	struct airo_info *ai = dev->priv;
	union iwreq_data wrqu;
	StatusRid status_rid;

2337 2338 2339 2340 2341 2342 2343
	clear_bit(JOB_EVENT, &ai->flags);
	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;
2344

2345 2346
	/* Send event to user space */
	wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
2347
}
2348
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2349

2350 2351 2352 2353 2354 2355 2356
static int airo_thread(void *data) {
	struct net_device *dev = data;
	struct airo_info *ai = dev->priv;
	int locked;
	
	daemonize("%s", dev->name);
	allow_signal(SIGTERM);
2357

2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418
	while(1) {
		if (signal_pending(current))
			flush_signals(current);

		/* make swsusp happy with our thread */
		if (current->flags & PF_FREEZE)
			refrigerator(PF_IOTHREAD);

		if (test_bit(JOB_DIE, &ai->flags))
			break;

		if (ai->flags & JOB_MASK) {
			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);
				if (ai->flags & JOB_MASK)
					break;
				if (ai->expires) {
					if (time_after_eq(jiffies,ai->expires)){
						set_bit(JOB_AUTOWEP,&ai->flags);
						break;
					}
					if (!signal_pending(current)) {
						schedule_timeout(ai->expires - jiffies);
						continue;
					}
				} else if (!signal_pending(current)) {
					schedule();
					continue;
				}
				break;
			}
			current->state = TASK_RUNNING;
			remove_wait_queue(&ai->thr_wait, &wait);
			locked = 1;
		}

		if (locked)
			continue;

		if (test_bit(JOB_DIE, &ai->flags)) {
			up(&ai->sem);
			break;
		}

		if (test_bit(FLAG_FLASHING, &ai->flags)) {
			up(&ai->sem);
			continue;
		}

		if (test_bit(JOB_XMIT, &ai->flags))
			airo_end_xmit(dev);
		else if (test_bit(JOB_XMIT11, &ai->flags))
			airo_end_xmit11(dev);
		else if (test_bit(JOB_STATS, &ai->flags))
			airo_read_stats(ai);
2419 2420
		else if (test_bit(JOB_WSTATS, &ai->flags))
			airo_read_wireless_stats(ai);
2421 2422
		else if (test_bit(JOB_PROMISC, &ai->flags))
			airo_set_promisc(ai);
2423
#ifdef MICSUPPORT
2424 2425
		else if (test_bit(JOB_MIC, &ai->flags))
			micinit(ai);
2426
#endif
2427 2428 2429 2430 2431 2432
#if WIRELESS_EXT > 13
		else if (test_bit(JOB_EVENT, &ai->flags))
			airo_send_event(dev);
#endif
		else if (test_bit(JOB_AUTOWEP, &ai->flags))
			timer_func(dev);
2433
	}
2434
	complete_and_exit (&ai->thr_exited, 0);
2435 2436
}

Jeff Garzik's avatar
Jeff Garzik committed
2437
static irqreturn_t airo_interrupt ( int irq, void* dev_id, struct pt_regs *regs) {
Linus Torvalds's avatar
Linus Torvalds committed
2438 2439 2440
	struct net_device *dev = (struct net_device *)dev_id;
	u16 status;
	u16 fid;
2441
	struct airo_info *apriv = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
2442
	u16 savedInterrupts = 0;
Jeff Garzik's avatar
Jeff Garzik committed
2443
	int handled = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2444

Linus Torvalds's avatar
Linus Torvalds committed
2445
	if (!netif_device_present(dev))
Jeff Garzik's avatar
Jeff Garzik committed
2446
		return IRQ_NONE;
Linus Torvalds's avatar
Linus Torvalds committed
2447 2448 2449

	for (;;) {
		status = IN4500( apriv, EVSTAT );
2450
		if ( !(status & STATUS_INTS) || status == 0xffff ) break;
Linus Torvalds's avatar
Linus Torvalds committed
2451

Jeff Garzik's avatar
Jeff Garzik committed
2452 2453
		handled = 1;

Linus Torvalds's avatar
Linus Torvalds committed
2454 2455 2456 2457 2458 2459 2460 2461 2462 2463
		if ( status & EV_AWAKE ) {
			OUT4500( apriv, EVACK, EV_AWAKE );
			OUT4500( apriv, EVACK, EV_AWAKE );
		}

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

2464 2465
		if ( status & EV_MIC ) {
			OUT4500( apriv, EVACK, EV_MIC );
2466 2467
#ifdef MICSUPPORT
			if (test_bit(FLAG_MIC_CAPABLE, &apriv->flags)) {
Jeff Garzik's avatar
Jeff Garzik committed
2468 2469
				set_bit(JOB_MIC, &apriv->flags);
				wake_up_interruptible(&apriv->thr_wait);
2470 2471
			}
#endif
2472
		}
Linus Torvalds's avatar
Linus Torvalds committed
2473
		if ( status & EV_LINK ) {
2474 2475 2476
#if WIRELESS_EXT > 13
			union iwreq_data	wrqu;
#endif /* WIRELESS_EXT > 13 */
Linus Torvalds's avatar
Linus Torvalds committed
2477 2478 2479 2480 2481 2482 2483
			/* 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: */
Linus Torvalds's avatar
Linus Torvalds committed
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511
#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) */
#define ASSOCIATED 0x0400 /* Assocatied */
#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 */
Linus Torvalds's avatar
Linus Torvalds committed
2512
			if (newStatus != ASSOCIATED) {
2513 2514 2515
				if (auto_wep && !apriv->expires) {
					apriv->expires = RUN_AT(3*HZ);
					wake_up_interruptible(&apriv->thr_wait);
Linus Torvalds's avatar
Linus Torvalds committed
2516
				}
2517 2518
			} else {
				struct task_struct *task = apriv->task;
2519 2520
				if (auto_wep)
					apriv->expires = 0;
2521 2522
				if (task)
					wake_up_process (task);
2523 2524
				set_bit(FLAG_UPDATE_UNI, &apriv->flags);
				set_bit(FLAG_UPDATE_MULTI, &apriv->flags);
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544
			}
#if WIRELESS_EXT > 13
			/* Question : is ASSOCIATED the only status
			 * that is valid ? We want to catch handover
			 * and reassociations as valid status
			 * Jean II */
			if(newStatus == ASSOCIATED) {
				if (apriv->scan_timestamp) {
					/* 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(dev, SIOCGIWSCAN, &wrqu, NULL);
					apriv->scan_timestamp = 0;
				}
2545 2546 2547 2548 2549
				if (down_trylock(&apriv->sem) != 0) {
					set_bit(JOB_EVENT, &apriv->flags);
					wake_up_interruptible(&apriv->thr_wait);
				} else
					airo_send_event(dev);
2550 2551 2552 2553 2554 2555
			} else {
				memset(wrqu.ap_addr.sa_data, '\0', ETH_ALEN);
				wrqu.ap_addr.sa_family = ARPHRD_ETHER;

				/* Send event to user space */
				wireless_send_event(dev, SIOCGIWAP, &wrqu,NULL);
Linus Torvalds's avatar
Linus Torvalds committed
2556
			}
2557
#endif /* WIRELESS_EXT > 13 */
Linus Torvalds's avatar
Linus Torvalds committed
2558
		}
Linus Torvalds's avatar
Linus Torvalds committed
2559 2560 2561 2562 2563

		/* Check to see if there is something to receive */
		if ( status & EV_RX  ) {
			struct sk_buff *skb = NULL;
			u16 fc, len, hdrlen = 0;
2564
#pragma pack(1)
Linus Torvalds's avatar
Linus Torvalds committed
2565 2566 2567
			struct {
				u16 status, len;
				u8 rssi[2];
2568 2569 2570
				u8 rate;
				u8 freq;
				u16 tmp[4];
Linus Torvalds's avatar
Linus Torvalds committed
2571
			} hdr;
2572 2573 2574 2575
#pragma pack()
			u16 gap;
			u16 tmpbuf[4];
			u16 *buffer;
Linus Torvalds's avatar
Linus Torvalds committed
2576 2577 2578 2579

			fid = IN4500( apriv, RXFID );

			/* Get the packet length */
2580
			if (test_bit(FLAG_802_11, &apriv->flags)) {
Linus Torvalds's avatar
Linus Torvalds committed
2581 2582 2583 2584 2585
				bap_setup (apriv, fid, 4, BAP0);
				bap_read (apriv, (u16*)&hdr, sizeof(hdr), BAP0);
				/* Bad CRC. Ignore packet */
				if (le16_to_cpu(hdr.status) & 2)
					hdr.len = 0;
2586 2587
				if (apriv->wifidev == NULL)
					hdr.len = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2588
			} else {
2589 2590
				bap_setup (apriv, fid, 0x36, BAP0);
				bap_read (apriv, (u16*)&hdr.len, 2, BAP0);
Linus Torvalds's avatar
Linus Torvalds committed
2591 2592
			}
			len = le16_to_cpu(hdr.len);
Linus Torvalds's avatar
Linus Torvalds committed
2593

Linus Torvalds's avatar
Linus Torvalds committed
2594 2595
			if (len > 2312) {
				printk( KERN_ERR "airo: Bad size %d\n", len );
2596
				goto badrx;
Linus Torvalds's avatar
Linus Torvalds committed
2597
			}
2598 2599
			if (len == 0)
				goto badrx;
Linus Torvalds's avatar
Linus Torvalds committed
2600

2601 2602 2603 2604 2605 2606 2607
			if (test_bit(FLAG_802_11, &apriv->flags)) {
				bap_read (apriv, (u16*)&fc, sizeof(fc), BAP0);
				fc = le16_to_cpu(fc);
				switch (fc & 0xc) {
					case 4:
						if ((fc & 0xe0) == 0xc0)
							hdrlen = 10;
2608
						else
2609 2610 2611 2612 2613 2614
							hdrlen = 16;
						break;
					case 8:
						if ((fc&0x300)==0x300){
							hdrlen = 30;
							break;
2615
						}
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647
					default:
						hdrlen = 24;
				}
			} else
				hdrlen = ETH_ALEN * 2;

			skb = dev_alloc_skb( len + hdrlen + 2 );
			if ( !skb ) {
				apriv->stats.rx_dropped++;
				goto badrx;
			}
			buffer = (u16*)skb_put (skb, len + hdrlen);
			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);

				bap_read (apriv, &gap, sizeof(gap), BAP0);
				gap = le16_to_cpu(gap);
				if (gap) {
					if (gap <= 8)
						bap_read (apriv, tmpbuf, gap, BAP0);
					else
						printk(KERN_ERR "airo: gaplen too big. Problems will follow...\n");
				}
				bap_read (apriv, buffer + hdrlen/2, len, BAP0);
			} else {
#ifdef MICSUPPORT
				MICBuffer micbuf;
#endif
				bap_read (apriv, buffer, ETH_ALEN*2, BAP0);
2648
#ifdef MICSUPPORT
2649 2650 2651 2652 2653 2654 2655 2656 2657 2658
				if (apriv->micstats.enabled) {
					bap_read (apriv,(u16*)&micbuf,sizeof(micbuf),BAP0);
					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);
2659
					}
2660
				}
2661
#endif
2662 2663 2664 2665 2666
				bap_read(apriv,buffer+ETH_ALEN,len,BAP0);
#ifdef MICSUPPORT
				if (decapsulate(apriv,&micbuf,(etherHead*)buffer,len)) {
badmic:
					dev_kfree_skb_irq (skb);
2667 2668 2669
#else
				if (0) {
#endif
2670 2671 2672
badrx:
					OUT4500( apriv, EVACK, EV_RX);
					goto exitrx;
Linus Torvalds's avatar
Linus Torvalds committed
2673
				}
2674
			}
2675
#if WIRELESS_EXT > 15
2676
#ifdef IW_WIRELESS_SPY		/* defined in iw_handler.h */
2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695
			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);
					bap_read (apriv, (u16*)hdr.rssi, 2, BAP0);
				} 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;
				wstats.updated = 3;	
				/* Update spy records */
				wireless_spy_update(dev, sa, &wstats);
			}
2696 2697
#endif /* IW_WIRELESS_SPY */
#else /* WIRELESS_EXT > 15 */
Linus Torvalds's avatar
Linus Torvalds committed
2698
#ifdef WIRELESS_SPY
2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710
			if (apriv->spy_number > 0) {
				int i;
				char *sa;

				sa = (char*)buffer + (test_bit(FLAG_802_11, &apriv->flags) ? 10 : 6);

				for (i=0; i<apriv->spy_number; i++)
					if (!memcmp(sa,apriv->spy_address[i],ETH_ALEN))
					{
						if (!test_bit(FLAG_802_11, &apriv->flags)) {
							bap_setup (apriv, fid, 8, BAP0);
							bap_read (apriv, (u16*)hdr.rssi, 2, BAP0);
Linus Torvalds's avatar
Linus Torvalds committed
2711
						}
2712 2713 2714 2715 2716 2717 2718 2719 2720 2721
						apriv->spy_stat[i].qual = hdr.rssi[0];
						if (apriv->rssi)
							apriv->spy_stat[i].level = 0x100 - apriv->rssi[hdr.rssi[1]].rssidBm;
						else
							apriv->spy_stat[i].level = (hdr.rssi[1] + 321) / 2;
						apriv->spy_stat[i].noise = 0;
						apriv->spy_stat[i].updated = 3;
						break;
					}
			}
Linus Torvalds's avatar
Linus Torvalds committed
2722
#endif /* WIRELESS_SPY  */
2723
#endif /* WIRELESS_EXT > 15 */
2724
			OUT4500( apriv, EVACK, EV_RX);
2725

2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
			if (test_bit(FLAG_802_11, &apriv->flags)) {
				skb->mac.raw = skb->data;
				skb->pkt_type = PACKET_OTHERHOST;
				skb->dev = apriv->wifidev;
				skb->protocol = htons(ETH_P_802_2);
			} else {
				skb->dev = dev;
				skb->protocol = eth_type_trans(skb,dev);
			}
			skb->dev->last_rx = jiffies;
			skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds's avatar
Linus Torvalds committed
2737

2738
			netif_rx( skb );
Linus Torvalds's avatar
Linus Torvalds committed
2739
		}
2740
exitrx:
Linus Torvalds's avatar
Linus Torvalds committed
2741

Linus Torvalds's avatar
Linus Torvalds committed
2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754
		/* Check to see if a packet has been transmitted */
		if (  status & ( EV_TX|EV_TXEXC ) ) {
			int i;
			int len = 0;
			int index = -1;

			fid = IN4500(apriv, TXCOMPLFID);

			for( i = 0; i < MAX_FIDS; i++ ) {
				if ( ( apriv->fids[i] & 0xffff ) == fid ) {
					len = apriv->fids[i] >> 16;
					index = i;
				}
Linus Torvalds's avatar
Linus Torvalds committed
2755
			}
2756 2757 2758
			if (index != -1) {
				if (status & EV_TXEXC)
					get_tx_error(apriv, index);
2759 2760 2761
				OUT4500( apriv, EVACK, status & (EV_TX | EV_TXEXC));
				/* Set up to be used again */
				apriv->fids[index] &= 0xffff;
2762
				if (index < MAX_FIDS / 2) {
2763
					if (!test_bit(FLAG_PENDING_XMIT, &apriv->flags))
2764 2765
						netif_wake_queue(dev);
				} else {
2766
					if (!test_bit(FLAG_PENDING_XMIT11, &apriv->flags))
2767 2768
						netif_wake_queue(apriv->wifidev);
				}
2769 2770
			} else {
				OUT4500( apriv, EVACK, status & (EV_TX | EV_TXEXC));
Linus Torvalds's avatar
Linus Torvalds committed
2771 2772
				printk( KERN_ERR "airo: Unallocated FID was used to xmit\n" );
			}
Linus Torvalds's avatar
Linus Torvalds committed
2773
		}
Linus Torvalds's avatar
Linus Torvalds committed
2774 2775 2776
		if ( status & ~STATUS_INTS & ~IGNORE_INTS )
			printk( KERN_WARNING "airo: Got weird status %x\n",
				status & ~STATUS_INTS & ~IGNORE_INTS );
Linus Torvalds's avatar
Linus Torvalds committed
2777
	}
Linus Torvalds's avatar
Linus Torvalds committed
2778 2779 2780 2781

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

Linus Torvalds's avatar
Linus Torvalds committed
2782
	/* done.. */
Jeff Garzik's avatar
Jeff Garzik committed
2783
	return IRQ_RETVAL(handled);
Linus Torvalds's avatar
Linus Torvalds committed
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805
}

/*
 *  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 ( !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;
Linus Torvalds's avatar
Linus Torvalds committed
2806

Linus Torvalds's avatar
Linus Torvalds committed
2807 2808 2809 2810 2811 2812 2813 2814 2815
	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;
}

2816
static int enable_MAC( struct airo_info *ai, Resp *rsp, int lock ) {
2817
	int rc;
Linus Torvalds's avatar
Linus Torvalds committed
2818 2819
        Cmd cmd;

2820 2821 2822 2823 2824 2825
	/* 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 */
2826
	if (ai->flags & FLAG_RADIO_MASK) return SUCCESS;
Linus Torvalds's avatar
Linus Torvalds committed
2827 2828
	memset(&cmd, 0, sizeof(cmd));
	cmd.cmd = MAC_ENABLE;
2829
	if (!lock)
2830 2831 2832 2833 2834 2835 2836
		return issuecommand(ai, &cmd, rsp);

	if (down_interruptible(&ai->sem))
		return -ERESTARTSYS;
	rc = issuecommand(ai, &cmd, rsp);
	up(&ai->sem);
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
2837 2838
}

2839
static void disable_MAC( struct airo_info *ai, int lock ) {
Linus Torvalds's avatar
Linus Torvalds committed
2840 2841 2842 2843 2844
        Cmd cmd;
	Resp rsp;

	memset(&cmd, 0, sizeof(cmd));
	cmd.cmd = MAC_DISABLE; // disable in case already enabled
2845
	if (!lock) {
2846 2847 2848 2849 2850 2851 2852 2853
		issuecommand(ai, &cmd, &rsp);
		return;
	}

	if (down_interruptible(&ai->sem))
		return;
	issuecommand(ai, &cmd, &rsp);
	up(&ai->sem);
Linus Torvalds's avatar
Linus Torvalds committed
2854 2855 2856 2857 2858 2859 2860 2861 2862
}

static void enable_interrupts( struct airo_info *ai ) {
	/* Reset the status register */
	u16 status = IN4500( ai, EVSTAT );
	OUT4500( ai, EVACK, status );
	/* Enable the interrupts */
	OUT4500( ai, EVINTEN, STATUS_INTS );
	/* Note there is a race condition between the last two lines that
2863
	   I don't know how to get rid of right now... */
Linus Torvalds's avatar
Linus Torvalds committed
2864 2865 2866 2867 2868 2869
}

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

2870
static u16 setup_card(struct airo_info *ai, u8 *mac)
Linus Torvalds's avatar
Linus Torvalds committed
2871
{
Linus Torvalds's avatar
Linus Torvalds committed
2872
	Cmd cmd;
Linus Torvalds's avatar
Linus Torvalds committed
2873 2874 2875 2876 2877 2878 2879 2880 2881
	Resp rsp;
	int status;
	int i;
	SsidRid mySsid;
	u16 lastindex;
	WepKeyRid wkr;
	int rc;

	memset( &mySsid, 0, sizeof( mySsid ) );
Linus Torvalds's avatar
Linus Torvalds committed
2882 2883 2884 2885
	if (ai->flash) {
		kfree (ai->flash);
		ai->flash = NULL;
	}
Linus Torvalds's avatar
Linus Torvalds committed
2886 2887 2888 2889

	/* The NOP is the first step in getting the card going */
	cmd.cmd = NOP;
	cmd.parm0 = cmd.parm1 = cmd.parm2 = 0;
2890 2891 2892 2893
	if (down_interruptible(&ai->sem))
		return ERROR;
	if ( issuecommand( ai, &cmd, &rsp ) != SUCCESS ) {
		up(&ai->sem);
Linus Torvalds's avatar
Linus Torvalds committed
2894 2895 2896 2897
		return ERROR;
	}
	memset(&cmd, 0, sizeof(cmd));
	cmd.cmd = MAC_DISABLE; // disable in case already enabled
2898 2899
	if ( issuecommand( ai, &cmd, &rsp ) != SUCCESS ) {
		up(&ai->sem);
Linus Torvalds's avatar
Linus Torvalds committed
2900 2901
		return ERROR;
	}
Linus Torvalds's avatar
Linus Torvalds committed
2902

Linus Torvalds's avatar
Linus Torvalds committed
2903 2904
	// Let's figure out if we need to use the AUX port
	cmd.cmd = CMD_ENABLEAUX;
2905 2906
	if (issuecommand(ai, &cmd, &rsp) != SUCCESS) {
		up(&ai->sem);
Linus Torvalds's avatar
Linus Torvalds committed
2907 2908 2909 2910 2911 2912 2913 2914 2915 2916
		printk(KERN_ERR "airo: Error checking for AUX port\n");
		return ERROR;
	}
	if (!aux_bap || rsp.status & 0xff00) {
		ai->bap_read = fast_bap_read;
		printk(KERN_DEBUG "airo: Doing fast bap_reads\n");
	} else {
		ai->bap_read = aux_bap_read;
		printk(KERN_DEBUG "airo: Doing AUX bap_reads\n");
	}
2917 2918
	up(&ai->sem);
	if (ai->config.len == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
2919
		tdsRssiRid rssi_rid;
2920
		CapabilityRid cap_rid;
Linus Torvalds's avatar
Linus Torvalds committed
2921

Linus Torvalds's avatar
Linus Torvalds committed
2922
		// general configuration (read/modify/write)
2923
		status = readConfigRid(ai, 1);
Linus Torvalds's avatar
Linus Torvalds committed
2924
		if ( status != SUCCESS ) return ERROR;
Linus Torvalds's avatar
Linus Torvalds committed
2925

2926 2927 2928
		status = readCapabilityRid(ai, &cap_rid);
		if ( status != SUCCESS ) return ERROR;

2929
		status = PC4500_readrid(ai,RID_RSSI,&rssi_rid,sizeof(rssi_rid),1);
Linus Torvalds's avatar
Linus Torvalds committed
2930 2931 2932 2933 2934 2935 2936 2937 2938
		if ( status == SUCCESS ) {
			if (ai->rssi || (ai->rssi = kmalloc(512, GFP_KERNEL)) != NULL)
				memcpy(ai->rssi, (u8*)&rssi_rid + 2, 512);
		}
		else {
			if (ai->rssi) {
				kfree(ai->rssi);
				ai->rssi = NULL;
			}
2939
			if (cap_rid.softCap & 8)
2940
				ai->config.rmode |= RXMODE_NORMALIZED_RSSI;
Linus Torvalds's avatar
Linus Torvalds committed
2941
			else
Linus Torvalds's avatar
Linus Torvalds committed
2942
				printk(KERN_WARNING "airo: unknown received signal level scale\n");
Linus Torvalds's avatar
Linus Torvalds committed
2943
		}
2944
		ai->config.opmode = adhoc ? MODE_STA_IBSS : MODE_STA_ESS;
2945 2946
		ai->config.authType = AUTH_OPEN;
		ai->config.modulation = MOD_CCK;
Linus Torvalds's avatar
Linus Torvalds committed
2947

2948
#ifdef MICSUPPORT
2949 2950
		if ((cap_rid.len>=sizeof(cap_rid)) && (cap_rid.extSoftCap&1) &&
		    (micsetup(ai) == SUCCESS)) {
2951
			ai->config.opmode |= MODE_MIC;
2952
			set_bit(FLAG_MIC_CAPABLE, &ai->flags);
2953 2954 2955
		}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
2956
		/* Save off the MAC */
2957
		for( i = 0; i < ETH_ALEN; i++ ) {
2958
			mac[i] = ai->config.macAddr[i];
Linus Torvalds's avatar
Linus Torvalds committed
2959 2960
		}

Linus Torvalds's avatar
Linus Torvalds committed
2961
		/* Check to see if there are any insmod configured
Linus Torvalds's avatar
Linus Torvalds committed
2962 2963 2964
		   rates to add */
		if ( rates ) {
			int i = 0;
2965
			if ( rates[0] ) memset(ai->config.rates,0,sizeof(ai->config.rates));
Linus Torvalds's avatar
Linus Torvalds committed
2966
			for( i = 0; i < 8 && rates[i]; i++ ) {
2967
				ai->config.rates[i] = rates[i];
Linus Torvalds's avatar
Linus Torvalds committed
2968
			}
Linus Torvalds's avatar
Linus Torvalds committed
2969 2970 2971 2972
		}
		if ( basic_rate > 0 ) {
			int i;
			for( i = 0; i < 8; i++ ) {
2973 2974 2975
				if ( ai->config.rates[i] == basic_rate ||
				     !ai->config.rates ) {
					ai->config.rates[i] = basic_rate | 0x80;
Linus Torvalds's avatar
Linus Torvalds committed
2976 2977 2978 2979
					break;
				}
			}
		}
2980
		ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
2981
	}
Linus Torvalds's avatar
Linus Torvalds committed
2982

Linus Torvalds's avatar
Linus Torvalds committed
2983 2984
	/* Setup the SSIDs if present */
	if ( ssids[0] ) {
2985
		int i;
Linus Torvalds's avatar
Linus Torvalds committed
2986 2987
		for( i = 0; i < 3 && ssids[i]; i++ ) {
			mySsid.ssids[i].len = strlen(ssids[i]);
Linus Torvalds's avatar
Linus Torvalds committed
2988
			if ( mySsid.ssids[i].len > 32 )
Linus Torvalds's avatar
Linus Torvalds committed
2989 2990 2991 2992
				mySsid.ssids[i].len = 32;
			memcpy(mySsid.ssids[i].ssid, ssids[i],
			       mySsid.ssids[i].len);
		}
2993
		mySsid.len = sizeof(mySsid);
Linus Torvalds's avatar
Linus Torvalds committed
2994
	}
Linus Torvalds's avatar
Linus Torvalds committed
2995

2996
	status = writeConfigRid(ai, 1);
Linus Torvalds's avatar
Linus Torvalds committed
2997
	if ( status != SUCCESS ) return ERROR;
Linus Torvalds's avatar
Linus Torvalds committed
2998

Linus Torvalds's avatar
Linus Torvalds committed
2999 3000 3001
	/* Set up the SSID list */
	status = writeSsidRid(ai, &mySsid);
	if ( status != SUCCESS ) return ERROR;
Linus Torvalds's avatar
Linus Torvalds committed
3002

3003
	status = enable_MAC(ai, &rsp, 1);
3004 3005 3006 3007
	if ( status != SUCCESS || (rsp.status & 0xFF00) != 0) {
		printk( KERN_ERR "airo: Bad MAC enable reason = %x, rid = %x, offset = %d\n", rsp.rsp0, rsp.rsp1, rsp.rsp2 );
		return ERROR;
	}
3008

Linus Torvalds's avatar
Linus Torvalds committed
3009 3010 3011 3012 3013 3014 3015 3016 3017
	/* Grab the initial wep key, we gotta save it for auto_wep */
	rc = readWepKeyRid(ai, &wkr, 1);
	if (rc == SUCCESS) do {
		lastindex = wkr.kindex;
		if (wkr.kindex == 0xffff) {
			ai->defindex = wkr.mac[0];
		}
		rc = readWepKeyRid(ai, &wkr, 0);
	} while(lastindex != wkr.kindex);
Linus Torvalds's avatar
Linus Torvalds committed
3018

3019 3020 3021
	if (auto_wep) {
		ai->expires = RUN_AT(3*HZ);
		wake_up_interruptible(&ai->thr_wait);
Linus Torvalds's avatar
Linus Torvalds committed
3022
	}
Linus Torvalds's avatar
Linus Torvalds committed
3023

3024
	return SUCCESS;
Linus Torvalds's avatar
Linus Torvalds committed
3025 3026
}

3027
static u16 issuecommand(struct airo_info *ai, Cmd *pCmd, Resp *pRsp) {
Linus Torvalds's avatar
Linus Torvalds committed
3028
        // Im really paranoid about letting it run forever!
Linus Torvalds's avatar
Linus Torvalds committed
3029
	int max_tries = 600000;
3030
	u16 cmd;
Linus Torvalds's avatar
Linus Torvalds committed
3031 3032 3033 3034 3035

	OUT4500(ai, PARAM0, pCmd->parm0);
	OUT4500(ai, PARAM1, pCmd->parm1);
	OUT4500(ai, PARAM2, pCmd->parm2);
	OUT4500(ai, COMMAND, pCmd->cmd);
3036 3037 3038 3039 3040
	while ( max_tries-- && (IN4500(ai, EVSTAT) & EV_CMD) == 0 &&
		(cmd = IN4500(ai, COMMAND)) != 0 )
			if (cmd == pCmd->cmd)
				// PC4500 didn't notice command, try again
				OUT4500(ai, COMMAND, pCmd->cmd);
Linus Torvalds's avatar
Linus Torvalds committed
3041
	if ( max_tries == -1 ) {
Linus Torvalds's avatar
Linus Torvalds committed
3042
		printk( KERN_ERR
Linus Torvalds's avatar
Linus Torvalds committed
3043
			"airo: Max tries exceeded when issueing command\n" );
Linus Torvalds's avatar
Linus Torvalds committed
3044
                return ERROR;
Linus Torvalds's avatar
Linus Torvalds committed
3045
	}
3046

3047 3048 3049 3050 3051 3052 3053 3054 3055
	while (max_tries-- && (IN4500(ai, EVSTAT) & EV_CMD) == 0) {
		if (!in_atomic() && (max_tries & 255) == 0)
			schedule();
	}
	if ( max_tries == -1 ) {
		printk( KERN_ERR
			"airo: Max tries exceeded waiting for command\n" );
                return ERROR;
	}
Linus Torvalds's avatar
Linus Torvalds committed
3056 3057 3058 3059 3060
	// command completed
	pRsp->status = IN4500(ai, STATUS);
	pRsp->rsp0 = IN4500(ai, RESP0);
	pRsp->rsp1 = IN4500(ai, RESP1);
	pRsp->rsp2 = IN4500(ai, RESP2);
Linus Torvalds's avatar
Linus Torvalds committed
3061

Linus Torvalds's avatar
Linus Torvalds committed
3062 3063 3064 3065 3066 3067
	// 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);
3068 3069

	return SUCCESS;
Linus Torvalds's avatar
Linus Torvalds committed
3070 3071 3072 3073 3074 3075 3076 3077 3078
}

/* 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;
Linus Torvalds's avatar
Linus Torvalds committed
3079

Linus Torvalds's avatar
Linus Torvalds committed
3080 3081 3082 3083 3084 3085 3086
	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 */
Linus Torvalds's avatar
Linus Torvalds committed
3087
			if (timeout--) {
Linus Torvalds's avatar
Linus Torvalds committed
3088 3089 3090 3091
				continue;
			}
		} else if ( status & BAP_ERR ) {
			/* invalid rid or offset */
Linus Torvalds's avatar
Linus Torvalds committed
3092
			printk( KERN_ERR "airo: BAP error %x %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
3093 3094 3095 3096 3097 3098
				status, whichbap );
			return ERROR;
		} else if (status & BAP_DONE) { // success
			return SUCCESS;
		}
		if ( !(max_tries--) ) {
Linus Torvalds's avatar
Linus Torvalds committed
3099
			printk( KERN_ERR
Linus Torvalds's avatar
Linus Torvalds committed
3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127
				"airo: BAP setup error too many retries\n" );
			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 */
static int aux_bap_read(struct airo_info *ai, u16 *pu16Dst,
Linus Torvalds's avatar
Linus Torvalds committed
3128
			int bytelen, int whichbap)
Linus Torvalds's avatar
Linus Torvalds committed
3129 3130 3131 3132 3133 3134 3135
{
	u16 len;
	u16 page;
	u16 offset;
	u16 next;
	int words;
	int i;
Jeff Garzik's avatar
Jeff Garzik committed
3136
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
3137 3138 3139 3140 3141 3142 3143 3144 3145 3146

	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);
Linus Torvalds's avatar
Linus Torvalds committed
3147 3148
		if ( !do8bitIO )
			insw( ai->dev->base_addr+DATA0+whichbap,
Linus Torvalds's avatar
Linus Torvalds committed
3149 3150
			      pu16Dst+i,count );
		else
Linus Torvalds's avatar
Linus Torvalds committed
3151
			insb( ai->dev->base_addr+DATA0+whichbap,
Linus Torvalds's avatar
Linus Torvalds committed
3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163
			      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 */
Linus Torvalds's avatar
Linus Torvalds committed
3164
static int fast_bap_read(struct airo_info *ai, u16 *pu16Dst,
Linus Torvalds's avatar
Linus Torvalds committed
3165 3166 3167
			 int bytelen, int whichbap)
{
	bytelen = (bytelen + 1) & (~1); // round up to even value
Linus Torvalds's avatar
Linus Torvalds committed
3168
	if ( !do8bitIO )
Linus Torvalds's avatar
Linus Torvalds committed
3169 3170 3171 3172 3173 3174 3175
		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 */
Linus Torvalds's avatar
Linus Torvalds committed
3176
static int bap_write(struct airo_info *ai, const u16 *pu16Src,
Linus Torvalds's avatar
Linus Torvalds committed
3177 3178 3179
		     int bytelen, int whichbap)
{
	bytelen = (bytelen + 1) & (~1); // round up to even value
Linus Torvalds's avatar
Linus Torvalds committed
3180 3181
	if ( !do8bitIO )
		outsw( ai->dev->base_addr+DATA0+whichbap,
Linus Torvalds's avatar
Linus Torvalds committed
3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206
		       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. */
3207
static int PC4500_readrid(struct airo_info *ai, u16 rid, void *pBuf, int len, int lock)
Linus Torvalds's avatar
Linus Torvalds committed
3208
{
3209
	u16 status;
Linus Torvalds's avatar
Linus Torvalds committed
3210 3211
        int rc = SUCCESS;

3212
	if (lock) {
3213 3214 3215
		if (down_interruptible(&ai->sem))
			return ERROR;
	}
Linus Torvalds's avatar
Linus Torvalds committed
3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226
	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
Linus Torvalds's avatar
Linus Torvalds committed
3227 3228
	len = min(len, (int)le16_to_cpu(*(u16*)pBuf)) - 2;

Linus Torvalds's avatar
Linus Torvalds committed
3229
	if ( len <= 2 ) {
Linus Torvalds's avatar
Linus Torvalds committed
3230
		printk( KERN_ERR
Linus Torvalds's avatar
Linus Torvalds committed
3231 3232 3233 3234 3235 3236 3237 3238
			"airo: Rid %x has a length of %d which is too short\n",
			(int)rid,
			(int)len );
		rc = ERROR;
                goto done;
	}
	// read remainder of the rid
	rc = bap_read(ai, ((u16*)pBuf)+1, len, BAP1);
3239
done:
3240
	if (lock)
3241
		up(&ai->sem);
Linus Torvalds's avatar
Linus Torvalds committed
3242 3243 3244 3245 3246
	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 */
Linus Torvalds's avatar
Linus Torvalds committed
3247
static int PC4500_writerid(struct airo_info *ai, u16 rid,
3248
			   const void *pBuf, int len, int lock)
Linus Torvalds's avatar
Linus Torvalds committed
3249
{
3250
	u16 status;
Linus Torvalds's avatar
Linus Torvalds committed
3251 3252
	int rc = SUCCESS;

3253 3254
	*(u16*)pBuf = cpu_to_le16((u16)len);

3255
	if (lock) {
3256 3257 3258
		if (down_interruptible(&ai->sem))
			return ERROR;
	}
Linus Torvalds's avatar
Linus Torvalds committed
3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272
	// --- 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:
3273
	if (lock)
3274
		up(&ai->sem);
Linus Torvalds's avatar
Linus Torvalds committed
3275 3276 3277 3278 3279
        return rc;
}

/* Allocates a FID to be used for transmitting packets.  We only use
   one for now. */
3280
static u16 transmit_allocate(struct airo_info *ai, int lenPayload, int raw)
Linus Torvalds's avatar
Linus Torvalds committed
3281
{
3282
	unsigned int loop = 3000;
Linus Torvalds's avatar
Linus Torvalds committed
3283 3284 3285 3286 3287 3288 3289
	Cmd cmd;
	Resp rsp;
	u16 txFid;
	u16 txControl;

	cmd.cmd = CMD_ALLOCATETX;
	cmd.parm0 = lenPayload;
3290 3291 3292
	if (down_interruptible(&ai->sem))
		return ERROR;
	if (issuecommand(ai, &cmd, &rsp) != SUCCESS) {
3293
		txFid = ERROR;
3294 3295 3296
		goto done;
	}
	if ( (rsp.status & 0xFF00) != 0) {
3297
		txFid = ERROR;
3298 3299
		goto done;
	}
Linus Torvalds's avatar
Linus Torvalds committed
3300 3301 3302
	/* 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. */
3303 3304 3305 3306 3307 3308
	while (((IN4500(ai, EVSTAT) & EV_ALLOC) == 0) && --loop);
	if (!loop) {
		txFid = ERROR;
		goto done;
	}

Linus Torvalds's avatar
Linus Torvalds committed
3309 3310 3311
	// get the allocated fid and acknowledge
	txFid = IN4500(ai, TXALLOCFID);
	OUT4500(ai, EVACK, EV_ALLOC);
Linus Torvalds's avatar
Linus Torvalds committed
3312

Linus Torvalds's avatar
Linus Torvalds committed
3313 3314 3315 3316 3317
	/*  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. */
3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330
	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);
Linus Torvalds's avatar
Linus Torvalds committed
3331 3332 3333 3334 3335 3336 3337

	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. */
3338
static int transmit_802_3_packet(struct airo_info *ai, int len, char *pPacket)
Linus Torvalds's avatar
Linus Torvalds committed
3339 3340 3341 3342
{
	u16 payloadLen;
	Cmd cmd;
	Resp rsp;
3343
	int miclen = 0;
3344
	u16 txFid = len;
3345 3346
	MICBuffer pMic;

3347
	len >>= 16;
Linus Torvalds's avatar
Linus Torvalds committed
3348

3349
	if (len <= ETH_ALEN * 2) {
Linus Torvalds's avatar
Linus Torvalds committed
3350 3351 3352
		printk( KERN_WARNING "Short packet %d\n", len );
		return ERROR;
	}
3353 3354 3355
	len -= ETH_ALEN * 2;

#ifdef MICSUPPORT
3356
	if (test_bit(FLAG_MIC_CAPABLE, &ai->flags) && ai->micstats.enabled && 
3357 3358 3359 3360 3361 3362
	    (ntohs(((u16 *)pPacket)[6]) != 0x888E)) {
		if (encapsulate(ai,(etherHead *)pPacket,&pMic,len) != SUCCESS)
			return ERROR;
		miclen = sizeof(pMic);
	}
#endif
Linus Torvalds's avatar
Linus Torvalds committed
3363

Linus Torvalds's avatar
Linus Torvalds committed
3364 3365 3366 3367 3368
	// 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 */
3369
	payloadLen = cpu_to_le16(len + miclen);
Linus Torvalds's avatar
Linus Torvalds committed
3370
	bap_write(ai, &payloadLen, sizeof(payloadLen),BAP1);
3371 3372 3373 3374
	bap_write(ai, (const u16*)pPacket, sizeof(etherHead), BAP1);
	if (miclen)
		bap_write(ai, (const u16*)&pMic, miclen, BAP1);
	bap_write(ai, (const u16*)(pPacket + sizeof(etherHead)), len, BAP1);
Linus Torvalds's avatar
Linus Torvalds committed
3375 3376 3377 3378 3379 3380 3381 3382 3383
	// 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;
}

3384 3385 3386 3387 3388 3389 3390
static int transmit_802_11_packet(struct airo_info *ai, int len, char *pPacket)
{
	u16 fc, payloadLen;
	Cmd cmd;
	Resp rsp;
	int hdrlen;
	struct {
3391
		u8 addr4[ETH_ALEN];
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 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442
		u16 gaplen;
		u8 gap[6];
	} gap;
	u16 txFid = len;
	len >>= 16;
	gap.gaplen = 6;

	fc = le16_to_cpu(*(const u16*)pPacket);
	switch (fc & 0xc) {
		case 4:
			if ((fc & 0xe0) == 0xc0)
				hdrlen = 10;
			else
				hdrlen = 16;
			break;
		case 8:
			if ((fc&0x300)==0x300){
				hdrlen = 30;
				break;
			}
		default:
			hdrlen = 24;
	}

	if (len < hdrlen) {
		printk( KERN_WARNING "Short packet %d\n", len );
		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;
	bap_write(ai, (const u16*)pPacket, hdrlen, BAP1);
	bap_write(ai, hdrlen == 30 ?
		(const u16*)&gap.gaplen : (const u16*)&gap, 38 - hdrlen, BAP1);

	bap_write(ai, (const u16*)(pPacket + hdrlen), len - hdrlen, BAP1);
	// 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;
}

Linus Torvalds's avatar
Linus Torvalds committed
3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463
/*
 *  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 *buffer,
			  size_t len,
			  loff_t *offset);

static ssize_t proc_write( struct file *file,
			   const char *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 );
Linus Torvalds's avatar
Linus Torvalds committed
3464
static int proc_BSSList_open( struct inode *inode, struct file *file );
Linus Torvalds's avatar
Linus Torvalds committed
3465 3466 3467 3468
static int proc_config_open( struct inode *inode, struct file *file );
static int proc_wepkey_open( struct inode *inode, struct file *file );

static struct file_operations proc_statsdelta_ops = {
3469 3470 3471
	.read		= proc_read,
	.open		= proc_statsdelta_open,
	.release	= proc_close
Linus Torvalds's avatar
Linus Torvalds committed
3472 3473 3474
};

static struct file_operations proc_stats_ops = {
3475 3476 3477
	.read		= proc_read,
	.open		= proc_stats_open,
	.release	= proc_close
Linus Torvalds's avatar
Linus Torvalds committed
3478 3479 3480
};

static struct file_operations proc_status_ops = {
3481 3482 3483
	.read		= proc_read,
	.open		= proc_status_open,
	.release	= proc_close
Linus Torvalds's avatar
Linus Torvalds committed
3484 3485 3486
};

static struct file_operations proc_SSID_ops = {
3487 3488 3489 3490
	.read		= proc_read,
	.write		= proc_write,
	.open		= proc_SSID_open,
	.release	= proc_close
Linus Torvalds's avatar
Linus Torvalds committed
3491 3492
};

Linus Torvalds's avatar
Linus Torvalds committed
3493
static struct file_operations proc_BSSList_ops = {
3494 3495 3496 3497
	.read		= proc_read,
	.write		= proc_write,
	.open		= proc_BSSList_open,
	.release	= proc_close
Linus Torvalds's avatar
Linus Torvalds committed
3498 3499
};

Linus Torvalds's avatar
Linus Torvalds committed
3500
static struct file_operations proc_APList_ops = {
3501 3502 3503 3504
	.read		= proc_read,
	.write		= proc_write,
	.open		= proc_APList_open,
	.release	= proc_close
Linus Torvalds's avatar
Linus Torvalds committed
3505 3506 3507
};

static struct file_operations proc_config_ops = {
3508 3509 3510 3511
	.read		= proc_read,
	.write		= proc_write,
	.open		= proc_config_open,
	.release	= proc_close
Linus Torvalds's avatar
Linus Torvalds committed
3512 3513 3514
};

static struct file_operations proc_wepkey_ops = {
3515 3516 3517 3518
	.read		= proc_read,
	.write		= proc_write,
	.open		= proc_wepkey_open,
	.release	= proc_close
Linus Torvalds's avatar
Linus Torvalds committed
3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532
};

static struct proc_dir_entry *airo_entry = 0;

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

Linus Torvalds's avatar
Linus Torvalds committed
3533 3534 3535 3536
#ifndef SETPROC_OPS
#define SETPROC_OPS(entry, ops) (entry)->proc_fops = &(ops)
#endif

Linus Torvalds's avatar
Linus Torvalds committed
3537 3538 3539 3540 3541 3542 3543 3544 3545
static int setup_proc_entry( struct net_device *dev,
			     struct airo_info *apriv ) {
	struct proc_dir_entry *entry;
	/* First setup the device directory */
	apriv->proc_entry = create_proc_entry(dev->name,
					      S_IFDIR|airo_perm,
					      airo_entry);
        apriv->proc_entry->uid = proc_uid;
        apriv->proc_entry->gid = proc_gid;
3546
        apriv->proc_entry->owner = THIS_MODULE;
Linus Torvalds's avatar
Linus Torvalds committed
3547 3548 3549 3550 3551 3552 3553 3554

	/* Setup the StatsDelta */
	entry = create_proc_entry("StatsDelta",
				  S_IFREG | (S_IRUGO&proc_perm),
				  apriv->proc_entry);
        entry->uid = proc_uid;
        entry->gid = proc_gid;
	entry->data = dev;
3555
        entry->owner = THIS_MODULE;
Linus Torvalds's avatar
Linus Torvalds committed
3556
	SETPROC_OPS(entry, proc_statsdelta_ops);
Linus Torvalds's avatar
Linus Torvalds committed
3557

Linus Torvalds's avatar
Linus Torvalds committed
3558 3559 3560 3561 3562 3563 3564
	/* Setup the Stats */
	entry = create_proc_entry("Stats",
				  S_IFREG | (S_IRUGO&proc_perm),
				  apriv->proc_entry);
        entry->uid = proc_uid;
        entry->gid = proc_gid;
	entry->data = dev;
3565
        entry->owner = THIS_MODULE;
Linus Torvalds's avatar
Linus Torvalds committed
3566
	SETPROC_OPS(entry, proc_stats_ops);
Linus Torvalds's avatar
Linus Torvalds committed
3567

Linus Torvalds's avatar
Linus Torvalds committed
3568 3569 3570 3571 3572 3573 3574
	/* Setup the Status */
	entry = create_proc_entry("Status",
				  S_IFREG | (S_IRUGO&proc_perm),
				  apriv->proc_entry);
        entry->uid = proc_uid;
        entry->gid = proc_gid;
	entry->data = dev;
3575
        entry->owner = THIS_MODULE;
Linus Torvalds's avatar
Linus Torvalds committed
3576
	SETPROC_OPS(entry, proc_status_ops);
Linus Torvalds's avatar
Linus Torvalds committed
3577

Linus Torvalds's avatar
Linus Torvalds committed
3578 3579 3580 3581 3582 3583 3584
	/* Setup the Config */
	entry = create_proc_entry("Config",
				  S_IFREG | proc_perm,
				  apriv->proc_entry);
        entry->uid = proc_uid;
        entry->gid = proc_gid;
	entry->data = dev;
3585
        entry->owner = THIS_MODULE;
Linus Torvalds's avatar
Linus Torvalds committed
3586
	SETPROC_OPS(entry, proc_config_ops);
Linus Torvalds's avatar
Linus Torvalds committed
3587 3588 3589 3590 3591 3592 3593 3594

	/* Setup the SSID */
	entry = create_proc_entry("SSID",
				  S_IFREG | proc_perm,
				  apriv->proc_entry);
        entry->uid = proc_uid;
        entry->gid = proc_gid;
	entry->data = dev;
3595
        entry->owner = THIS_MODULE;
Linus Torvalds's avatar
Linus Torvalds committed
3596
	SETPROC_OPS(entry, proc_SSID_ops);
Linus Torvalds's avatar
Linus Torvalds committed
3597 3598 3599 3600 3601 3602 3603 3604

	/* Setup the APList */
	entry = create_proc_entry("APList",
				  S_IFREG | proc_perm,
				  apriv->proc_entry);
        entry->uid = proc_uid;
        entry->gid = proc_gid;
	entry->data = dev;
3605
        entry->owner = THIS_MODULE;
Linus Torvalds's avatar
Linus Torvalds committed
3606 3607 3608 3609 3610 3611 3612 3613 3614
	SETPROC_OPS(entry, proc_APList_ops);

	/* Setup the BSSList */
	entry = create_proc_entry("BSSList",
				  S_IFREG | proc_perm,
				  apriv->proc_entry);
	entry->uid = proc_uid;
	entry->gid = proc_gid;
	entry->data = dev;
3615
        entry->owner = THIS_MODULE;
Linus Torvalds's avatar
Linus Torvalds committed
3616
	SETPROC_OPS(entry, proc_BSSList_ops);
Linus Torvalds's avatar
Linus Torvalds committed
3617 3618 3619 3620 3621 3622 3623 3624

	/* Setup the WepKey */
	entry = create_proc_entry("WepKey",
				  S_IFREG | proc_perm,
				  apriv->proc_entry);
        entry->uid = proc_uid;
        entry->gid = proc_gid;
	entry->data = dev;
3625
        entry->owner = THIS_MODULE;
Linus Torvalds's avatar
Linus Torvalds committed
3626
	SETPROC_OPS(entry, proc_wepkey_ops);
Linus Torvalds's avatar
Linus Torvalds committed
3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639

	return 0;
}

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);
Linus Torvalds's avatar
Linus Torvalds committed
3640
	remove_proc_entry("BSSList",apriv->proc_entry);
Linus Torvalds's avatar
Linus Torvalds committed
3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665
	remove_proc_entry("WepKey",apriv->proc_entry);
	remove_proc_entry(dev->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 *buffer,
			  size_t len,
			  loff_t *offset )
{
	int i;
	int pos;
	struct proc_data *priv = (struct proc_data*)file->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
3666

Linus Torvalds's avatar
Linus Torvalds committed
3667
	if( !priv->rbuffer ) return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
3668

Linus Torvalds's avatar
Linus Torvalds committed
3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684
	pos = *offset;
	for( i = 0; i+pos < priv->readlen && i < len; i++ ) {
		if (put_user( priv->rbuffer[i+pos], buffer+i ))
			return -EFAULT;
	}
	*offset += i;
	return i;
}

/*
 *  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 *buffer,
			   size_t len,
Linus Torvalds's avatar
Linus Torvalds committed
3685
			   loff_t *offset )
Linus Torvalds's avatar
Linus Torvalds committed
3686 3687 3688 3689
{
	int i;
	int pos;
	struct proc_data *priv = (struct proc_data*)file->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
3690

Linus Torvalds's avatar
Linus Torvalds committed
3691 3692 3693
	if ( !priv->wbuffer ) {
		return -EINVAL;
	}
Linus Torvalds's avatar
Linus Torvalds committed
3694

Linus Torvalds's avatar
Linus Torvalds committed
3695
	pos = *offset;
Linus Torvalds's avatar
Linus Torvalds committed
3696

Linus Torvalds's avatar
Linus Torvalds committed
3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708
	for( i = 0; i + pos <  priv->maxwritelen &&
		     i < len; i++ ) {
		if (get_user( priv->wbuffer[i+pos], buffer + i ))
			return -EFAULT;
	}
	if ( i+pos > priv->writelen ) priv->writelen = i+file->f_pos;
	*offset += i;
	return i;
}

static int proc_status_open( struct inode *inode, struct file *file ) {
	struct proc_data *data;
3709
	struct proc_dir_entry *dp = PDE(inode);
Linus Torvalds's avatar
Linus Torvalds committed
3710
	struct net_device *dev = dp->data;
3711
	struct airo_info *apriv = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
3712 3713
	CapabilityRid cap_rid;
	StatusRid status_rid;
Linus Torvalds's avatar
Linus Torvalds committed
3714
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
3715

Linus Torvalds's avatar
Linus Torvalds committed
3716 3717 3718 3719 3720 3721 3722 3723
	if ((file->private_data = kmalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
		return -ENOMEM;
	memset(file->private_data, 0, sizeof(struct proc_data));
	data = (struct proc_data *)file->private_data;
	if ((data->rbuffer = kmalloc( 2048, GFP_KERNEL )) == NULL) {
		kfree (file->private_data);
		return -ENOMEM;
	}
Linus Torvalds's avatar
Linus Torvalds committed
3724

3725
	readStatusRid(apriv, &status_rid, 1);
Linus Torvalds's avatar
Linus Torvalds committed
3726
	readCapabilityRid(apriv, &cap_rid);
Linus Torvalds's avatar
Linus Torvalds committed
3727

Linus Torvalds's avatar
Linus Torvalds committed
3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738
        i = sprintf(data->rbuffer, "Status: %s%s%s%s%s%s%s%s%s\n",
                    status_rid.mode & 1 ? "CFG ": "",
                    status_rid.mode & 2 ? "ACT ": "",
                    status_rid.mode & 0x10 ? "SYN ": "",
                    status_rid.mode & 0x20 ? "LNK ": "",
                    status_rid.mode & 0x40 ? "LEAP ": "",
                    status_rid.mode & 0x80 ? "PRIV ": "",
                    status_rid.mode & 0x100 ? "KEY ": "",
                    status_rid.mode & 0x200 ? "WEP ": "",
                    status_rid.mode & 0x8000 ? "ERR ": "");
	sprintf( data->rbuffer+i, "Mode: %x\n"
Linus Torvalds's avatar
Linus Torvalds committed
3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772
		 "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",
		 (int)status_rid.mode,
		 (int)status_rid.normalizedSignalStrength,
		 (int)status_rid.signalQuality,
		 (int)status_rid.SSIDlen,
		 status_rid.SSID,
		 status_rid.apName,
		 (int)status_rid.channel,
		 (int)status_rid.currentXmitRate/2,
		 version,
		 cap_rid.prodName,
		 cap_rid.manName,
		 cap_rid.prodVer,
		 cap_rid.radioType,
		 cap_rid.country,
		 cap_rid.hardVer,
		 (int)cap_rid.softVer,
		 (int)cap_rid.softSubVer,
		 (int)cap_rid.bootBlockVer );
	data->readlen = strlen( data->rbuffer );
	return 0;
}

static int proc_stats_rid_open(struct inode*, struct file*, u16);
Linus Torvalds's avatar
Linus Torvalds committed
3773
static int proc_statsdelta_open( struct inode *inode,
Linus Torvalds's avatar
Linus Torvalds committed
3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784
				 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);
}

Linus Torvalds's avatar
Linus Torvalds committed
3785
static int proc_stats_rid_open( struct inode *inode,
Linus Torvalds's avatar
Linus Torvalds committed
3786 3787 3788
				struct file *file,
				u16 rid ) {
	struct proc_data *data;
3789
	struct proc_dir_entry *dp = PDE(inode);
Linus Torvalds's avatar
Linus Torvalds committed
3790
	struct net_device *dev = dp->data;
3791
	struct airo_info *apriv = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
3792 3793 3794
	StatsRid stats;
	int i, j;
	int *vals = stats.vals;
3795

Linus Torvalds's avatar
Linus Torvalds committed
3796 3797 3798 3799 3800 3801 3802 3803
	if ((file->private_data = kmalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
		return -ENOMEM;
	memset(file->private_data, 0, sizeof(struct proc_data));
	data = (struct proc_data *)file->private_data;
	if ((data->rbuffer = kmalloc( 4096, GFP_KERNEL )) == NULL) {
		kfree (file->private_data);
		return -ENOMEM;
	}
Linus Torvalds's avatar
Linus Torvalds committed
3804

3805
	readStatsRid(apriv, &stats, rid, 1);
Linus Torvalds's avatar
Linus Torvalds committed
3806

Linus Torvalds's avatar
Linus Torvalds committed
3807
        j = 0;
3808
	for(i=0; statsLabels[i]!=(char *)-1 &&
Linus Torvalds's avatar
Linus Torvalds committed
3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839
		    i*4<stats.len; i++){
		if (!statsLabels[i]) continue;
		if (j+strlen(statsLabels[i])+16>4096) {
			printk(KERN_WARNING
			       "airo: Potentially disasterous buffer overflow averted!\n");
			break;
		}
		j+=sprintf(data->rbuffer+j, "%s: %d\n", statsLabels[i], vals[i]);
	}
	if (i*4>=stats.len){
		printk(KERN_WARNING
		       "airo: Got a short rid\n");
	}
	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;
}

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

Linus Torvalds's avatar
Linus Torvalds committed
3844 3845
static void proc_config_on_close( struct inode *inode, struct file *file ) {
	struct proc_data *data = file->private_data;
3846
	struct proc_dir_entry *dp = PDE(inode);
Linus Torvalds's avatar
Linus Torvalds committed
3847
	struct net_device *dev = dp->data;
3848
	struct airo_info *ai = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
3849
	char *line;
Linus Torvalds's avatar
Linus Torvalds committed
3850

Linus Torvalds's avatar
Linus Torvalds committed
3851
	if ( !data->writelen ) return;
Linus Torvalds's avatar
Linus Torvalds committed
3852

3853
	readConfigRid(ai, 1);
3854
	ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
3855 3856 3857 3858 3859 3860

	line = data->wbuffer;
	while( line[0] ) {
/*** Mode processing */
		if ( !strncmp( line, "Mode: ", 6 ) ) {
			line += 6;
3861
			if ((ai->config.rmode & 0xff) >= RXMODE_RFMON)
3862
					ai->need_commit = 2;
3863
			ai->config.rmode &= 0xfe00;
3864
			clear_bit (FLAG_802_11, &ai->flags);
3865
			ai->config.opmode &= 0xFF00;
3866
			ai->config.scanMode = SCANMODE_ACTIVE;
Linus Torvalds's avatar
Linus Torvalds committed
3867
			if ( line[0] == 'a' ) {
3868
				ai->config.opmode |= 0;
Linus Torvalds's avatar
Linus Torvalds committed
3869
			} else {
3870
				ai->config.opmode |= 1;
3871 3872
				if ( line[0] == 'r' ) {
					ai->config.rmode |= RXMODE_RFMON | RXMODE_DISABLE_802_3_HEADER;
3873
					ai->config.scanMode = SCANMODE_PASSIVE;
3874
					set_bit (FLAG_802_11, &ai->flags);
3875 3876
				} else if ( line[0] == 'y' ) {
					ai->config.rmode |= RXMODE_RFMON_ANYBSS | RXMODE_DISABLE_802_3_HEADER;
3877
					ai->config.scanMode = SCANMODE_PASSIVE;
3878
					set_bit (FLAG_802_11, &ai->flags);
3879 3880
				} else if ( line[0] == 'l' )
					ai->config.rmode |= RXMODE_LANMON;
Linus Torvalds's avatar
Linus Torvalds committed
3881
			}
3882
			ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
3883
		}
Linus Torvalds's avatar
Linus Torvalds committed
3884

Linus Torvalds's avatar
Linus Torvalds committed
3885 3886 3887 3888
/*** Radio status */
		else if (!strncmp(line,"Radio: ", 7)) {
			line += 7;
			if (!strncmp(line,"off",3)) {
3889
				set_bit (FLAG_RADIO_OFF, &ai->flags);
Linus Torvalds's avatar
Linus Torvalds committed
3890
			} else {
3891
				clear_bit (FLAG_RADIO_OFF, &ai->flags);
Linus Torvalds's avatar
Linus Torvalds committed
3892 3893 3894 3895 3896
			}
		}
/*** NodeName processing */
		else if ( !strncmp( line, "NodeName: ", 10 ) ) {
			int j;
Linus Torvalds's avatar
Linus Torvalds committed
3897

Linus Torvalds's avatar
Linus Torvalds committed
3898
			line += 10;
3899
			memset( ai->config.nodeName, 0, 16 );
Linus Torvalds's avatar
Linus Torvalds committed
3900 3901
/* Do the name, assume a space between the mode and node name */
			for( j = 0; j < 16 && line[j] != '\n'; j++ ) {
3902
				ai->config.nodeName[j] = line[j];
Linus Torvalds's avatar
Linus Torvalds committed
3903
			}
3904
			ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
3905 3906
		}

Linus Torvalds's avatar
Linus Torvalds committed
3907 3908 3909 3910
/*** PowerMode processing */
		else if ( !strncmp( line, "PowerMode: ", 11 ) ) {
			line += 11;
			if ( !strncmp( line, "PSPCAM", 6 ) ) {
3911 3912
				ai->config.powerSaveMode = POWERSAVE_PSPCAM;
				ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
3913
			} else if ( !strncmp( line, "PSP", 3 ) ) {
3914 3915
				ai->config.powerSaveMode = POWERSAVE_PSP;
				ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
3916
			} else {
3917 3918
				ai->config.powerSaveMode = POWERSAVE_CAM;
				ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
3919
			}
Linus Torvalds's avatar
Linus Torvalds committed
3920
		} else if ( !strncmp( line, "DataRates: ", 11 ) ) {
Linus Torvalds's avatar
Linus Torvalds committed
3921
			int v, i = 0, k = 0; /* i is index into line,
Linus Torvalds's avatar
Linus Torvalds committed
3922
						k is index to rates */
Linus Torvalds's avatar
Linus Torvalds committed
3923

Linus Torvalds's avatar
Linus Torvalds committed
3924 3925
			line += 11;
			while((v = get_dec_u16(line, &i, 3))!=-1) {
3926
				ai->config.rates[k++] = (u8)v;
Linus Torvalds's avatar
Linus Torvalds committed
3927 3928 3929
				line += i + 1;
				i = 0;
			}
3930
			ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
3931 3932 3933 3934
		} else if ( !strncmp( line, "Channel: ", 9 ) ) {
			int v, i = 0;
			line += 9;
			v = get_dec_u16(line, &i, i+3);
3935 3936 3937 3938
			if ( v != -1 ) {
				ai->config.channelSet = (u16)v;
				ai->need_commit = 1;
			}
Linus Torvalds's avatar
Linus Torvalds committed
3939 3940 3941 3942
		} else if ( !strncmp( line, "XmitPower: ", 11 ) ) {
			int v, i = 0;
			line += 11;
			v = get_dec_u16(line, &i, i+3);
3943 3944 3945 3946
			if ( v != -1 ) {
				ai->config.txPower = (u16)v;
				ai->need_commit = 1;
			}
Linus Torvalds's avatar
Linus Torvalds committed
3947 3948 3949 3950
		} else if ( !strncmp( line, "WEP: ", 5 ) ) {
			line += 5;
			switch( line[0] ) {
			case 's':
3951
				ai->config.authType = (u16)AUTH_SHAREDKEY;
Linus Torvalds's avatar
Linus Torvalds committed
3952 3953
				break;
			case 'e':
3954
				ai->config.authType = (u16)AUTH_ENCRYPT;
Linus Torvalds's avatar
Linus Torvalds committed
3955 3956
				break;
			default:
3957
				ai->config.authType = (u16)AUTH_OPEN;
Linus Torvalds's avatar
Linus Torvalds committed
3958 3959
				break;
			}
3960
			ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
3961 3962
		} else if ( !strncmp( line, "LongRetryLimit: ", 16 ) ) {
			int v, i = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3963

Linus Torvalds's avatar
Linus Torvalds committed
3964 3965 3966
			line += 16;
			v = get_dec_u16(line, &i, 3);
			v = (v<0) ? 0 : ((v>255) ? 255 : v);
3967 3968
			ai->config.longRetryLimit = (u16)v;
			ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
3969 3970
		} else if ( !strncmp( line, "ShortRetryLimit: ", 17 ) ) {
			int v, i = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3971

Linus Torvalds's avatar
Linus Torvalds committed
3972 3973 3974
			line += 17;
			v = get_dec_u16(line, &i, 3);
			v = (v<0) ? 0 : ((v>255) ? 255 : v);
3975 3976
			ai->config.shortRetryLimit = (u16)v;
			ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
3977 3978
		} else if ( !strncmp( line, "RTSThreshold: ", 14 ) ) {
			int v, i = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3979

Linus Torvalds's avatar
Linus Torvalds committed
3980 3981 3982
			line += 14;
			v = get_dec_u16(line, &i, 4);
			v = (v<0) ? 0 : ((v>2312) ? 2312 : v);
3983 3984
			ai->config.rtsThres = (u16)v;
			ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
3985 3986
		} else if ( !strncmp( line, "TXMSDULifetime: ", 16 ) ) {
			int v, i = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3987

Linus Torvalds's avatar
Linus Torvalds committed
3988 3989 3990
			line += 16;
			v = get_dec_u16(line, &i, 5);
			v = (v<0) ? 0 : v;
3991 3992
			ai->config.txLifetime = (u16)v;
			ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
3993 3994
		} else if ( !strncmp( line, "RXMSDULifetime: ", 16 ) ) {
			int v, i = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3995

Linus Torvalds's avatar
Linus Torvalds committed
3996 3997 3998
			line += 16;
			v = get_dec_u16(line, &i, 5);
			v = (v<0) ? 0 : v;
3999 4000
			ai->config.rxLifetime = (u16)v;
			ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
4001
		} else if ( !strncmp( line, "TXDiversity: ", 13 ) ) {
4002
			ai->config.txDiversity =
Linus Torvalds's avatar
Linus Torvalds committed
4003 4004
				(line[13]=='l') ? 1 :
				((line[13]=='r')? 2: 3);
4005
			ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
4006
		} else if ( !strncmp( line, "RXDiversity: ", 13 ) ) {
4007
			ai->config.rxDiversity =
Linus Torvalds's avatar
Linus Torvalds committed
4008 4009
				(line[13]=='l') ? 1 :
				((line[13]=='r')? 2: 3);
4010
			ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
4011 4012
		} else if ( !strncmp( line, "FragThreshold: ", 15 ) ) {
			int v, i = 0;
Linus Torvalds's avatar
Linus Torvalds committed
4013

Linus Torvalds's avatar
Linus Torvalds committed
4014 4015 4016 4017
			line += 15;
			v = get_dec_u16(line, &i, 4);
			v = (v<256) ? 256 : ((v>2312) ? 2312 : v);
			v = v & 0xfffe; /* Make sure its even */
4018 4019
			ai->config.fragThresh = (u16)v;
			ai->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
4020 4021 4022
		} else if (!strncmp(line, "Modulation: ", 12)) {
			line += 12;
			switch(*line) {
4023 4024 4025
			case 'd':  ai->config.modulation=MOD_DEFAULT; ai->need_commit=1; break;
			case 'c':  ai->config.modulation=MOD_CCK; ai->need_commit=1; break;
			case 'm':  ai->config.modulation=MOD_MOK; ai->need_commit=1; break;
Linus Torvalds's avatar
Linus Torvalds committed
4026 4027 4028
			default:
				printk( KERN_WARNING "airo: Unknown modulation\n" );
			}
Linus Torvalds's avatar
Linus Torvalds committed
4029 4030 4031
		} else if (!strncmp(line, "Preamble: ", 10)) {
			line += 10;
			switch(*line) {
4032 4033 4034
			case 'a': ai->config.preamble=PREAMBLE_AUTO; ai->need_commit=1; break;
			case 'l': ai->config.preamble=PREAMBLE_LONG; ai->need_commit=1; break;
			case 's': ai->config.preamble=PREAMBLE_SHORT; ai->need_commit=1; break;
Linus Torvalds's avatar
Linus Torvalds committed
4035 4036
		        default: printk(KERN_WARNING "airo: Unknown preamble\n");
			}
Linus Torvalds's avatar
Linus Torvalds committed
4037 4038 4039 4040 4041 4042
		} else {
			printk( KERN_WARNING "Couldn't figure out %s\n", line );
		}
		while( line[0] && line[0] != '\n' ) line++;
		if ( line[0] ) line++;
	}
4043
	airo_config_commit(dev, NULL, NULL, NULL);
4044 4045 4046 4047 4048 4049 4050 4051 4052
}

static char *get_rmode(u16 mode) {
        switch(mode&0xff) {
        case RXMODE_RFMON:  return "rfmon";
        case RXMODE_RFMON_ANYBSS:  return "yna (any) bss rfmon";
        case RXMODE_LANMON:  return "lanmon";
        }
        return "ESS";
Linus Torvalds's avatar
Linus Torvalds committed
4053 4054 4055 4056
}

static int proc_config_open( struct inode *inode, struct file *file ) {
	struct proc_data *data;
4057
	struct proc_dir_entry *dp = PDE(inode);
Linus Torvalds's avatar
Linus Torvalds committed
4058
	struct net_device *dev = dp->data;
4059
	struct airo_info *ai = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
4060
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
4061

Linus Torvalds's avatar
Linus Torvalds committed
4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077
	if ((file->private_data = kmalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
		return -ENOMEM;
	memset(file->private_data, 0, sizeof(struct proc_data));
	data = (struct proc_data *)file->private_data;
	if ((data->rbuffer = kmalloc( 2048, GFP_KERNEL )) == NULL) {
		kfree (file->private_data);
		return -ENOMEM;
	}
	if ((data->wbuffer = kmalloc( 2048, GFP_KERNEL )) == NULL) {
		kfree (data->rbuffer);
		kfree (file->private_data);
		return -ENOMEM;
	}
	memset( data->wbuffer, 0, 2048 );
	data->maxwritelen = 2048;
	data->on_close = proc_config_on_close;
Linus Torvalds's avatar
Linus Torvalds committed
4078

4079
	readConfigRid(ai, 1);
Linus Torvalds's avatar
Linus Torvalds committed
4080 4081

	i = sprintf( data->rbuffer,
Linus Torvalds's avatar
Linus Torvalds committed
4082 4083 4084 4085 4086 4087 4088
		     "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",
4089 4090 4091 4092
		     (ai->config.opmode & 0xFF) == 0 ? "adhoc" :
		     (ai->config.opmode & 0xFF) == 1 ? get_rmode(ai->config.rmode):
		     (ai->config.opmode & 0xFF) == 2 ? "AP" :
		     (ai->config.opmode & 0xFF) == 3 ? "AP RPTR" : "Error",
4093
		     test_bit(FLAG_RADIO_OFF, &ai->flags) ? "off" : "on",
4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107
		     ai->config.nodeName,
		     ai->config.powerSaveMode == 0 ? "CAM" :
		     ai->config.powerSaveMode == 1 ? "PSP" :
		     ai->config.powerSaveMode == 2 ? "PSPCAM" : "Error",
		     (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],
		     (int)ai->config.channelSet,
		     (int)ai->config.txPower
Linus Torvalds's avatar
Linus Torvalds committed
4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118
		);
	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"
Linus Torvalds's avatar
Linus Torvalds committed
4119 4120
		 "Modulation: %s\n"
		 "Preamble: %s\n",
4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138
		 (int)ai->config.longRetryLimit,
		 (int)ai->config.shortRetryLimit,
		 (int)ai->config.rtsThres,
		 (int)ai->config.txLifetime,
		 (int)ai->config.rxLifetime,
		 ai->config.txDiversity == 1 ? "left" :
		 ai->config.txDiversity == 2 ? "right" : "both",
		 ai->config.rxDiversity == 1 ? "left" :
		 ai->config.rxDiversity == 2 ? "right" : "both",
		 (int)ai->config.fragThresh,
		 ai->config.authType == AUTH_ENCRYPT ? "encrypt" :
		 ai->config.authType == AUTH_SHAREDKEY ? "shared" : "open",
		 ai->config.modulation == 0 ? "default" :
		 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"
Linus Torvalds's avatar
Linus Torvalds committed
4139 4140 4141 4142 4143 4144 4145
		);
	data->readlen = strlen( data->rbuffer );
	return 0;
}

static void proc_SSID_on_close( struct inode *inode, struct file *file ) {
	struct proc_data *data = (struct proc_data *)file->private_data;
4146
	struct proc_dir_entry *dp = PDE(inode);
Linus Torvalds's avatar
Linus Torvalds committed
4147
	struct net_device *dev = dp->data;
4148
	struct airo_info *ai = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
4149
	SsidRid SSID_rid;
4150
	Resp rsp;
Linus Torvalds's avatar
Linus Torvalds committed
4151 4152
	int i;
	int offset = 0;
Linus Torvalds's avatar
Linus Torvalds committed
4153

Linus Torvalds's avatar
Linus Torvalds committed
4154
	if ( !data->writelen ) return;
Linus Torvalds's avatar
Linus Torvalds committed
4155

Linus Torvalds's avatar
Linus Torvalds committed
4156
	memset( &SSID_rid, 0, sizeof( SSID_rid ) );
Linus Torvalds's avatar
Linus Torvalds committed
4157

Linus Torvalds's avatar
Linus Torvalds committed
4158 4159 4160 4161 4162 4163 4164 4165 4166
	for( i = 0; i < 3; i++ ) {
		int j;
		for( j = 0; j+offset < data->writelen && j < 32 &&
			     data->wbuffer[offset+j] != '\n'; j++ ) {
			SSID_rid.ssids[i].ssid[j] = data->wbuffer[offset+j];
		}
		if ( j == 0 ) break;
		SSID_rid.ssids[i].len = j;
		offset += j;
Linus Torvalds's avatar
Linus Torvalds committed
4167
		while( data->wbuffer[offset] != '\n' &&
Linus Torvalds's avatar
Linus Torvalds committed
4168 4169 4170
		       offset < data->writelen ) offset++;
		offset++;
	}
4171 4172
	if (i)
		SSID_rid.len = sizeof(SSID_rid);
4173
	disable_MAC(ai, 1);
Linus Torvalds's avatar
Linus Torvalds committed
4174
	writeSsidRid(ai, &SSID_rid);
4175
	enable_MAC(ai, &rsp, 1);
Linus Torvalds's avatar
Linus Torvalds committed
4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186
}

inline static u8 hexVal(char c) {
	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;
4187
	struct proc_dir_entry *dp = PDE(inode);
Linus Torvalds's avatar
Linus Torvalds committed
4188
	struct net_device *dev = dp->data;
4189
	struct airo_info *ai = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
4190
	APListRid APList_rid;
4191
	Resp rsp;
Linus Torvalds's avatar
Linus Torvalds committed
4192
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
4193

Linus Torvalds's avatar
Linus Torvalds committed
4194
	if ( !data->writelen ) return;
Linus Torvalds's avatar
Linus Torvalds committed
4195

Linus Torvalds's avatar
Linus Torvalds committed
4196 4197
	memset( &APList_rid, 0, sizeof(APList_rid) );
	APList_rid.len = sizeof(APList_rid);
Linus Torvalds's avatar
Linus Torvalds committed
4198

Linus Torvalds's avatar
Linus Torvalds committed
4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213
	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;
			}
		}
	}
4214
	disable_MAC(ai, 1);
Linus Torvalds's avatar
Linus Torvalds committed
4215
	writeAPListRid(ai, &APList_rid);
4216
	enable_MAC(ai, &rsp, 1);
Linus Torvalds's avatar
Linus Torvalds committed
4217 4218 4219 4220
}

/* This function wraps PC4500_writerid with a MAC disable */
static int do_writerid( struct airo_info *ai, u16 rid, const void *rid_data,
4221
			int len, int dummy ) {
Linus Torvalds's avatar
Linus Torvalds committed
4222 4223
	int rc;
	Resp rsp;
Linus Torvalds's avatar
Linus Torvalds committed
4224

4225 4226 4227
	disable_MAC(ai, 1);
	rc = PC4500_writerid(ai, rid, rid_data, len, 1);
	enable_MAC(ai, &rsp, 1);
Linus Torvalds's avatar
Linus Torvalds committed
4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254
	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;
	u16 lastindex;

	rc = readWepKeyRid(ai, &wkr, 1);
	if (rc == SUCCESS) do {
		lastindex = wkr.kindex;
		if (wkr.kindex == index) {
			if (index == 0xffff) {
				return wkr.mac[0];
			}
			return wkr.klen;
		}
		readWepKeyRid(ai, &wkr, 0);
	} while(lastindex != wkr.kindex);
	return -1;
}

static int set_wep_key(struct airo_info *ai, u16 index,
4255
		       const char *key, u16 keylen, int perm, int lock ) {
4256
	static const unsigned char macaddr[ETH_ALEN] = { 0x01, 0, 0, 0, 0, 0 };
Linus Torvalds's avatar
Linus Torvalds committed
4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272
	WepKeyRid wkr;

	memset(&wkr, 0, sizeof(wkr));
	if (keylen == 0) {
// We are selecting which key to use
		wkr.len = sizeof(wkr);
		wkr.kindex = 0xffff;
		wkr.mac[0] = (char)index;
		if (perm) printk(KERN_INFO "Setting transmit key to %d\n", index);
		if (perm) ai->defindex = (char)index;
	} else {
// We are actually setting the key
		wkr.len = sizeof(wkr);
		wkr.kindex = index;
		wkr.klen = keylen;
		memcpy( wkr.key, key, keylen );
4273
		memcpy( wkr.mac, macaddr, ETH_ALEN );
Linus Torvalds's avatar
Linus Torvalds committed
4274 4275
		printk(KERN_INFO "Setting key %d\n", index);
	}
Linus Torvalds's avatar
Linus Torvalds committed
4276

4277
	writeWepKeyRid(ai, &wkr, perm, lock);
Linus Torvalds's avatar
Linus Torvalds committed
4278 4279 4280 4281 4282
	return 0;
}

static void proc_wepkey_on_close( struct inode *inode, struct file *file ) {
	struct proc_data *data;
4283
	struct proc_dir_entry *dp = PDE(inode);
Linus Torvalds's avatar
Linus Torvalds committed
4284
	struct net_device *dev = dp->data;
4285
	struct airo_info *ai = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
4286 4287 4288 4289 4290 4291
	int i;
	char key[16];
	u16 index = 0;
	int j = 0;

	memset(key, 0, sizeof(key));
Linus Torvalds's avatar
Linus Torvalds committed
4292

Linus Torvalds's avatar
Linus Torvalds committed
4293 4294
	data = (struct proc_data *)file->private_data;
	if ( !data->writelen ) return;
Linus Torvalds's avatar
Linus Torvalds committed
4295

Linus Torvalds's avatar
Linus Torvalds committed
4296 4297 4298 4299
	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') {
4300
			set_wep_key(ai, index, 0, 0, 1, 1);
Linus Torvalds's avatar
Linus Torvalds committed
4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318
			return;
		}
		j = 2;
	} else {
		printk(KERN_ERR "airo:  WepKey passed invalid key index\n");
		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;
		}
	}
4319
	set_wep_key(ai, index, key, i/3, 1, 1);
Linus Torvalds's avatar
Linus Torvalds committed
4320 4321 4322 4323
}

static int proc_wepkey_open( struct inode *inode, struct file *file ) {
	struct proc_data *data;
4324
	struct proc_dir_entry *dp = PDE(inode);
Linus Torvalds's avatar
Linus Torvalds committed
4325
	struct net_device *dev = dp->data;
4326
	struct airo_info *ai = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
4327 4328 4329 4330 4331
	char *ptr;
	WepKeyRid wkr;
	u16 lastindex;
	int j=0;
	int rc;
Linus Torvalds's avatar
Linus Torvalds committed
4332

Linus Torvalds's avatar
Linus Torvalds committed
4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351
	if ((file->private_data = kmalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
		return -ENOMEM;
	memset(file->private_data, 0, sizeof(struct proc_data));
	memset(&wkr, 0, sizeof(wkr));
	data = (struct proc_data *)file->private_data;
	if ((data->rbuffer = kmalloc( 180, GFP_KERNEL )) == NULL) {
		kfree (file->private_data);
		return -ENOMEM;
	}
	memset(data->rbuffer, 0, 180);
	data->writelen = 0;
	data->maxwritelen = 80;
	if ((data->wbuffer = kmalloc( 80, GFP_KERNEL )) == NULL) {
		kfree (data->rbuffer);
		kfree (file->private_data);
		return -ENOMEM;
	}
	memset( data->wbuffer, 0, 80 );
	data->on_close = proc_wepkey_on_close;
Linus Torvalds's avatar
Linus Torvalds committed
4352

Linus Torvalds's avatar
Linus Torvalds committed
4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373
	ptr = data->rbuffer;
	strcpy(ptr, "No wep keys\n");
	rc = readWepKeyRid(ai, &wkr, 1);
	if (rc == SUCCESS) do {
		lastindex = wkr.kindex;
		if (wkr.kindex == 0xffff) {
			j += sprintf(ptr+j, "Tx key = %d\n",
				     (int)wkr.mac[0]);
		} else {
			j += sprintf(ptr+j, "Key %d set with length = %d\n",
				     (int)wkr.kindex, (int)wkr.klen);
		}
		readWepKeyRid(ai, &wkr, 0);
	} while((lastindex != wkr.kindex) && (j < 180-30));

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

static int proc_SSID_open( struct inode *inode, struct file *file ) {
	struct proc_data *data;
4374
	struct proc_dir_entry *dp = PDE(inode);
Linus Torvalds's avatar
Linus Torvalds committed
4375
	struct net_device *dev = dp->data;
4376
	struct airo_info *ai = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397
	int i;
	char *ptr;
	SsidRid SSID_rid;

	if ((file->private_data = kmalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
		return -ENOMEM;
	memset(file->private_data, 0, sizeof(struct proc_data));
	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;
	if ((data->wbuffer = kmalloc( 33*3, GFP_KERNEL )) == NULL) {
		kfree (data->rbuffer);
		kfree (file->private_data);
		return -ENOMEM;
	}
	memset( data->wbuffer, 0, 33*3 );
	data->on_close = proc_SSID_on_close;
Linus Torvalds's avatar
Linus Torvalds committed
4398

Linus Torvalds's avatar
Linus Torvalds committed
4399 4400 4401 4402 4403
	readSsidRid(ai, &SSID_rid);
	ptr = data->rbuffer;
	for( i = 0; i < 3; i++ ) {
		int j;
		if ( !SSID_rid.ssids[i].len ) break;
Linus Torvalds's avatar
Linus Torvalds committed
4404 4405
		for( j = 0; j < 32 &&
			     j < SSID_rid.ssids[i].len &&
Linus Torvalds's avatar
Linus Torvalds committed
4406
			     SSID_rid.ssids[i].ssid[j]; j++ ) {
Linus Torvalds's avatar
Linus Torvalds committed
4407
			*ptr++ = SSID_rid.ssids[i].ssid[j];
Linus Torvalds's avatar
Linus Torvalds committed
4408 4409 4410 4411 4412 4413 4414 4415 4416 4417
		}
		*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;
4418
	struct proc_dir_entry *dp = PDE(inode);
Linus Torvalds's avatar
Linus Torvalds committed
4419
	struct net_device *dev = dp->data;
4420
	struct airo_info *ai = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441
	int i;
	char *ptr;
	APListRid APList_rid;

	if ((file->private_data = kmalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
		return -ENOMEM;
	memset(file->private_data, 0, sizeof(struct proc_data));
	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;
	if ((data->wbuffer = kmalloc( data->maxwritelen, GFP_KERNEL )) == NULL) {
		kfree (data->rbuffer);
		kfree (file->private_data);
		return -ENOMEM;
	}
	memset( data->wbuffer, 0, data->maxwritelen );
	data->on_close = proc_APList_on_close;
Linus Torvalds's avatar
Linus Torvalds committed
4442

Linus Torvalds's avatar
Linus Torvalds committed
4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463
	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;
		ptr += sprintf(ptr, "%02x:%02x:%02x:%02x:%02x:%02x\n",
			       (int)APList_rid.ap[i][0],
			       (int)APList_rid.ap[i][1],
			       (int)APList_rid.ap[i][2],
			       (int)APList_rid.ap[i][3],
			       (int)APList_rid.ap[i][4],
			       (int)APList_rid.ap[i][5]);
	}
	if (i==0) ptr += sprintf(ptr, "Not using specific APs\n");

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

Linus Torvalds's avatar
Linus Torvalds committed
4464 4465
static int proc_BSSList_open( struct inode *inode, struct file *file ) {
	struct proc_data *data;
4466
	struct proc_dir_entry *dp = PDE(inode);
Linus Torvalds's avatar
Linus Torvalds committed
4467
	struct net_device *dev = dp->data;
4468
	struct airo_info *ai = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
4469 4470 4471 4472 4473
	char *ptr;
	BSSListRid BSSList_rid;
	int rc;
	/* If doLoseSync is not 1, we won't do a Lose Sync */
	int doLoseSync = -1;
Linus Torvalds's avatar
Linus Torvalds committed
4474

Linus Torvalds's avatar
Linus Torvalds committed
4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486
	if ((file->private_data = kmalloc(sizeof(struct proc_data ), GFP_KERNEL)) == NULL)
		return -ENOMEM;
	memset(file->private_data, 0, sizeof(struct proc_data));
	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 = 0;
	data->on_close = 0;
Linus Torvalds's avatar
Linus Torvalds committed
4487

Linus Torvalds's avatar
Linus Torvalds committed
4488 4489 4490 4491
	if (file->f_mode & FMODE_WRITE) {
		if (!(file->f_mode & FMODE_READ)) {
			Cmd cmd;
			Resp rsp;
Linus Torvalds's avatar
Linus Torvalds committed
4492

Linus Torvalds's avatar
Linus Torvalds committed
4493 4494
			memset(&cmd, 0, sizeof(cmd));
			cmd.cmd=CMD_LISTBSS;
4495 4496 4497 4498
			if (down_interruptible(&ai->sem))
				return -ERESTARTSYS;
			issuecommand(ai, &cmd, &rsp);
			up(&ai->sem);
Linus Torvalds's avatar
Linus Torvalds committed
4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532
			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);
	while(rc == 0 && BSSList_rid.index != 0xffff) {
		ptr += sprintf(ptr, "%02x:%02x:%02x:%02x:%02x:%02x %*s rssi = %d",
				(int)BSSList_rid.bssid[0],
				(int)BSSList_rid.bssid[1],
				(int)BSSList_rid.bssid[2],
				(int)BSSList_rid.bssid[3],
				(int)BSSList_rid.bssid[4],
				(int)BSSList_rid.bssid[5],
				(int)BSSList_rid.ssidLen,
				BSSList_rid.ssid,
				(int)BSSList_rid.rssi);
		ptr += sprintf(ptr, " channel = %d %s %s %s %s\n",
				(int)BSSList_rid.dsChannel,
				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;
}

Linus Torvalds's avatar
Linus Torvalds committed
4533
static int proc_close( struct inode *inode, struct file *file )
Linus Torvalds's avatar
Linus Torvalds committed
4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547
{
	struct proc_data *data = (struct proc_data *)file->private_data;
	if ( data->on_close != NULL ) data->on_close( inode, file );
	if ( data->rbuffer ) kfree( data->rbuffer );
	if ( data->wbuffer ) kfree( data->wbuffer );
	kfree( data );
	return 0;
}

static struct net_device_list {
	struct net_device *dev;
	struct net_device_list *next;
} *airo_devices = 0;

4548
/* Since the card doesn't automatically switch to the right WEP mode,
Linus Torvalds's avatar
Linus Torvalds committed
4549 4550 4551 4552
   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. */
4553
static void timer_func( struct net_device *dev ) {
4554 4555
	struct airo_info *apriv = dev->priv;
	Resp rsp;
Linus Torvalds's avatar
Linus Torvalds committed
4556

Linus Torvalds's avatar
Linus Torvalds committed
4557
/* We don't have a link so try changing the authtype */
4558 4559 4560
	readConfigRid(apriv, 0);
	disable_MAC(apriv, 0);
	switch(apriv->config.authType) {
Linus Torvalds's avatar
Linus Torvalds committed
4561 4562
		case AUTH_ENCRYPT:
/* So drop to OPEN */
4563
			apriv->config.authType = AUTH_OPEN;
Linus Torvalds's avatar
Linus Torvalds committed
4564 4565 4566
			break;
		case AUTH_SHAREDKEY:
			if (apriv->keyindex < auto_wep) {
4567
				set_wep_key(apriv, apriv->keyindex, 0, 0, 0, 0);
4568
				apriv->config.authType = AUTH_SHAREDKEY;
Linus Torvalds's avatar
Linus Torvalds committed
4569 4570 4571 4572
				apriv->keyindex++;
			} else {
			        /* Drop to ENCRYPT */
				apriv->keyindex = 0;
4573
				set_wep_key(apriv, apriv->defindex, 0, 0, 0, 0);
4574
				apriv->config.authType = AUTH_ENCRYPT;
Linus Torvalds's avatar
Linus Torvalds committed
4575 4576 4577
			}
			break;
		default:  /* We'll escalate to SHAREDKEY */
4578
			apriv->config.authType = AUTH_SHAREDKEY;
4579 4580 4581 4582 4583
	}
	apriv->need_commit = 1;
	writeConfigRid(apriv, 0);
	enable_MAC(apriv, &rsp, 0);
	up(&apriv->sem);
Linus Torvalds's avatar
Linus Torvalds committed
4584 4585

/* Schedule check to see if the change worked */
4586 4587
	clear_bit(JOB_AUTOWEP, &apriv->flags);
	apriv->expires = RUN_AT(HZ*3);
Linus Torvalds's avatar
Linus Torvalds committed
4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606
}

static int add_airo_dev( struct net_device *dev ) {
	struct net_device_list *node = kmalloc( sizeof( *node ), GFP_KERNEL );
	if ( !node )
		return -ENOMEM;

	node->dev = dev;
	node->next = airo_devices;
	airo_devices = node;

	return 0;
}

static void del_airo_dev( struct net_device *dev ) {
	struct net_device_list **p = &airo_devices;
	while( *p && ( (*p)->dev != dev ) )
		p = &(*p)->next;
	if ( *p && (*p)->dev == dev )
Linus Torvalds's avatar
Linus Torvalds committed
4607
		*p = (*p)->next;
Linus Torvalds's avatar
Linus Torvalds committed
4608 4609 4610
}

#ifdef CONFIG_PCI
Linus Torvalds's avatar
Linus Torvalds committed
4611
static int __devinit airo_pci_probe(struct pci_dev *pdev,
Linus Torvalds's avatar
Linus Torvalds committed
4612 4613
				    const struct pci_device_id *pent)
{
Linus Torvalds's avatar
Linus Torvalds committed
4614 4615
	struct net_device *dev;

4616 4617 4618 4619
	if (pci_enable_device(pdev))
		return -ENODEV;
	pci_set_master(pdev);

Linus Torvalds's avatar
Linus Torvalds committed
4620 4621
	dev = init_airo_card(pdev->irq,	pdev->resource[2].start, 0);
	if (!dev)
Linus Torvalds's avatar
Linus Torvalds committed
4622
		return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
4623 4624

	pci_set_drvdata(pdev, dev);
Linus Torvalds's avatar
Linus Torvalds committed
4625 4626 4627 4628 4629
	return 0;
}

static void __devexit airo_pci_remove(struct pci_dev *pdev)
{
Linus Torvalds's avatar
Linus Torvalds committed
4630
	stop_airo_card(pci_get_drvdata(pdev), 1);
Linus Torvalds's avatar
Linus Torvalds committed
4631 4632 4633 4634 4635
}
#endif

static int __init airo_init_module( void )
{
4636
	int i, have_isa_dev = 0;
Linus Torvalds's avatar
Linus Torvalds committed
4637

Linus Torvalds's avatar
Linus Torvalds committed
4638 4639 4640 4641 4642
	airo_entry = create_proc_entry("aironet",
				       S_IFDIR | airo_perm,
				       proc_root_driver);
        airo_entry->uid = proc_uid;
        airo_entry->gid = proc_gid;
Linus Torvalds's avatar
Linus Torvalds committed
4643

Linus Torvalds's avatar
Linus Torvalds committed
4644
	for( i = 0; i < 4 && io[i] && irq[i]; i++ ) {
Linus Torvalds's avatar
Linus Torvalds committed
4645
		printk( KERN_INFO
Linus Torvalds's avatar
Linus Torvalds committed
4646 4647 4648 4649 4650
			"airo:  Trying to configure ISA adapter at irq=%d io=0x%x\n",
			irq[i], io[i] );
		if (init_airo_card( irq[i], io[i], 0 ))
			have_isa_dev = 1;
	}
Linus Torvalds's avatar
Linus Torvalds committed
4651

Linus Torvalds's avatar
Linus Torvalds committed
4652 4653
#ifdef CONFIG_PCI
	printk( KERN_INFO "airo:  Probing for PCI adapters\n" );
4654
	pci_register_driver(&airo_driver);
Linus Torvalds's avatar
Linus Torvalds committed
4655 4656 4657
	printk( KERN_INFO "airo:  Finished probing for PCI adapters\n" );
#endif

Linus Torvalds's avatar
Linus Torvalds committed
4658 4659
	/* Always exit with success, as we are a library module
	 * as well as a driver module
Linus Torvalds's avatar
Linus Torvalds committed
4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670
	 */
	return 0;
}

static void __exit airo_cleanup_module( void )
{
	while( airo_devices ) {
		printk( KERN_INFO "airo: Unregistering %s\n", airo_devices->dev->name );
		stop_airo_card( airo_devices->dev, 1 );
	}
	remove_proc_entry("aironet", proc_root_driver);
4671

4672
#ifdef CONFIG_PCI
4673
	pci_unregister_driver(&airo_driver);
4674
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4675 4676 4677 4678 4679 4680
}

#ifdef WIRELESS_EXT
/*
 * Initial Wireless Extension code for Aironet driver by :
 *	Jean Tourrilhes <jt@hpl.hp.com> - HPL - 17 November 00
4681 4682 4683 4684 4685
 * 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
Linus Torvalds's avatar
Linus Torvalds committed
4686 4687
 */

4688
/*------------------------------------------------------------------*/
Linus Torvalds's avatar
Linus Torvalds committed
4689
/*
4690
 * Wireless Handler : get protocol name
Linus Torvalds's avatar
Linus Torvalds committed
4691
 */
4692 4693 4694 4695
static int airo_get_name(struct net_device *dev,
			 struct iw_request_info *info,
			 char *cwrq,
			 char *extra)
Linus Torvalds's avatar
Linus Torvalds committed
4696
{
4697 4698 4699
	strcpy(cwrq, "IEEE 802.11-DS");
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
4700

4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723
/*------------------------------------------------------------------*/
/*
 * 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;
Linus Torvalds's avatar
Linus Torvalds committed
4724
	}
4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735
	/* 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 !!! */
		if((channel < 1) || (channel > 16)) {
			printk(KERN_DEBUG "%s: New channel value of %d is invalid!\n", dev->name, fwrq->m);
			rc = -EINVAL;
		} else {
4736
			readConfigRid(local, 1);
4737 4738 4739 4740 4741 4742 4743
			/* Yes ! We can set it !!! */
			local->config.channelSet = (u16)(channel - 1);
			local->need_commit = 1;
		}
	}
	return rc;
}
Linus Torvalds's avatar
Linus Torvalds committed
4744

4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755
/*------------------------------------------------------------------*/
/*
 * 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 */
Linus Torvalds's avatar
Linus Torvalds committed
4756

4757
	readConfigRid(local, 1);
4758 4759 4760
	if ((local->config.opmode & 0xFF) == MODE_STA_ESS)
		status_rid.channel = local->config.channelSet;
	else
4761
		readStatusRid(local, &status_rid, 1);
Linus Torvalds's avatar
Linus Torvalds committed
4762 4763

#ifdef WEXT_USECHANNELS
4764 4765
	fwrq->m = ((int)status_rid.channel) + 1;
	fwrq->e = 0;
Linus Torvalds's avatar
Linus Torvalds committed
4766
#else
4767 4768 4769 4770 4771
	{
		int f = (int)status_rid.channel;
		fwrq->m = frequency_list[f] * 100000;
		fwrq->e = 1;
	}
Linus Torvalds's avatar
Linus Torvalds committed
4772 4773
#endif

4774 4775
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
4776

4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788
/*------------------------------------------------------------------*/
/*
 * 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;
	Resp rsp;
	SsidRid SSID_rid;		/* SSIDs */
Linus Torvalds's avatar
Linus Torvalds committed
4789

4790 4791
	/* Reload the list of current SSID */
	readSsidRid(local, &SSID_rid);
Linus Torvalds's avatar
Linus Torvalds committed
4792

4793 4794 4795 4796 4797 4798
	/* 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;
Linus Torvalds's avatar
Linus Torvalds committed
4799

4800 4801 4802 4803 4804 4805 4806
		/* Check the size of the string */
		if(dwrq->length > IW_ESSID_MAX_SIZE+1) {
			return -E2BIG ;
		}
		/* Check if index is valid */
		if((index < 0) || (index >= 4)) {
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
4807 4808
		}

4809 4810 4811 4812 4813
		/* 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);
		SSID_rid.ssids[index].len = dwrq->length - 1;
4814
		SSID_rid.len = sizeof(SSID_rid);
4815 4816
	}
	/* Write it to the card */
4817
	disable_MAC(local, 1);
4818
	writeSsidRid(local, &SSID_rid);
4819
	enable_MAC(local, &rsp, 1);
Linus Torvalds's avatar
Linus Torvalds committed
4820

4821 4822
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
4823

4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834
/*------------------------------------------------------------------*/
/*
 * 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 */
Linus Torvalds's avatar
Linus Torvalds committed
4835

4836
	readStatusRid(local, &status_rid, 1);
Linus Torvalds's avatar
Linus Torvalds committed
4837

4838 4839
	/* Note : if dwrq->flags != 0, we should
	 * get the relevant SSID from the SSID list... */
Linus Torvalds's avatar
Linus Torvalds committed
4840

4841 4842 4843 4844
	/* Get the current SSID */
	memcpy(extra, status_rid.SSID, status_rid.SSIDlen);
	extra[status_rid.SSIDlen] = '\0';
	/* If none, we may want to get the one that was set */
Linus Torvalds's avatar
Linus Torvalds committed
4845

4846 4847 4848
	/* Push it out ! */
	dwrq->length = status_rid.SSIDlen + 1;
	dwrq->flags = 1; /* active */
Linus Torvalds's avatar
Linus Torvalds committed
4849

4850 4851
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
4852

4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865
/*------------------------------------------------------------------*/
/*
 * 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;
4866
	static const unsigned char bcast[ETH_ALEN] = { 255, 255, 255, 255, 255, 255 };
Linus Torvalds's avatar
Linus Torvalds committed
4867

4868 4869
	if (awrq->sa_family != ARPHRD_ETHER)
		return -EINVAL;
4870
	else if (!memcmp(bcast, awrq->sa_data, ETH_ALEN)) {
4871 4872 4873 4874 4875 4876 4877 4878 4879
		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));
		APList_rid.len = sizeof(APList_rid);
4880
		memcpy(APList_rid.ap[0], awrq->sa_data, ETH_ALEN);
4881
		disable_MAC(local, 1);
4882
		writeAPListRid(local, &APList_rid);
4883
		enable_MAC(local, &rsp, 1);
4884 4885 4886
	}
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
4887

4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898
/*------------------------------------------------------------------*/
/*
 * 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 */
Linus Torvalds's avatar
Linus Torvalds committed
4899

4900
	readStatusRid(local, &status_rid, 1);
Linus Torvalds's avatar
Linus Torvalds committed
4901

4902
	/* Tentative. This seems to work, wow, I'm lucky !!! */
4903
	memcpy(awrq->sa_data, status_rid.bssid[0], ETH_ALEN);
4904
	awrq->sa_family = ARPHRD_ETHER;
Linus Torvalds's avatar
Linus Torvalds committed
4905

4906 4907
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
4908

4909 4910 4911 4912 4913 4914 4915 4916 4917 4918
/*------------------------------------------------------------------*/
/*
 * 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;
Linus Torvalds's avatar
Linus Torvalds committed
4919

4920 4921 4922
	/* Check the size of the string */
	if(dwrq->length > 16 + 1) {
		return -E2BIG;
Linus Torvalds's avatar
Linus Torvalds committed
4923
	}
4924
	readConfigRid(local, 1);
4925 4926 4927
	memset(local->config.nodeName, 0, sizeof(local->config.nodeName));
	memcpy(local->config.nodeName, extra, dwrq->length);
	local->need_commit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
4928

4929 4930
	return -EINPROGRESS;		/* Call commit handler */
}
Linus Torvalds's avatar
Linus Torvalds committed
4931

4932 4933 4934 4935 4936 4937 4938 4939 4940 4941
/*------------------------------------------------------------------*/
/*
 * 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;
Linus Torvalds's avatar
Linus Torvalds committed
4942

4943
	readConfigRid(local, 1);
4944 4945 4946
	strncpy(extra, local->config.nodeName, 16);
	extra[16] = '\0';
	dwrq->length = strlen(extra) + 1;
Linus Torvalds's avatar
Linus Torvalds committed
4947

4948 4949
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
4950

4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963
/*------------------------------------------------------------------*/
/*
 * 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;
Linus Torvalds's avatar
Linus Torvalds committed
4964

4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980
	/* First : get a valid bit rate value */
	readCapabilityRid(local, &cap_rid);

	/* 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;
Linus Torvalds's avatar
Linus Torvalds committed
4981 4982 4983
				break;
			}
		}
4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998
	}
	/* -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;
	}
Linus Torvalds's avatar
Linus Torvalds committed
4999

5000
	readConfigRid(local, 1);
5001 5002 5003 5004 5005 5006 5007 5008
	/* 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;
Linus Torvalds's avatar
Linus Torvalds committed
5009
		}
5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032
	} else {
		/* Fixed mode */
		/* One rate, fixed */
		memset(local->config.rates, 0, 8);
		local->config.rates[0] = brate;
	}
	local->need_commit = 1;

	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 */

5033
	readStatusRid(local, &status_rid, 1);
5034 5035 5036

	vwrq->value = status_rid.currentXmitRate * 500000;
	/* If more than one rate, set auto */
5037
	readConfigRid(local, 1);
5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059
	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)
		rthr = 2312;
	if((rthr < 0) || (rthr > 2312)) {
		return -EINVAL;
	}
5060
	readConfigRid(local, 1);
5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077
	local->config.rtsThres = rthr;
	local->need_commit = 1;

	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;

5078
	readConfigRid(local, 1);
5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103
	vwrq->value = local->config.rtsThres;
	vwrq->disabled = (vwrq->value >= 2312);
	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)
		fthr = 2312;
	if((fthr < 256) || (fthr > 2312)) {
		return -EINVAL;
	}
	fthr &= ~0x1;	/* Get an even value - is it really needed ??? */
5104
	readConfigRid(local, 1);
5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121
	local->config.fragThresh = (u16)fthr;
	local->need_commit = 1;

	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;

5122
	readConfigRid(local, 1);
5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139
	vwrq->value = local->config.fragThresh;
	vwrq->disabled = (vwrq->value >= 2312);
	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;
5140 5141
	int commit = 1;

5142
	readConfigRid(local, 1);
5143 5144
	if ((local->config.rmode & 0xff) >= RXMODE_RFMON)
		commit = 2;
5145 5146 5147

	switch(*uwrq) {
		case IW_MODE_ADHOC:
5148 5149
			local->config.opmode &= 0xFF00;
			local->config.opmode |= MODE_STA_IBSS;
5150
			local->config.rmode &= 0xfe00;
5151
			local->config.scanMode = SCANMODE_ACTIVE;
5152
			clear_bit (FLAG_802_11, &local->flags);
5153 5154
			break;
		case IW_MODE_INFRA:
5155 5156
			local->config.opmode &= 0xFF00;
			local->config.opmode |= MODE_STA_ESS;
5157
			local->config.rmode &= 0xfe00;
5158
			local->config.scanMode = SCANMODE_ACTIVE;
5159
			clear_bit (FLAG_802_11, &local->flags);
5160 5161
			break;
		case IW_MODE_MASTER:
5162 5163
			local->config.opmode &= 0xFF00;
			local->config.opmode |= MODE_AP;
5164
			local->config.rmode &= 0xfe00;
5165
			local->config.scanMode = SCANMODE_ACTIVE;
5166
			clear_bit (FLAG_802_11, &local->flags);
5167 5168
			break;
		case IW_MODE_REPEAT:
5169 5170
			local->config.opmode &= 0xFF00;
			local->config.opmode |= MODE_AP_RPTR;
5171
			local->config.rmode &= 0xfe00;
5172
			local->config.scanMode = SCANMODE_ACTIVE;
5173
			clear_bit (FLAG_802_11, &local->flags);
5174 5175 5176 5177 5178 5179
			break;
		case IW_MODE_MONITOR:
			local->config.opmode &= 0xFF00;
			local->config.opmode |= MODE_STA_ESS;
			local->config.rmode &= 0xfe00;
			local->config.rmode |= RXMODE_RFMON | RXMODE_DISABLE_802_3_HEADER;
5180
			local->config.scanMode = SCANMODE_PASSIVE;
5181
			set_bit (FLAG_802_11, &local->flags);
5182 5183 5184 5185
			break;
		default:
			return -EINVAL;
	}
5186
	local->need_commit = commit;
5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201

	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;

5202
	readConfigRid(local, 1);
5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238
	/* If not managed, assume it's ad-hoc */
	switch (local->config.opmode & 0xFF) {
		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;
}

/*------------------------------------------------------------------*/
/*
 * 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 */

	/* Is WEP supported ? */
	readCapabilityRid(local, &cap_rid);
	/* Older firmware doesn't support this...
	if(!(cap_rid.softCap & 2)) {
		return -EOPNOTSUPP;
	} */
5239
	readConfigRid(local, 1);
5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273

	/* 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) */
		if ((index < 0) || (index>=(cap_rid.softCap&0x80)?4:1))
			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 */
5274
			set_wep_key(local, index, key.key, key.len, 1, 1);
5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287
		}
		/* 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;
			local->need_commit = 1;
		}
	} else {
		/* Do we want to just set the transmit key index ? */
		int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
		if ((index>=0) && (index<(cap_rid.softCap&0x80)?4:1)) {
5288
			set_wep_key(local, index, 0, 0, 1, 1);
5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325
		} else
			/* Don't complain if only change the mode */
			if(!dwrq->flags & IW_ENCODE_MODE) {
				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 */
	if(dwrq->flags & IW_ENCODE_MODE)
		local->need_commit = 1;
	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);
	if(!(cap_rid.softCap & 2)) {
		return -EOPNOTSUPP;
	}
5326
	readConfigRid(local, 1);
5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372
	/* 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 */
	if((index < 0) || (index >= (cap_rid.softCap & 0x80) ? 4 : 1))
		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;
}

/*------------------------------------------------------------------*/
/*
 * 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;

	readCapabilityRid(local, &cap_rid);

	if (vwrq->disabled) {
5373
		set_bit (FLAG_RADIO_OFF, &local->flags);
5374 5375 5376 5377 5378 5379
		local->need_commit = 1;
		return -EINPROGRESS;		/* Call commit handler */
	}
	if (vwrq->flags != IW_TXPOW_MWATT) {
		return -EINVAL;
	}
5380
	clear_bit (FLAG_RADIO_OFF, &local->flags);
5381 5382
	for (i = 0; cap_rid.txPowerLevels[i] && (i < 8); i++)
		if ((vwrq->value==cap_rid.txPowerLevels[i])) {
5383
			readConfigRid(local, 1);
5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402
			local->config.txPower = vwrq->value;
			local->need_commit = 1;
			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;

5403
	readConfigRid(local, 1);
5404 5405
	vwrq->value = local->config.txPower;
	vwrq->fixed = 1;	/* No power control */
5406
	vwrq->disabled = test_bit(FLAG_RADIO_OFF, &local->flags);
5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426
	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;
	}
5427
	readConfigRid(local, 1);
5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461
	if(vwrq->flags & IW_RETRY_LIMIT) {
		if(vwrq->flags & IW_RETRY_MAX)
			local->config.longRetryLimit = vwrq->value;
		else if (vwrq->flags & IW_RETRY_MIN)
			local->config.shortRetryLimit = vwrq->value;
		else {
			/* No modifier : set both */
			local->config.longRetryLimit = vwrq->value;
			local->config.shortRetryLimit = vwrq->value;
		}
		local->need_commit = 1;
		rc = -EINPROGRESS;		/* Call commit handler */
	}
	if(vwrq->flags & IW_RETRY_LIFETIME) {
		local->config.txLifetime = vwrq->value / 1024;
		local->need_commit = 1;
		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 */

5462
	readConfigRid(local, 1);
5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497
	/* Note : by default, display the min retry number */
	if((vwrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
		vwrq->flags = IW_RETRY_LIFETIME;
		vwrq->value = (int)local->config.txLifetime * 1024;
	} else if((vwrq->flags & IW_RETRY_MAX)) {
		vwrq->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
		vwrq->value = (int)local->config.longRetryLimit;
	} else {
		vwrq->flags = IW_RETRY_LIMIT;
		vwrq->value = (int)local->config.shortRetryLimit;
		if((int)local->config.shortRetryLimit != (int)local->config.longRetryLimit)
			vwrq->flags |= IW_RETRY_MIN;
	}

	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);

	dwrq->length = sizeof(struct iw_range);
Andrew Morton's avatar
Andrew Morton committed
5498
	memset(range, 0, sizeof(*range));
5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563
	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;

	/* Hum... Should put the right values there */
	range->max_qual.qual = 10;
	range->max_qual.level = 0x100 - 120;	/* -120 dBm */
	range->max_qual.noise = 0;
	range->sensitivity = 65535;

	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;
	range->max_rts = 2312;
	range->min_frag = 256;
	range->max_frag = 2312;

	if(cap_rid.softCap & 2) {
		// WEP: RC4 40 bits
		range->encoding_size[0] = 5;
		// RC4 ~128 bits
		if (cap_rid.softCap & 0x100) {
			range->encoding_size[1] = 13;
			range->num_encoding_sizes = 2;
		} else
			range->num_encoding_sizes = 1;
		range->max_encoding_tokens = (cap_rid.softCap & 0x80) ? 4 : 1;
	} 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++) {
		range->txpower[i] = cap_rid.txPowerLevels[i];
		if(range->txpower[i] == 0)
Linus Torvalds's avatar
Linus Torvalds committed
5564
			break;
5565 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 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600
	}
	range->num_txpower = i;
	range->txpower_capa = IW_TXPOW_MWATT;
	range->we_version_source = 12;
	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;
	/* Experimental measurements - boundary 11/5.5 Mb/s */
	/* Note : with or without the (local->rssi), results
	 * are somewhat different. - Jean II */
	range->avg_qual.qual = 6;
	if (local->rssi)
		range->avg_qual.level = 186;	/* -70 dBm */
	else
		range->avg_qual.level = 176;	/* -80 dBm */
	range->avg_qual.noise = 0;

	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;

5601
	readConfigRid(local, 1);
5602 5603 5604
	if (vwrq->disabled) {
		if ((local->config.rmode & 0xFF) >= RXMODE_RFMON) {
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
5605
		}
5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656
		local->config.powerSaveMode = POWERSAVE_CAM;
		local->config.rmode &= 0xFF00;
		local->config.rmode |= RXMODE_BC_MC_ADDR;
		local->need_commit = 1;
		return -EINPROGRESS;		/* Call commit handler */
	}
	if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
		local->config.fastListenDelay = (vwrq->value + 500) / 1024;
		local->config.powerSaveMode = POWERSAVE_PSPCAM;
		local->need_commit = 1;
	} else if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_PERIOD) {
		local->config.fastListenInterval = local->config.listenInterval = (vwrq->value + 500) / 1024;
		local->config.powerSaveMode = POWERSAVE_PSPCAM;
		local->need_commit = 1;
	}
	switch (vwrq->flags & IW_POWER_MODE) {
		case IW_POWER_UNICAST_R:
			if ((local->config.rmode & 0xFF) >= RXMODE_RFMON) {
				return -EINVAL;
			}
			local->config.rmode &= 0xFF00;
			local->config.rmode |= RXMODE_ADDR;
			local->need_commit = 1;
			break;
		case IW_POWER_ALL_R:
			if ((local->config.rmode & 0xFF) >= RXMODE_RFMON) {
				return -EINVAL;
			}
			local->config.rmode &= 0xFF00;
			local->config.rmode |= RXMODE_BC_MC_ADDR;
			local->need_commit = 1;
		case IW_POWER_ON:
			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;
5657
	int mode;
5658

5659
	readConfigRid(local, 1);
5660
	mode = local->config.powerSaveMode;
5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688
	if ((vwrq->disabled = (mode == POWERSAVE_CAM)))
		return 0;
	if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
		vwrq->value = (int)local->config.fastListenDelay * 1024;
		vwrq->flags = IW_POWER_TIMEOUT;
	} else {
		vwrq->value = (int)local->config.fastListenInterval * 1024;
		vwrq->flags = IW_POWER_PERIOD;
	}
	if ((local->config.rmode & 0xFF) == RXMODE_ADDR)
		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;

5689
	readConfigRid(local, 1);
5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706
	local->config.rssiThreshold = vwrq->disabled ? RSSI_DEFAULT : vwrq->value;
	local->need_commit = 1;

	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;

5707
	readConfigRid(local, 1);
5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735
	vwrq->value = local->config.rssiThreshold;
	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++) {
		if (readBSSListRid(local, loseSync, &BSSList))
			break;
		loseSync = 0;
5736
		memcpy(address[i].sa_data, BSSList.bssid, ETH_ALEN);
5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748
		address[i].sa_family = ARPHRD_ETHER;
		if (local->rssi)
			qual[i].level = 0x100 - local->rssi[BSSList.rssi].rssidBm;
		else
			qual[i].level = (BSSList.rssi + 321) / 2;
		qual[i].qual = qual[i].noise = 0;
		qual[i].updated = 2;
		if (BSSList.index == 0xffff)
			break;
	}
	if (!i) {
		StatusRid status_rid;		/* Card status info */
5749
		readStatusRid(local, &status_rid, 1);
5750 5751 5752 5753 5754 5755 5756
		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]
5757
			      & status_rid.bssid[i][5])!=0xff &&
5758 5759 5760 5761 5762 5763 5764 5765
			     (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,
5766
			       status_rid.bssid[i], ETH_ALEN);
5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793
			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;
}

#if WIRELESS_EXT > 13
/*------------------------------------------------------------------*/
/*
 * 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;

	/* Note : you may have realised that, as this is a SET operation,
5794
	 * this is privileged and therefore a normal user can't
5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815
	 * perform scanning.
	 * This is not an error, while the device perform scanning,
	 * traffic doesn't flow, so it's a perfect DoS...
	 * Jean II */

	/* Initiate a scan command */
	memset(&cmd, 0, sizeof(cmd));
	cmd.cmd=CMD_LISTBSS;
	if (down_interruptible(&ai->sem))
		return -ERESTARTSYS;
	issuecommand(ai, &cmd, &rsp);
	ai->scan_timestamp = jiffies;
	up(&ai->sem);

	/* At this point, just return to the user. */

	return 0;
}

/*------------------------------------------------------------------*/
/*
5816
 * Translate scan data returned from the card to a card independent
5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850
 * 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,
					BSSListRid *list)
{
	struct airo_info *ai = dev->priv;
	struct iw_event		iwe;		/* Temporary buffer */
	u16			capabilities;
	char *			current_val;	/* For rates */
	int			i;

	/* First entry *MUST* be the AP MAC address */
	iwe.cmd = SIOCGIWAP;
	iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
	memcpy(iwe.u.ap_addr.sa_data, list->bssid, ETH_ALEN);
	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 */
	iwe.u.data.length = list->ssidLen;
	if(iwe.u.data.length > 32)
		iwe.u.data.length = 32;
	iwe.cmd = SIOCGIWESSID;
	iwe.u.data.flags = 1;
	current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, list->ssid);

	/* Add mode */
	iwe.cmd = SIOCGIWMODE;
	capabilities = le16_to_cpu(list->cap);
	if(capabilities & (CAP_ESS | CAP_IBSS)) {
		if(capabilities & CAP_ESS)
5851
			iwe.u.mode = IW_MODE_MASTER;
5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940
		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;
	iwe.u.freq.m = le16_to_cpu(list->dsChannel);
	iwe.u.freq.m = frequency_list[iwe.u.freq.m] * 100000;
	iwe.u.freq.e = 1;
	current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_FREQ_LEN);

	/* Add quality statistics */
	iwe.cmd = IWEVQUAL;
	if (ai->rssi)
		iwe.u.qual.level = 0x100 - ai->rssi[list->rssi].rssidBm;
	else
		iwe.u.qual.level = (list->rssi + 321) / 2;
	iwe.u.qual.noise = 0;
	iwe.u.qual.qual = 0;
	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;
	current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, list->ssid);

	/* 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 */
		if(list->rates[i] == 0)
			break;
		/* Bit rate given in 500 kb/s units (+ 0x80) */
		iwe.u.bitrate.value = ((list->rates[i] & 0x7f) * 500000);
		/* 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;

	/* The other data in the scan result are not really
	 * interesting, so for now drop it - Jean II */
	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;
	BSSListRid BSSList;
	int rc;
	char *current_ev = extra;

	/* When we are associated again, the scan has surely finished.
	 * Just in case, let's make sure enough time has elapsed since
	 * we started the scan. - Javier */
	if(ai->scan_timestamp && time_before(jiffies,ai->scan_timestamp+3*HZ)) {
		/* Important note : we don't want to block the caller
		 * until results are ready for various reasons.
		 * First, managing wait queues is complex and racy
		 * (there may be multiple simultaneous callers).
		 * Second, we grab some rtnetlink lock before comming
		 * here (in dev_ioctl()).
		 * Third, the caller can wait on the Wireless Event
		 * - Jean II */
		return -EAGAIN;
	}
	ai->scan_timestamp = 0;

	/* There's only a race with proc_BSSList_open(), but its
	 * consequences are begnign. So I don't bother fixing it - Javier */
Linus Torvalds's avatar
Linus Torvalds committed
5941

5942
	/* Try to read the first entry of the scan result */
5943
	rc = PC4500_readrid(ai, RID_BSSLISTFIRST, &BSSList, sizeof(BSSList), 1);
5944 5945 5946 5947 5948
	if((rc) || (BSSList.index == 0xffff)) {
		/* Client error, no scan results...
		 * The caller need to restart the scan. */
		return -ENODATA;
	}
Linus Torvalds's avatar
Linus Torvalds committed
5949

5950 5951 5952 5953 5954 5955
	/* Read and parse all entries */
	while((!rc) && (BSSList.index != 0xffff)) {
		/* Translate to WE format this entry */
		current_ev = airo_translate_scan(dev, current_ev,
						 extra + IW_SCAN_MAX_DATA,
						 &BSSList);
Linus Torvalds's avatar
Linus Torvalds committed
5956

5957 5958
		/* Read next entry */
		rc = PC4500_readrid(ai, RID_BSSLISTNEXT,
5959
				    &BSSList, sizeof(BSSList), 1);
5960 5961 5962 5963 5964 5965 5966 5967 5968
	}
	/* Length of data */
	dwrq->length = (current_ev - extra);
	dwrq->flags = 0;	/* todo */

	return 0;
}
#endif	/* WIRELESS_EXT > 13 */

5969
#if WIRELESS_EXT <= 15
5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991
#ifdef WIRELESS_SPY
/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set Spy List
 */
static int airo_set_spy(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;

	/* Disable spy while we copy the addresses.
	 * As we don't disable interrupts, we need to do this to avoid races */
	local->spy_number = 0;

	if (dwrq->length > 0) {
		int i;

		/* Copy addresses */
		for (i = 0; i < dwrq->length; i++)
5992
			memcpy(local->spy_address[i], address[i].sa_data, ETH_ALEN);
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
		/* Reset stats */
		memset(local->spy_stat, 0, sizeof(struct iw_quality) * IW_MAX_SPY);
	}
	/* Enable addresses */
	local->spy_number = dwrq->length;

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get Spy List
 */
static int airo_get_spy(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;
	int i;

	dwrq->length = local->spy_number;

	/* Copy addresses. */
	for(i = 0; i < local->spy_number; i++) 	{
6019
		memcpy(address[i].sa_data, local->spy_address[i], ETH_ALEN);
6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031
		address[i].sa_family = AF_UNIX;
	}
	/* Copy stats to the user buffer (just after). */
	if(local->spy_number > 0)
		memcpy(extra  + (sizeof(struct sockaddr) * local->spy_number),
		       local->spy_stat, sizeof(struct iw_quality) * local->spy_number);
	/* Reset updated flags. */
	for (i=0; i<local->spy_number; i++)
		local->spy_stat[i].updated = 0;
	return 0;
}
#endif			/* WIRELESS_SPY */
6032
#endif /* WIRELESS_EXT <= 15 */
6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050

/*------------------------------------------------------------------*/
/*
 * 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;
	Resp rsp;

	if (!local->need_commit)
		return 0;

	/* Some of the "SET" function may have modified some of the
	 * parameters. It's now time to commit them in the card */
6051
	disable_MAC(local, 1);
6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062
	if (local->need_commit > 1) {
		APListRid APList_rid;
		SsidRid SSID_rid;

		readAPListRid(local, &APList_rid);
		readSsidRid(local, &SSID_rid);
		reset_airo_card(dev);
		disable_MAC(local, 1);
		writeSsidRid(local, &SSID_rid);
		writeAPListRid(local, &APList_rid);
	}
6063 6064 6065 6066
	if (down_interruptible(&local->sem))
		return -ERESTARTSYS;
	writeConfigRid(local, 0);
	enable_MAC(local, &rsp, 0);
6067 6068
	if (local->need_commit > 1)
		airo_set_promisc(local);
6069 6070
	else
		up(&local->sem);
6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106

	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" },
};

#if WIRELESS_EXT > 12
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 */
6107 6108 6109 6110 6111 6112
#if WIRELESS_EXT > 15
	iw_handler_set_spy,			/* SIOCSIWSPY */
	iw_handler_get_spy,			/* SIOCGIWSPY */
	iw_handler_set_thrspy,			/* SIOCSIWTHRSPY */
	iw_handler_get_thrspy,			/* SIOCGIWTHRSPY */
#else /* WIRELESS_EXT > 15 */
6113 6114 6115 6116 6117 6118 6119 6120 6121
#ifdef WIRELESS_SPY
	(iw_handler) airo_set_spy,		/* SIOCSIWSPY */
	(iw_handler) airo_get_spy,		/* SIOCGIWSPY */
#else	/* WIRELESS_SPY */
	(iw_handler) NULL,			/* SIOCSIWSPY */
	(iw_handler) NULL,			/* SIOCGIWSPY */
#endif	/* WIRELESS_SPY */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) NULL,			/* -- hole -- */
6122
#endif /* WIRELESS_EXT > 15 */
6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169
	(iw_handler) airo_set_wap,		/* SIOCSIWAP */
	(iw_handler) airo_get_wap,		/* SIOCGIWAP */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) airo_get_aplist,		/* SIOCGIWAPLIST */
#if WIRELESS_EXT > 13
	(iw_handler) airo_set_scan,		/* SIOCSIWSCAN */
	(iw_handler) airo_get_scan,		/* SIOCGIWSCAN */
#else	/* WIRELESS_EXT > 13 */
	(iw_handler) NULL,			/* SIOCSIWSCAN */
	(iw_handler) NULL,			/* SIOCGIWSCAN */
#endif	/* WIRELESS_EXT > 13 */
	(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 */
};

/* 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 =
{
6170 6171 6172 6173 6174 6175
	.num_standard	= sizeof(airo_handler)/sizeof(iw_handler),
	.num_private	= sizeof(airo_private_handler)/sizeof(iw_handler),
	.num_private_args = sizeof(airo_private_args)/sizeof(struct iw_priv_args),
	.standard	= (iw_handler *) airo_handler,
	.private	= (iw_handler *) airo_private_handler,
	.private_args	= (struct iw_priv_args *) airo_private_args,
6176
#if WIRELESS_EXT > 15
6177 6178 6179 6180
	.spy_offset	= ((void *) (&((struct airo_info *) NULL)->spy_data) -
			   (void *) NULL),
#endif /* WIRELESS_EXT > 15 */

6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210
};

#endif /* WIRELESS_EXT > 12 */
#endif /* WIRELESS_EXT */

/*
 * 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;
#if defined(WIRELESS_EXT) && WIRELESS_EXT < 13
	struct iwreq *wrq = (struct iwreq *) rq;
#endif /* WIRELESS_EXT < 13 */

	switch (cmd) {
/* WE 13 and higher will use airo_handler_def */
#if defined(WIRELESS_EXT) && WIRELESS_EXT < 13
	case SIOCGIWNAME:	// Get name
		airo_get_name(dev, NULL, (char *) &(wrq->u.name), NULL);
Linus Torvalds's avatar
Linus Torvalds committed
6211 6212
		break;

6213 6214
	case SIOCSIWFREQ:	// Set frequency/channel
		rc = airo_set_freq(dev, NULL, &(wrq->u.freq), NULL);
Linus Torvalds's avatar
Linus Torvalds committed
6215
		break;
6216 6217 6218 6219 6220 6221 6222 6223 6224 6225

	case SIOCGIWFREQ:	// Get frequency/channel
		rc = airo_get_freq(dev, NULL, &(wrq->u.freq), NULL);
		break;

	case SIOCSIWESSID:	// Set desired network name (ESSID)
		{
			char essidbuf[IW_ESSID_MAX_SIZE+1];
			if (wrq->u.essid.length > IW_ESSID_MAX_SIZE) {
				rc = -E2BIG;
Linus Torvalds's avatar
Linus Torvalds committed
6226 6227
				break;
			}
6228 6229 6230 6231 6232 6233 6234 6235
			if (copy_from_user(essidbuf, wrq->u.essid.pointer,
					   wrq->u.essid.length)) {
				rc = -EFAULT;
				break;
			}
			rc = airo_set_essid(dev, NULL,
					    &(wrq->u.essid), essidbuf);
		}
Linus Torvalds's avatar
Linus Torvalds committed
6236 6237
		break;

6238 6239 6240 6241 6242 6243 6244 6245 6246 6247
	case SIOCGIWESSID:	// Get current network name (ESSID)
		{
			char essidbuf[IW_ESSID_MAX_SIZE+1];
			if (wrq->u.essid.pointer)
				rc = airo_get_essid(dev, NULL,
						    &(wrq->u.essid), essidbuf);
				if ( copy_to_user(wrq->u.essid.pointer,
						  essidbuf,
						  wrq->u.essid.length) )
					rc = -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
6248
		}
6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264
		break;

	case SIOCSIWAP:
		rc = airo_set_wap(dev, NULL, &(wrq->u.ap_addr), NULL);
		break;

	case SIOCGIWAP:		// Get current Access Point (BSSID)
		rc = airo_get_wap(dev, NULL, &(wrq->u.ap_addr), NULL);
		break;

	case SIOCSIWNICKN:	// Set desired station name
		{
			char nickbuf[IW_ESSID_MAX_SIZE+1];
			if (wrq->u.data.length > IW_ESSID_MAX_SIZE) {
				rc = -E2BIG;
				break;
Linus Torvalds's avatar
Linus Torvalds committed
6265
			}
6266 6267 6268 6269 6270 6271 6272
			if (copy_from_user(nickbuf, wrq->u.data.pointer,
					   wrq->u.data.length)) {
				rc = -EFAULT;
				break;
			}
			rc = airo_set_nick(dev, NULL,
					   &(wrq->u.data), nickbuf);
Linus Torvalds's avatar
Linus Torvalds committed
6273 6274 6275
		}
		break;

6276 6277 6278 6279 6280 6281 6282 6283 6284 6285
	case SIOCGIWNICKN:	// Get current station name
		{
			char nickbuf[IW_ESSID_MAX_SIZE+1];
			if (wrq->u.data.pointer)
				rc = airo_get_nick(dev, NULL,
						   &(wrq->u.data), nickbuf);
				if ( copy_to_user(wrq->u.data.pointer,
						  nickbuf,
						  wrq->u.data.length) )
					rc = -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
6286
		}
6287
		break;
Linus Torvalds's avatar
Linus Torvalds committed
6288

6289 6290
	case SIOCSIWRATE:	// Set the desired bit-rate
		rc = airo_set_rate(dev, NULL, &(wrq->u.bitrate), NULL);
Linus Torvalds's avatar
Linus Torvalds committed
6291 6292
		break;

6293 6294 6295
	case SIOCGIWRATE:	// Get the current bit-rate
		rc = airo_get_rate(dev, NULL, &(wrq->u.bitrate), NULL);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
6296

6297 6298 6299
	case SIOCSIWRTS:	// Set the desired RTS threshold
		rc = airo_set_rts(dev, NULL, &(wrq->u.rts), NULL);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
6300

6301 6302 6303
	case SIOCGIWRTS:	// Get the current RTS threshold
		rc = airo_get_rts(dev, NULL, &(wrq->u.rts), NULL);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
6304

6305 6306 6307
	case SIOCSIWFRAG:	// Set the desired fragmentation threshold
		rc = airo_set_frag(dev, NULL, &(wrq->u.frag), NULL);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
6308

6309 6310
	case SIOCGIWFRAG:	// Get the current fragmentation threshold
		rc = airo_get_frag(dev, NULL, &(wrq->u.frag), NULL);
Linus Torvalds's avatar
Linus Torvalds committed
6311 6312
		break;

6313 6314 6315
	case SIOCSIWMODE:	// Set mode of operation
		rc = airo_set_mode(dev, NULL, &(wrq->u.mode), NULL);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
6316

6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336
	case SIOCGIWMODE:	// Get mode of operation
		rc = airo_get_mode(dev, NULL, &(wrq->u.mode), NULL);
		break;

	case SIOCSIWENCODE:	// Set WEP keys and mode
		{
			char keybuf[MAX_KEY_SIZE];
			if (wrq->u.encoding.pointer) {
				/* We actually have a key to set */
				if (wrq->u.encoding.length > MAX_KEY_SIZE) {
					rc = -E2BIG;
					break;
				}
				if (copy_from_user(keybuf,
						   wrq->u.encoding.pointer,
						   wrq->u.encoding.length)) {
					rc = -EFAULT;
					break;
				}
			} else if (wrq->u.encoding.length != 0) {
Linus Torvalds's avatar
Linus Torvalds committed
6337 6338 6339
				rc = -EINVAL;
				break;
			}
6340 6341
			rc = airo_set_encode(dev, NULL,
					     &(wrq->u.encoding), keybuf);
Linus Torvalds's avatar
Linus Torvalds committed
6342
		}
6343 6344 6345 6346 6347 6348 6349
		break;

	case SIOCGIWENCODE:	// Get the WEP keys and mode
		// Only super-user can see WEP key
		// Note : this is needed only for very old versions of WE
		if (!capable(CAP_NET_ADMIN)) {
			rc = -EPERM;
Linus Torvalds's avatar
Linus Torvalds committed
6350
			break;
6351 6352 6353 6354 6355 6356 6357 6358 6359 6360
		}
		{
			char keybuf[MAX_KEY_SIZE];
			rc = airo_get_encode(dev, NULL,
					     &(wrq->u.encoding), keybuf);
			if (wrq->u.encoding.pointer) {
				if (copy_to_user(wrq->u.encoding.pointer,
						 keybuf,
						 wrq->u.encoding.length))
					rc = -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
6361 6362 6363 6364
			}
		}
		break;

6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397
	case SIOCGIWTXPOW:	// Get the current Tx-Power
		rc=airo_get_txpow(dev, NULL, &(wrq->u.txpower), NULL);
		break;
	case SIOCSIWTXPOW:
		rc=airo_set_txpow(dev, NULL, &(wrq->u.txpower), NULL);
		break;

	case SIOCSIWRETRY:
		rc=airo_set_retry(dev, NULL, &(wrq->u.retry), NULL);
		break;
	case SIOCGIWRETRY:
		rc=airo_get_retry(dev, NULL, &(wrq->u.retry), NULL);
		break;

	case SIOCGIWRANGE:	// Get range of parameters
		{
			struct iw_range range;
			rc = airo_get_range(dev, NULL,
					    &(wrq->u.data), (char *) &range);
			if (copy_to_user(wrq->u.data.pointer, &range,
					 sizeof(struct iw_range)))
				rc = -EFAULT;
		}
		break;

	case SIOCGIWPOWER:
		rc=airo_get_power(dev, NULL, &(wrq->u.power), NULL);
		break;

	case SIOCSIWPOWER:
		rc=airo_set_power(dev, NULL, &(wrq->u.power), NULL);
		break;

Linus Torvalds's avatar
Linus Torvalds committed
6398
	case SIOCGIWSENS:
6399
		rc = airo_get_sens(dev, NULL, &(wrq->u.sens), NULL);
Linus Torvalds's avatar
Linus Torvalds committed
6400 6401 6402
		break;

	case SIOCSIWSENS:
6403
		rc = airo_set_sens(dev, NULL, &(wrq->u.sens), NULL);
Linus Torvalds's avatar
Linus Torvalds committed
6404 6405 6406
		break;

	case SIOCGIWAPLIST:
6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418
		{
			char buffer[IW_MAX_AP * (sizeof(struct sockaddr) +
						  sizeof(struct iw_quality))];
			if (wrq->u.data.pointer) {
				rc = airo_get_aplist(dev, NULL,
						     &(wrq->u.data), buffer);
				if (copy_to_user(wrq->u.data.pointer,
						 buffer,
						 (wrq->u.data.length *
						  (sizeof(struct sockaddr) +
						   sizeof(struct iw_quality)))
						 ))
Linus Torvalds's avatar
Linus Torvalds committed
6419
					rc = -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
6420 6421 6422 6423 6424
			}
		}
		break;

#ifdef WIRELESS_SPY
6425
	case SIOCSIWSPY:	// Set the spy list
Linus Torvalds's avatar
Linus Torvalds committed
6426 6427
		{
			struct sockaddr address[IW_MAX_SPY];
6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438
			/* Check the number of addresses */
			if (wrq->u.data.length > IW_MAX_SPY) {
				rc = -E2BIG;
				break;
			}
			/* Get the data in the driver */
			if (wrq->u.data.pointer) {
				if (copy_from_user((char *) address,
						   wrq->u.data.pointer,
						   sizeof(struct sockaddr) *
						   wrq->u.data.length)) {
Linus Torvalds's avatar
Linus Torvalds committed
6439 6440
				rc = -EFAULT;
				break;
6441 6442 6443 6444
				}
			} else if (wrq->u.data.length != 0) {
				rc = -EINVAL;
				break;
Linus Torvalds's avatar
Linus Torvalds committed
6445
			}
6446 6447
			rc=airo_set_spy(dev, NULL, &(wrq->u.data),
					(char *) address);
Linus Torvalds's avatar
Linus Torvalds committed
6448 6449 6450
		}
		break;

6451
	case SIOCGIWSPY:	// Get the spy list
Linus Torvalds's avatar
Linus Torvalds committed
6452
		{
6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464
			char buffer[IW_MAX_SPY * (sizeof(struct sockaddr) +
						  sizeof(struct iw_quality))];
			if (wrq->u.data.pointer) {
				rc = airo_get_spy(dev, NULL,
						  &(wrq->u.data), buffer);
				if (copy_to_user(wrq->u.data.pointer,
						 buffer,
						 (wrq->u.data.length *
						  (sizeof(struct sockaddr) +
						   sizeof(struct iw_quality)))
						 ))
					rc = -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
6465
			}
Linus Torvalds's avatar
Linus Torvalds committed
6466 6467 6468 6469 6470 6471
		}
		break;
#endif /* WIRELESS_SPY */

#ifdef CISCO_EXT
	case SIOCGIWPRIV:
6472
		if(wrq->u.data.pointer) {
Linus Torvalds's avatar
Linus Torvalds committed
6473
			/* Set the number of ioctl available */
6474
			wrq->u.data.length = sizeof(airo_private_args) / sizeof( airo_private_args[0]);
Linus Torvalds's avatar
Linus Torvalds committed
6475 6476

			/* Copy structure to the user buffer */
6477 6478 6479
			if(copy_to_user(wrq->u.data.pointer,
					(u_char *) airo_private_args,
					sizeof(airo_private_args)))
Linus Torvalds's avatar
Linus Torvalds committed
6480 6481 6482 6483
				rc = -EFAULT;
		}
		break;
#endif /* CISCO_EXT */
6484
#endif /* WIRELESS_EXT < 13 */
Linus Torvalds's avatar
Linus Torvalds committed
6485 6486 6487

#ifdef CISCO_EXT
	case AIROIDIFC:
Linus Torvalds's avatar
Linus Torvalds committed
6488 6489 6490
#ifdef AIROOLDIDIFC
	case AIROOLDIDIFC:
#endif
Linus Torvalds's avatar
Linus Torvalds committed
6491 6492 6493 6494 6495 6496 6497 6498 6499
	{
		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;
Linus Torvalds's avatar
Linus Torvalds committed
6500

Linus Torvalds's avatar
Linus Torvalds committed
6501
	case AIROIOCTL:
Linus Torvalds's avatar
Linus Torvalds committed
6502 6503 6504 6505
#ifdef AIROOLDIOCTL
	case AIROOLDIOCTL:
#endif
		/* Get the command struct and hand it off for evaluation by
Linus Torvalds's avatar
Linus Torvalds committed
6506 6507 6508 6509
		 * the proper subfunction
		 */
	{
		aironet_ioctl com;
Linus Torvalds's avatar
Linus Torvalds committed
6510 6511 6512 6513
		if (copy_from_user(&com,rq->ifr_data,sizeof(com))) {
			rc = -EFAULT;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
6514

Andrew Morton's avatar
Andrew Morton committed
6515
		/* Separate R/W functions bracket legality here
Linus Torvalds's avatar
Linus Torvalds committed
6516
		 */
6517
		if ( com.command <= AIRORRID )
Linus Torvalds's avatar
Linus Torvalds committed
6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533
			rc = readrids(dev,&com);
		else if ( com.command >= AIROPCAP && com.command <= AIROPLEAPUSR )
			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;
	}

6534 6535
#if defined(WIRELESS_EXT) && WIRELESS_EXT < 13
	/* WE 13 and higher will use airo_config_commit */
Linus Torvalds's avatar
Linus Torvalds committed
6536 6537
	/* Some of the "SET" function may have modified some of the
	 * parameters. It's now time to commit them in the card */
6538 6539 6540 6541
	airo_config_commit(dev, NULL, NULL, NULL);
	if (rc == -EINPROGRESS)
		return 0;
#endif /* WIRELESS_EXT < 13 */
Linus Torvalds's avatar
Linus Torvalds committed
6542

6543
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555
}

#ifdef WIRELESS_EXT
/*
 * 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
 */
6556
static void airo_read_wireless_stats(struct airo_info *local)
Linus Torvalds's avatar
Linus Torvalds committed
6557 6558 6559
{
	StatusRid status_rid;
	StatsRid stats_rid;
Linus Torvalds's avatar
Linus Torvalds committed
6560
	u32 *vals = stats_rid.vals;
Linus Torvalds's avatar
Linus Torvalds committed
6561 6562

	/* Get stats out of the card */
6563 6564 6565 6566
	clear_bit(JOB_WSTATS, &local->flags);
	readStatusRid(local, &status_rid, 0);
	readStatsRid(local, &stats_rid, RID_STATS, 0);
	up(&local->sem);
Linus Torvalds's avatar
Linus Torvalds committed
6567 6568 6569 6570 6571 6572

	/* The status */
	local->wstats.status = status_rid.mode;

	/* Signal quality and co. But where is the noise level ??? */
	local->wstats.qual.qual = status_rid.signalQuality;
Linus Torvalds's avatar
Linus Torvalds committed
6573 6574 6575 6576
	if (local->rssi)
		local->wstats.qual.level = 0x100 - local->rssi[status_rid.sigQuality].rssidBm;
	else
		local->wstats.qual.level = (status_rid.normalizedSignalStrength + 321) / 2;
6577 6578 6579 6580 6581 6582 6583
	if (status_rid.len >= 124) {
		local->wstats.qual.noise = 256 - status_rid.noisedBm;
		local->wstats.qual.updated = 7;
	} else {
		local->wstats.qual.noise = 0;
		local->wstats.qual.updated = 3;
	}
Linus Torvalds's avatar
Linus Torvalds committed
6584 6585 6586 6587 6588

	/* Packets discarded in the wireless adapter due to wireless
	 * specific problems */
	local->wstats.discard.nwid = vals[56] + vals[57] + vals[58];/* SSID Mismatch */
	local->wstats.discard.code = vals[6];/* RxWepErr */
Linus Torvalds's avatar
Linus Torvalds committed
6589 6590 6591 6592
	local->wstats.discard.fragment = vals[30];
	local->wstats.discard.retries = vals[10];
	local->wstats.discard.misc = vals[1] + vals[32];
	local->wstats.miss.beacon = vals[34];
6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605
}

struct iw_statistics *airo_get_wireless_stats(struct net_device *dev)
{
	struct airo_info *local =  dev->priv;

	/* Get stats out of the card if available */
	if (down_trylock(&local->sem) != 0) {
		set_bit(JOB_WSTATS, &local->flags);
		wake_up_interruptible(&local->thr_wait);
	} else
		airo_read_wireless_stats(local);

6606
	return &local->wstats;
Linus Torvalds's avatar
Linus Torvalds committed
6607 6608 6609 6610
}
#endif /* WIRELESS_EXT */

#ifdef CISCO_EXT
6611
#define RIDS_SIZE	2048
Linus Torvalds's avatar
Linus Torvalds committed
6612
/*
Linus Torvalds's avatar
Linus Torvalds committed
6613 6614 6615
 * 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
Linus Torvalds's avatar
Linus Torvalds committed
6616 6617 6618 6619
 * the card
 */
static int readrids(struct net_device *dev, aironet_ioctl *comp) {
	unsigned short ridcode;
6620
	unsigned char *iobuf;
6621
	int len;
6622 6623
	struct airo_info *ai = dev->priv;

6624
	if (test_bit(FLAG_FLASHING, &ai->flags))
6625
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
6626 6627 6628 6629

	switch(comp->command)
	{
	case AIROGCAP:      ridcode = RID_CAPABILITIES; break;
6630
	case AIROGCFG: writeConfigRid (ai, 1);
6631
			    ridcode = RID_CONFIG;       break;
Linus Torvalds's avatar
Linus Torvalds committed
6632 6633 6634 6635 6636 6637
	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 */
6638 6639
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
Linus Torvalds's avatar
Linus Torvalds committed
6640 6641 6642
		break;
	case AIROGWEPKNV:   ridcode = RID_WEP_PERM;
		/* Only super-user can read WEP keys */
6643 6644
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
Linus Torvalds's avatar
Linus Torvalds committed
6645 6646 6647 6648
		break;
	case AIROGSTAT:     ridcode = RID_STATUS;       break;
	case AIROGSTATSD32: ridcode = RID_STATSDELTA;   break;
	case AIROGSTATSC32: ridcode = RID_STATS;        break;
6649
#ifdef MICSUPPORT
6650 6651 6652
	case AIROGMICSTATS:
		if (copy_to_user(comp->data, &ai->micstats,
				 min((int)comp->len,(int)sizeof(ai->micstats))))
6653 6654
			return -EFAULT;
		return 0;
6655 6656
#endif
	case AIRORRID:      ridcode = comp->len;        break;
Linus Torvalds's avatar
Linus Torvalds committed
6657
	default:
6658 6659
		return -EINVAL;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
6660 6661
	}

6662 6663 6664
	if ((iobuf = kmalloc(RIDS_SIZE, GFP_KERNEL)) == NULL)
		return -ENOMEM;

6665
	PC4500_readrid(ai,ridcode,iobuf,RIDS_SIZE, 1);
Linus Torvalds's avatar
Linus Torvalds committed
6666
	/* get the count of bytes in the rid  docs say 1st 2 bytes is it.
Linus Torvalds's avatar
Linus Torvalds committed
6667
	 * then return it to the user
Linus Torvalds's avatar
Linus Torvalds committed
6668 6669
	 * 9/22/2000 Honor user given length
	 */
6670 6671 6672 6673
	if (comp->command == AIRORRID)
		len = le16_to_cpu(*(unsigned short *)iobuf); /* Yuck! */
	else
		len = comp->len;
Linus Torvalds's avatar
Linus Torvalds committed
6674

6675
	if (copy_to_user(comp->data, iobuf, min(len, (int)RIDS_SIZE))) {
6676 6677 6678 6679 6680
		kfree (iobuf);
		return -EFAULT;
	}
	kfree (iobuf);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
6681 6682 6683 6684 6685 6686 6687
}

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

static int writerids(struct net_device *dev, aironet_ioctl *comp) {
6688
	struct airo_info *ai = dev->priv;
6689
	int  ridcode, enabled;
Linus Torvalds's avatar
Linus Torvalds committed
6690
	Resp      rsp;
6691
	static int (* writer)(struct airo_info *, u16 rid, const void *, int, int);
6692
	unsigned char *iobuf;
Linus Torvalds's avatar
Linus Torvalds committed
6693 6694 6695 6696 6697

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

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

Linus Torvalds's avatar
Linus Torvalds committed
6701 6702 6703 6704 6705 6706 6707 6708
	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;
6709
	case AIROPCFG: ai->config.len = 0;
6710
			    ridcode = RID_CONFIG;       break;
Linus Torvalds's avatar
Linus Torvalds committed
6711 6712 6713 6714 6715 6716
	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;

Linus Torvalds's avatar
Linus Torvalds committed
6717
		/* this is not really a rid but a command given to the card
Linus Torvalds's avatar
Linus Torvalds committed
6718 6719 6720
		 * same with MAC off
		 */
	case AIROPMACON:
6721
		if (enable_MAC(ai, &rsp, 1) != 0)
6722 6723
			return -EIO;
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
6724

Linus Torvalds's avatar
Linus Torvalds committed
6725
		/*
Linus Torvalds's avatar
Linus Torvalds committed
6726 6727 6728 6729
		 * 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:
6730
		disable_MAC(ai, 1);
6731
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
6732 6733 6734 6735 6736 6737

		/* 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:
6738 6739 6740
		if ((iobuf = kmalloc(RIDS_SIZE, GFP_KERNEL)) == NULL)
			return -ENOMEM;

6741
		PC4500_readrid(ai,RID_STATSDELTACLEAR,iobuf,RIDS_SIZE, 1);
Linus Torvalds's avatar
Linus Torvalds committed
6742

6743
#ifdef MICSUPPORT
6744 6745 6746
		enabled = ai->micstats.enabled;
		memset(&ai->micstats,0,sizeof(ai->micstats));
		ai->micstats.enabled = enabled;
6747
#endif
Linus Torvalds's avatar
Linus Torvalds committed
6748

Linus Torvalds's avatar
Linus Torvalds committed
6749
		if (copy_to_user(comp->data, iobuf,
6750 6751 6752 6753 6754 6755
				 min((int)comp->len, (int)RIDS_SIZE))) {
			kfree (iobuf);
			return -EFAULT;
		}
		kfree (iobuf);
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
6756 6757

	default:
6758
		return -EOPNOTSUPP;	/* Blarg! */
Linus Torvalds's avatar
Linus Torvalds committed
6759
	}
6760 6761
	if(comp->len > RIDS_SIZE)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
6762

6763 6764
	if ((iobuf = kmalloc(RIDS_SIZE, GFP_KERNEL)) == NULL)
		return -ENOMEM;
6765 6766

	if (copy_from_user(iobuf,comp->data,comp->len)) {
6767 6768
		kfree (iobuf);
		return -EFAULT;
6769
	}
6770 6771 6772 6773

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

6774
		if (test_bit(FLAG_MIC_CAPABLE, &ai->flags))
6775 6776 6777
			cfg->opmode |= MODE_MIC;

		if ((cfg->opmode & 0xFF) == MODE_STA_IBSS)
6778
			set_bit (FLAG_ADHOC, &ai->flags);
6779
		else
6780
			clear_bit (FLAG_ADHOC, &ai->flags);
6781 6782
	}

6783
	if((*writer)(ai, ridcode, iobuf,comp->len,1)) {
6784 6785 6786 6787 6788
		kfree (iobuf);
		return -EIO;
	}
	kfree (iobuf);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
6789 6790 6791 6792 6793 6794 6795
}

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

Linus Torvalds's avatar
Linus Torvalds committed
6796
/*
Linus Torvalds's avatar
Linus Torvalds committed
6797 6798 6799 6800 6801 6802 6803 6804 6805
 * Flash command switch table
 */

int flashcard(struct net_device *dev, aironet_ioctl *comp) {
	int z;
	int cmdreset(struct airo_info *);
	int setflashmode(struct airo_info *);
	int flashgchar(struct airo_info *,int,int);
	int flashpchar(struct airo_info *,int,int);
Linus Torvalds's avatar
Linus Torvalds committed
6806
	int flashputbuf(struct airo_info *);
Linus Torvalds's avatar
Linus Torvalds committed
6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818
	int flashrestart(struct airo_info *,struct net_device *);

	/* 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:
Linus Torvalds's avatar
Linus Torvalds committed
6819 6820 6821
		if (!((struct airo_info *)dev->priv)->flash &&
			(((struct airo_info *)dev->priv)->flash = kmalloc (FLASHSIZE, GFP_KERNEL)) == NULL)
			return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
6822 6823 6824 6825 6826
		return setflashmode((struct airo_info *)dev->priv);

	case AIROFLSHGCHR: /* Get char from aux */
		if(comp->len != sizeof(int))
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
6827 6828
		if (copy_from_user(&z,comp->data,comp->len))
			return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
6829 6830 6831 6832 6833
		return flashgchar((struct airo_info *)dev->priv,z,8000);

	case AIROFLSHPCHR: /* Send char to card. */
		if(comp->len != sizeof(int))
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
6834 6835
		if (copy_from_user(&z,comp->data,comp->len))
			return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
6836 6837 6838
		return flashpchar((struct airo_info *)dev->priv,z,8000);

	case AIROFLPUTBUF: /* Send 32k to card */
Linus Torvalds's avatar
Linus Torvalds committed
6839
		if (!((struct airo_info *)dev->priv)->flash)
Linus Torvalds's avatar
Linus Torvalds committed
6840
			return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
6841
		if(comp->len > FLASHSIZE)
Linus Torvalds's avatar
Linus Torvalds committed
6842
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
6843 6844
		if(copy_from_user(((struct airo_info *)dev->priv)->flash,comp->data,comp->len))
			return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
6845

Linus Torvalds's avatar
Linus Torvalds committed
6846
		flashputbuf((struct airo_info *)dev->priv);
Linus Torvalds's avatar
Linus Torvalds committed
6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858
		return 0;

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

#define FLASH_COMMAND  0x7e7e

Linus Torvalds's avatar
Linus Torvalds committed
6859
/*
Linus Torvalds's avatar
Linus Torvalds committed
6860
 * STEP 1)
Linus Torvalds's avatar
Linus Torvalds committed
6861 6862
 * Disable MAC and do soft reset on
 * card.
Linus Torvalds's avatar
Linus Torvalds committed
6863 6864 6865
 */

int cmdreset(struct airo_info *ai) {
6866
	disable_MAC(ai, 1);
Linus Torvalds's avatar
Linus Torvalds committed
6867 6868 6869 6870 6871

	if(!waitbusy (ai)){
		printk(KERN_INFO "Waitbusy hang before RESET\n");
		return -EBUSY;
	}
Linus Torvalds's avatar
Linus Torvalds committed
6872

Linus Torvalds's avatar
Linus Torvalds committed
6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885
	OUT4500(ai,COMMAND,CMD_SOFTRESET);

	set_current_state (TASK_UNINTERRUPTIBLE);
	schedule_timeout (HZ);          /* WAS 600 12/7/00 */

	if(!waitbusy (ai)){
		printk(KERN_INFO "Waitbusy hang AFTER RESET\n");
		return -EBUSY;
	}
	return 0;
}

/* STEP 2)
Linus Torvalds's avatar
Linus Torvalds committed
6886
 * Put the card in legendary flash
Linus Torvalds's avatar
Linus Torvalds committed
6887 6888 6889 6890
 * mode
 */

int setflashmode (struct airo_info *ai) {
6891
	set_bit (FLAG_FLASHING, &ai->flags);
6892

Linus Torvalds's avatar
Linus Torvalds committed
6893 6894
	OUT4500(ai, SWS0, FLASH_COMMAND);
	OUT4500(ai, SWS1, FLASH_COMMAND);
6895 6896 6897 6898 6899 6900 6901 6902
	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);
	}
Linus Torvalds's avatar
Linus Torvalds committed
6903 6904
	set_current_state (TASK_UNINTERRUPTIBLE);
	schedule_timeout (HZ/2); /* 500ms delay */
Linus Torvalds's avatar
Linus Torvalds committed
6905

Linus Torvalds's avatar
Linus Torvalds committed
6906
	if(!waitbusy(ai)) {
6907
		clear_bit (FLAG_FLASHING, &ai->flags);
Linus Torvalds's avatar
Linus Torvalds committed
6908 6909 6910 6911 6912 6913
		printk(KERN_INFO "Waitbusy hang after setflash mode\n");
		return -EIO;
	}
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
6914 6915
/* Put character to SWS0 wait for dwelltime
 * x 50us for  echo .
Linus Torvalds's avatar
Linus Torvalds committed
6916 6917 6918 6919 6920 6921 6922 6923 6924 6925
 */

int flashpchar(struct airo_info *ai,int byte,int dwelltime) {
	int echo;
	int waittime;

	byte |= 0x8000;

	if(dwelltime == 0 )
		dwelltime = 200;
Linus Torvalds's avatar
Linus Torvalds committed
6926

Linus Torvalds's avatar
Linus Torvalds committed
6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963
	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 ){
		printk(KERN_INFO "flash putchar busywait timeout! \n");
		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)
 */
int flashgchar(struct airo_info *ai,int matchbyte,int dwelltime){
	int           rchar;
	unsigned char rbyte=0;

	do {
		rchar = IN4500(ai,SWS1);
Linus Torvalds's avatar
Linus Torvalds committed
6964

Linus Torvalds's avatar
Linus Torvalds committed
6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983
		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;
}

Linus Torvalds's avatar
Linus Torvalds committed
6984 6985 6986
/*
 * Transfer 32k of firmware data from user buffer to our buffer and
 * send to the card
Linus Torvalds's avatar
Linus Torvalds committed
6987 6988
 */

Linus Torvalds's avatar
Linus Torvalds committed
6989
int flashputbuf(struct airo_info *ai){
Linus Torvalds's avatar
Linus Torvalds committed
6990 6991 6992 6993 6994 6995 6996
	int            nwords;

	/* Write stuff */
	OUT4500(ai,AUXPAGE,0x100);
	OUT4500(ai,AUXOFF,0);

	for(nwords=0;nwords != FLASHSIZE / 2;nwords++){
Linus Torvalds's avatar
Linus Torvalds committed
6997
		OUT4500(ai,AUXDATA,ai->flash[nwords] & 0xffff);
Linus Torvalds's avatar
Linus Torvalds committed
6998
	}
Linus Torvalds's avatar
Linus Torvalds committed
6999

Linus Torvalds's avatar
Linus Torvalds committed
7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012
	OUT4500(ai,SWS0,0x8000);

	return 0;
}

/*
 *
 */
int flashrestart(struct airo_info *ai,struct net_device *dev){
	int    i,status;

	set_current_state (TASK_UNINTERRUPTIBLE);
	schedule_timeout (HZ);          /* Added 12/7/00 */
7013
	clear_bit (FLAG_FLASHING, &ai->flags);
7014
	status = setup_card(ai, dev->dev_addr);
Linus Torvalds's avatar
Linus Torvalds committed
7015 7016

	for( i = 0; i < MAX_FIDS; i++ ) {
7017
		ai->fids[i] = transmit_allocate( ai, 2312, i >= MAX_FIDS / 2 );
Linus Torvalds's avatar
Linus Torvalds committed
7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061
	}

	set_current_state (TASK_UNINTERRUPTIBLE);
	schedule_timeout (HZ);          /* 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
Linus Torvalds's avatar
Linus Torvalds committed
7062
    POSSIBILITY OF SUCH DAMAGE.
Linus Torvalds's avatar
Linus Torvalds committed
7063 7064 7065 7066
*/

module_init(airo_init_module);
module_exit(airo_cleanup_module);