atmel.c 130.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*** -*- linux-c -*- **********************************************************

     Driver for Atmel at76c502 at76c504 and at76c506 wireless cards.

J
John Daiker 已提交
5 6
	Copyright 2000-2001 ATMEL Corporation.
	Copyright 2003-2004 Simon Kelley.
L
Linus Torvalds 已提交
7

C
Carlo Perassi 已提交
8 9 10
    This code was developed from version 2.1.1 of the Atmel drivers,
    released by Atmel corp. under the GPL in December 2002. It also
    includes code from the Linux aironet drivers (C) Benjamin Reed,
L
Linus Torvalds 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    and the Linux PCMCIA package, (C) David Hinds and the Linux wireless
    extensions, (C) Jean Tourrilhes.

    The firmware module for reading the MAC address of the card comes from
    net.russotto.AtmelMACFW, written by Matthew T. Russotto and copyright
    by him. net.russotto.AtmelMACFW is used under the GPL license version 2.
    This file contains the module in binary form and, under the terms
    of the GPL, in source form. The source is located at the end of the file.

    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 software 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.

    You should have received a copy of the GNU General Public License
31 32
    along with Atmel wireless lan drivers; if not, see
    <http://www.gnu.org/licenses/>.
L
Linus Torvalds 已提交
33

C
Carlo Perassi 已提交
34
    For all queries about this code, please contact the current author,
L
Linus Torvalds 已提交
35 36 37 38 39 40 41
    Simon Kelley <simon@thekelleys.org.uk> and not Atmel Corporation.

    Credit is due to HP UK and Cambridge Online Systems Ltd for supplying
    hardware used during development of this driver.

******************************************************************************/

42
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
43 44 45 46 47 48 49

#include <linux/kernel.h>
#include <linux/ptrace.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/timer.h>
50
#include <asm/byteorder.h>
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/if_arp.h>
#include <linux/ioport.h>
#include <linux/fcntl.h>
#include <linux/delay.h>
#include <linux/wireless.h>
#include <net/iw_handler.h>
#include <linux/crc32.h>
#include <linux/proc_fs.h>
65
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
66 67 68
#include <linux/device.h>
#include <linux/moduleparam.h>
#include <linux/firmware.h>
69
#include <linux/jiffies.h>
J
Johannes Berg 已提交
70
#include <linux/ieee80211.h>
L
Linus Torvalds 已提交
71 72 73
#include "atmel.h"

#define DRIVER_MAJOR 0
74
#define DRIVER_MINOR 98
L
Linus Torvalds 已提交
75 76 77 78 79 80

MODULE_AUTHOR("Simon Kelley");
MODULE_DESCRIPTION("Support for Atmel at76c50x 802.11 wireless ethernet cards.");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("Atmel at76c50x wireless cards");

C
Carlo Perassi 已提交
81
/* The name of the firmware file to be loaded
L
Linus Torvalds 已提交
82 83 84 85 86
   over-rides any automatic selection */
static char *firmware = NULL;
module_param(firmware, charp, 0);

/* table of firmware file names */
C
Carlo Perassi 已提交
87
static struct {
L
Linus Torvalds 已提交
88 89 90 91
	AtmelFWType fw_type;
	const char *fw_file;
	const char *fw_file_ext;
} fw_table[] = {
J
John Daiker 已提交
92 93 94 95 96 97 98 99 100
	{ ATMEL_FW_TYPE_502,		"atmel_at76c502",	"bin" },
	{ ATMEL_FW_TYPE_502D,		"atmel_at76c502d",	"bin" },
	{ ATMEL_FW_TYPE_502E,		"atmel_at76c502e",	"bin" },
	{ ATMEL_FW_TYPE_502_3COM,	"atmel_at76c502_3com",	"bin" },
	{ ATMEL_FW_TYPE_504,		"atmel_at76c504",	"bin" },
	{ ATMEL_FW_TYPE_504_2958,	"atmel_at76c504_2958",	"bin" },
	{ ATMEL_FW_TYPE_504A_2958,	"atmel_at76c504a_2958",	"bin" },
	{ ATMEL_FW_TYPE_506,		"atmel_at76c506",	"bin" },
	{ ATMEL_FW_TYPE_NONE,		NULL,			NULL }
L
Linus Torvalds 已提交
101
};
B
Ben Hutchings 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
MODULE_FIRMWARE("atmel_at76c502-wpa.bin");
MODULE_FIRMWARE("atmel_at76c502.bin");
MODULE_FIRMWARE("atmel_at76c502d-wpa.bin");
MODULE_FIRMWARE("atmel_at76c502d.bin");
MODULE_FIRMWARE("atmel_at76c502e-wpa.bin");
MODULE_FIRMWARE("atmel_at76c502e.bin");
MODULE_FIRMWARE("atmel_at76c502_3com-wpa.bin");
MODULE_FIRMWARE("atmel_at76c502_3com.bin");
MODULE_FIRMWARE("atmel_at76c504-wpa.bin");
MODULE_FIRMWARE("atmel_at76c504.bin");
MODULE_FIRMWARE("atmel_at76c504_2958-wpa.bin");
MODULE_FIRMWARE("atmel_at76c504_2958.bin");
MODULE_FIRMWARE("atmel_at76c504a_2958-wpa.bin");
MODULE_FIRMWARE("atmel_at76c504a_2958.bin");
MODULE_FIRMWARE("atmel_at76c506-wpa.bin");
MODULE_FIRMWARE("atmel_at76c506.bin");
L
Linus Torvalds 已提交
118 119 120 121

#define MAX_SSID_LENGTH 32
#define MGMT_JIFFIES (256 * HZ / 100)

C
Carlo Perassi 已提交
122
#define MAX_BSS_ENTRIES	64
L
Linus Torvalds 已提交
123 124

/* registers */
J
John Daiker 已提交
125 126
#define GCR  0x00    /* (SIR0)  General Configuration Register */
#define BSR  0x02    /* (SIR1)  Bank Switching Select Register */
L
Linus Torvalds 已提交
127 128
#define AR   0x04
#define DR   0x08
J
John Daiker 已提交
129 130 131 132
#define MR1  0x12    /* Mirror Register 1 */
#define MR2  0x14    /* Mirror Register 2 */
#define MR3  0x16    /* Mirror Register 3 */
#define MR4  0x18    /* Mirror Register 4 */
L
Linus Torvalds 已提交
133 134 135 136

#define GPR1                            0x0c
#define GPR2                            0x0e
#define GPR3                            0x10
J
John Daiker 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/*
 * Constants for the GCR register.
 */
#define GCR_REMAP     0x0400          /* Remap internal SRAM to 0 */
#define GCR_SWRES     0x0080          /* BIU reset (ARM and PAI are NOT reset) */
#define GCR_CORES     0x0060          /* Core Reset (ARM and PAI are reset) */
#define GCR_ENINT     0x0002          /* Enable Interrupts */
#define GCR_ACKINT    0x0008          /* Acknowledge Interrupts */

#define BSS_SRAM      0x0200          /* AMBA module selection --> SRAM */
#define BSS_IRAM      0x0100          /* AMBA module selection --> IRAM */
/*
 *Constants for the MR registers.
 */
#define MAC_INIT_COMPLETE       0x0001        /* MAC init has been completed */
#define MAC_BOOT_COMPLETE       0x0010        /* MAC boot has been completed */
#define MAC_INIT_OK             0x0002        /* MAC boot has been completed */
L
Linus Torvalds 已提交
154 155 156 157 158

#define MIB_MAX_DATA_BYTES    212
#define MIB_HEADER_SIZE       4    /* first four fields */

struct get_set_mib {
J
John Daiker 已提交
159 160 161 162 163
	u8 type;
	u8 size;
	u8 index;
	u8 reserved;
	u8 data[MIB_MAX_DATA_BYTES];
L
Linus Torvalds 已提交
164 165 166
};

struct rx_desc {
J
John Daiker 已提交
167 168 169 170 171 172 173 174 175 176 177 178
	u32          Next;
	u16          MsduPos;
	u16          MsduSize;

	u8           State;
	u8           Status;
	u8           Rate;
	u8           Rssi;
	u8           LinkQuality;
	u8           PreambleType;
	u16          Duration;
	u32          RxTime;
L
Linus Torvalds 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
};

#define RX_DESC_FLAG_VALID       0x80
#define RX_DESC_FLAG_CONSUMED    0x40
#define RX_DESC_FLAG_IDLE        0x00

#define RX_STATUS_SUCCESS        0x00

#define RX_DESC_MSDU_POS_OFFSET      4
#define RX_DESC_MSDU_SIZE_OFFSET     6
#define RX_DESC_FLAGS_OFFSET         8
#define RX_DESC_STATUS_OFFSET        9
#define RX_DESC_RSSI_OFFSET          11
#define RX_DESC_LINK_QUALITY_OFFSET  12
#define RX_DESC_PREAMBLE_TYPE_OFFSET 13
#define RX_DESC_DURATION_OFFSET      14
#define RX_DESC_RX_TIME_OFFSET       16

struct tx_desc {
	u32       NextDescriptor;
	u16       TxStartOfFrame;
	u16       TxLength;
C
Carlo Perassi 已提交
201

L
Linus Torvalds 已提交
202 203 204
	u8        TxState;
	u8        TxStatus;
	u8        RetryCount;
C
Carlo Perassi 已提交
205

L
Linus Torvalds 已提交
206 207 208 209 210
	u8        TxRate;

	u8        KeyIndex;
	u8        ChiperType;
	u8        ChipreLength;
J
John Daiker 已提交
211
	u8        Reserved1;
L
Linus Torvalds 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

	u8        Reserved;
	u8        PacketType;
	u16       HostTxLength;
};

#define TX_DESC_NEXT_OFFSET          0
#define TX_DESC_POS_OFFSET           4
#define TX_DESC_SIZE_OFFSET          6
#define TX_DESC_FLAGS_OFFSET         8
#define TX_DESC_STATUS_OFFSET        9
#define TX_DESC_RETRY_OFFSET         10
#define TX_DESC_RATE_OFFSET          11
#define TX_DESC_KEY_INDEX_OFFSET     12
#define TX_DESC_CIPHER_TYPE_OFFSET   13
#define TX_DESC_CIPHER_LENGTH_OFFSET 14
#define TX_DESC_PACKET_TYPE_OFFSET   17
#define TX_DESC_HOST_LENGTH_OFFSET   18

J
John Daiker 已提交
231 232 233
/*
 * Host-MAC interface
 */
L
Linus Torvalds 已提交
234 235 236 237 238 239 240 241 242 243 244

#define TX_STATUS_SUCCESS       0x00

#define TX_FIRM_OWN             0x80
#define TX_DONE                 0x40

#define TX_ERROR                0x01

#define TX_PACKET_TYPE_DATA     0x01
#define TX_PACKET_TYPE_MGMT     0x02

J
John Daiker 已提交
245 246 247 248 249 250 251 252
#define ISR_EMPTY               0x00        /* no bits set in ISR */
#define ISR_TxCOMPLETE          0x01        /* packet transmitted */
#define ISR_RxCOMPLETE          0x02        /* packet received */
#define ISR_RxFRAMELOST         0x04        /* Rx Frame lost */
#define ISR_FATAL_ERROR         0x08        /* Fatal error */
#define ISR_COMMAND_COMPLETE    0x10        /* command completed */
#define ISR_OUT_OF_RANGE        0x20        /* command completed */
#define ISR_IBSS_MERGE          0x40        /* (4.1.2.30): IBSS merge */
C
Carlo Perassi 已提交
253
#define ISR_GENERIC_IRQ         0x80
L
Linus Torvalds 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

#define Local_Mib_Type          0x01
#define Mac_Address_Mib_Type    0x02
#define Mac_Mib_Type            0x03
#define Statistics_Mib_Type     0x04
#define Mac_Mgmt_Mib_Type       0x05
#define Mac_Wep_Mib_Type        0x06
#define Phy_Mib_Type            0x07
#define Multi_Domain_MIB        0x08

#define MAC_MGMT_MIB_CUR_BSSID_POS            14
#define MAC_MIB_FRAG_THRESHOLD_POS            8
#define MAC_MIB_RTS_THRESHOLD_POS             10
#define MAC_MIB_SHORT_RETRY_POS               16
#define MAC_MIB_LONG_RETRY_POS                17
#define MAC_MIB_SHORT_RETRY_LIMIT_POS         16
#define MAC_MGMT_MIB_BEACON_PER_POS           0
#define MAC_MGMT_MIB_STATION_ID_POS           6
#define MAC_MGMT_MIB_CUR_PRIVACY_POS          11
#define MAC_MGMT_MIB_CUR_BSSID_POS            14
#define MAC_MGMT_MIB_PS_MODE_POS              53
#define MAC_MGMT_MIB_LISTEN_INTERVAL_POS      54
#define MAC_MGMT_MIB_MULTI_DOMAIN_IMPLEMENTED 56
#define MAC_MGMT_MIB_MULTI_DOMAIN_ENABLED     57
#define PHY_MIB_CHANNEL_POS                   14
#define PHY_MIB_RATE_SET_POS                  20
#define PHY_MIB_REG_DOMAIN_POS                26
#define LOCAL_MIB_AUTO_TX_RATE_POS            3
#define LOCAL_MIB_SSID_SIZE                   5
#define LOCAL_MIB_TX_PROMISCUOUS_POS          6
#define LOCAL_MIB_TX_MGMT_RATE_POS            7
#define LOCAL_MIB_TX_CONTROL_RATE_POS         8
#define LOCAL_MIB_PREAMBLE_TYPE               9
#define MAC_ADDR_MIB_MAC_ADDR_POS             0

#define         CMD_Set_MIB_Vars              0x01
#define         CMD_Get_MIB_Vars              0x02
#define         CMD_Scan                      0x03
#define         CMD_Join                      0x04
#define         CMD_Start                     0x05
#define         CMD_EnableRadio               0x06
#define         CMD_DisableRadio              0x07
#define         CMD_SiteSurvey                0x0B

#define         CMD_STATUS_IDLE                   0x00
#define         CMD_STATUS_COMPLETE               0x01
#define         CMD_STATUS_UNKNOWN                0x02
#define         CMD_STATUS_INVALID_PARAMETER      0x03
#define         CMD_STATUS_FUNCTION_NOT_SUPPORTED 0x04
#define         CMD_STATUS_TIME_OUT               0x07
#define         CMD_STATUS_IN_PROGRESS            0x08
#define         CMD_STATUS_REJECTED_RADIO_OFF     0x09
#define         CMD_STATUS_HOST_ERROR             0xFF
#define         CMD_STATUS_BUSY                   0xFE

#define CMD_BLOCK_COMMAND_OFFSET        0
#define CMD_BLOCK_STATUS_OFFSET         1
#define CMD_BLOCK_PARAMETERS_OFFSET     4

#define SCAN_OPTIONS_SITE_SURVEY        0x80

#define MGMT_FRAME_BODY_OFFSET		24
#define MAX_AUTHENTICATION_RETRIES	3
C
Carlo Perassi 已提交
317
#define MAX_ASSOCIATION_RETRIES		3
L
Linus Torvalds 已提交
318 319 320 321 322 323

#define AUTHENTICATION_RESPONSE_TIME_OUT  1000

#define MAX_WIRELESS_BODY  2316 /* mtu is 2312, CRC is 4 */
#define LOOP_RETRY_LIMIT   500000

C
Carlo Perassi 已提交
324 325
#define ACTIVE_MODE	1
#define PS_MODE		2
L
Linus Torvalds 已提交
326 327 328 329

#define MAX_ENCRYPTION_KEYS 4
#define MAX_ENCRYPTION_KEY_SIZE 40

J
John Daiker 已提交
330 331 332
/*
 * 802.11 related definitions
 */
L
Linus Torvalds 已提交
333

J
John Daiker 已提交
334 335 336
/*
 * Regulatory Domains
 */
L
Linus Torvalds 已提交
337

J
John Daiker 已提交
338 339 340 341 342 343 344 345
#define REG_DOMAIN_FCC		0x10	/* Channels	1-11	USA				*/
#define REG_DOMAIN_DOC		0x20	/* Channel	1-11	Canada				*/
#define REG_DOMAIN_ETSI		0x30	/* Channel	1-13	Europe (ex Spain/France)	*/
#define REG_DOMAIN_SPAIN	0x31	/* Channel	10-11	Spain				*/
#define REG_DOMAIN_FRANCE	0x32	/* Channel	10-13	France				*/
#define REG_DOMAIN_MKK		0x40	/* Channel	14	Japan				*/
#define REG_DOMAIN_MKK1		0x41	/* Channel	1-14	Japan(MKK1)			*/
#define REG_DOMAIN_ISRAEL	0x50	/* Channel	3-9	ISRAEL				*/
L
Linus Torvalds 已提交
346

C
Carlo Perassi 已提交
347
#define BSS_TYPE_AD_HOC		1
L
Linus Torvalds 已提交
348 349 350 351 352 353 354 355 356 357 358
#define BSS_TYPE_INFRASTRUCTURE 2

#define SCAN_TYPE_ACTIVE	0
#define SCAN_TYPE_PASSIVE	1

#define LONG_PREAMBLE		0
#define SHORT_PREAMBLE		1
#define AUTO_PREAMBLE		2

#define DATA_FRAME_WS_HEADER_SIZE   30

C
Carlo Perassi 已提交
359
/* promiscuous mode control */
L
Linus Torvalds 已提交
360 361 362 363 364 365 366 367
#define PROM_MODE_OFF			0x0
#define PROM_MODE_UNKNOWN		0x1
#define PROM_MODE_CRC_FAILED		0x2
#define PROM_MODE_DUPLICATED		0x4
#define PROM_MODE_MGMT			0x8
#define PROM_MODE_CTRL			0x10
#define PROM_MODE_BAD_PROTOCOL		0x20

C
Carlo Perassi 已提交
368
#define IFACE_INT_STATUS_OFFSET		0
L
Linus Torvalds 已提交
369 370 371 372 373 374 375
#define IFACE_INT_MASK_OFFSET		1
#define IFACE_LOCKOUT_HOST_OFFSET	2
#define IFACE_LOCKOUT_MAC_OFFSET	3
#define IFACE_FUNC_CTRL_OFFSET		28
#define IFACE_MAC_STAT_OFFSET		30
#define IFACE_GENERIC_INT_TYPE_OFFSET	32

C
Carlo Perassi 已提交
376
#define CIPHER_SUITE_NONE     0
L
Linus Torvalds 已提交
377 378 379 380 381 382
#define CIPHER_SUITE_WEP_64   1
#define CIPHER_SUITE_TKIP     2
#define CIPHER_SUITE_AES      3
#define CIPHER_SUITE_CCX      4
#define CIPHER_SUITE_WEP_128  5

J
John Daiker 已提交
383 384 385
/*
 * IFACE MACROS & definitions
 */
L
Linus Torvalds 已提交
386

J
John Daiker 已提交
387 388 389
/*
 * FuncCtrl field:
 */
L
Linus Torvalds 已提交
390 391
#define FUNC_CTRL_TxENABLE		0x10
#define FUNC_CTRL_RxENABLE		0x20
C
Carlo Perassi 已提交
392
#define FUNC_CTRL_INIT_COMPLETE		0x01
L
Linus Torvalds 已提交
393 394 395 396

/* A stub firmware image which reads the MAC address from NVRAM on the card.
   For copyright information and source see the end of this file. */
static u8 mac_reader[] = {
J
John Daiker 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
	0x06, 0x00, 0x00, 0xea, 0x04, 0x00, 0x00, 0xea, 0x03, 0x00, 0x00, 0xea, 0x02, 0x00, 0x00, 0xea,
	0x01, 0x00, 0x00, 0xea, 0x00, 0x00, 0x00, 0xea, 0xff, 0xff, 0xff, 0xea, 0xfe, 0xff, 0xff, 0xea,
	0xd3, 0x00, 0xa0, 0xe3, 0x00, 0xf0, 0x21, 0xe1, 0x0e, 0x04, 0xa0, 0xe3, 0x00, 0x10, 0xa0, 0xe3,
	0x81, 0x11, 0xa0, 0xe1, 0x00, 0x10, 0x81, 0xe3, 0x00, 0x10, 0x80, 0xe5, 0x1c, 0x10, 0x90, 0xe5,
	0x10, 0x10, 0xc1, 0xe3, 0x1c, 0x10, 0x80, 0xe5, 0x01, 0x10, 0xa0, 0xe3, 0x08, 0x10, 0x80, 0xe5,
	0x02, 0x03, 0xa0, 0xe3, 0x00, 0x10, 0xa0, 0xe3, 0xb0, 0x10, 0xc0, 0xe1, 0xb4, 0x10, 0xc0, 0xe1,
	0xb8, 0x10, 0xc0, 0xe1, 0xbc, 0x10, 0xc0, 0xe1, 0x56, 0xdc, 0xa0, 0xe3, 0x21, 0x00, 0x00, 0xeb,
	0x0a, 0x00, 0xa0, 0xe3, 0x1a, 0x00, 0x00, 0xeb, 0x10, 0x00, 0x00, 0xeb, 0x07, 0x00, 0x00, 0xeb,
	0x02, 0x03, 0xa0, 0xe3, 0x02, 0x14, 0xa0, 0xe3, 0xb4, 0x10, 0xc0, 0xe1, 0x4c, 0x10, 0x9f, 0xe5,
	0xbc, 0x10, 0xc0, 0xe1, 0x10, 0x10, 0xa0, 0xe3, 0xb8, 0x10, 0xc0, 0xe1, 0xfe, 0xff, 0xff, 0xea,
	0x00, 0x40, 0x2d, 0xe9, 0x00, 0x20, 0xa0, 0xe3, 0x02, 0x3c, 0xa0, 0xe3, 0x00, 0x10, 0xa0, 0xe3,
	0x28, 0x00, 0x9f, 0xe5, 0x37, 0x00, 0x00, 0xeb, 0x00, 0x40, 0xbd, 0xe8, 0x1e, 0xff, 0x2f, 0xe1,
	0x00, 0x40, 0x2d, 0xe9, 0x12, 0x2e, 0xa0, 0xe3, 0x06, 0x30, 0xa0, 0xe3, 0x00, 0x10, 0xa0, 0xe3,
	0x02, 0x04, 0xa0, 0xe3, 0x2f, 0x00, 0x00, 0xeb, 0x00, 0x40, 0xbd, 0xe8, 0x1e, 0xff, 0x2f, 0xe1,
	0x00, 0x02, 0x00, 0x02, 0x80, 0x01, 0x90, 0xe0, 0x01, 0x00, 0x00, 0x0a, 0x01, 0x00, 0x50, 0xe2,
	0xfc, 0xff, 0xff, 0xea, 0x1e, 0xff, 0x2f, 0xe1, 0x80, 0x10, 0xa0, 0xe3, 0xf3, 0x06, 0xa0, 0xe3,
	0x00, 0x10, 0x80, 0xe5, 0x00, 0x10, 0xa0, 0xe3, 0x00, 0x10, 0x80, 0xe5, 0x01, 0x10, 0xa0, 0xe3,
	0x04, 0x10, 0x80, 0xe5, 0x00, 0x10, 0x80, 0xe5, 0x0e, 0x34, 0xa0, 0xe3, 0x1c, 0x10, 0x93, 0xe5,
	0x02, 0x1a, 0x81, 0xe3, 0x1c, 0x10, 0x83, 0xe5, 0x58, 0x11, 0x9f, 0xe5, 0x30, 0x10, 0x80, 0xe5,
	0x54, 0x11, 0x9f, 0xe5, 0x34, 0x10, 0x80, 0xe5, 0x38, 0x10, 0x80, 0xe5, 0x3c, 0x10, 0x80, 0xe5,
	0x10, 0x10, 0x90, 0xe5, 0x08, 0x00, 0x90, 0xe5, 0x1e, 0xff, 0x2f, 0xe1, 0xf3, 0x16, 0xa0, 0xe3,
	0x08, 0x00, 0x91, 0xe5, 0x05, 0x00, 0xa0, 0xe3, 0x0c, 0x00, 0x81, 0xe5, 0x10, 0x00, 0x91, 0xe5,
	0x02, 0x00, 0x10, 0xe3, 0xfc, 0xff, 0xff, 0x0a, 0xff, 0x00, 0xa0, 0xe3, 0x0c, 0x00, 0x81, 0xe5,
	0x10, 0x00, 0x91, 0xe5, 0x02, 0x00, 0x10, 0xe3, 0xfc, 0xff, 0xff, 0x0a, 0x08, 0x00, 0x91, 0xe5,
	0x10, 0x00, 0x91, 0xe5, 0x01, 0x00, 0x10, 0xe3, 0xfc, 0xff, 0xff, 0x0a, 0x08, 0x00, 0x91, 0xe5,
	0xff, 0x00, 0x00, 0xe2, 0x1e, 0xff, 0x2f, 0xe1, 0x30, 0x40, 0x2d, 0xe9, 0x00, 0x50, 0xa0, 0xe1,
	0x03, 0x40, 0xa0, 0xe1, 0xa2, 0x02, 0xa0, 0xe1, 0x08, 0x00, 0x00, 0xe2, 0x03, 0x00, 0x80, 0xe2,
	0xd8, 0x10, 0x9f, 0xe5, 0x00, 0x00, 0xc1, 0xe5, 0x01, 0x20, 0xc1, 0xe5, 0xe2, 0xff, 0xff, 0xeb,
	0x01, 0x00, 0x10, 0xe3, 0xfc, 0xff, 0xff, 0x1a, 0x14, 0x00, 0xa0, 0xe3, 0xc4, 0xff, 0xff, 0xeb,
	0x04, 0x20, 0xa0, 0xe1, 0x05, 0x10, 0xa0, 0xe1, 0x02, 0x00, 0xa0, 0xe3, 0x01, 0x00, 0x00, 0xeb,
	0x30, 0x40, 0xbd, 0xe8, 0x1e, 0xff, 0x2f, 0xe1, 0x70, 0x40, 0x2d, 0xe9, 0xf3, 0x46, 0xa0, 0xe3,
	0x00, 0x30, 0xa0, 0xe3, 0x00, 0x00, 0x50, 0xe3, 0x08, 0x00, 0x00, 0x9a, 0x8c, 0x50, 0x9f, 0xe5,
	0x03, 0x60, 0xd5, 0xe7, 0x0c, 0x60, 0x84, 0xe5, 0x10, 0x60, 0x94, 0xe5, 0x02, 0x00, 0x16, 0xe3,
	0xfc, 0xff, 0xff, 0x0a, 0x01, 0x30, 0x83, 0xe2, 0x00, 0x00, 0x53, 0xe1, 0xf7, 0xff, 0xff, 0x3a,
	0xff, 0x30, 0xa0, 0xe3, 0x0c, 0x30, 0x84, 0xe5, 0x08, 0x00, 0x94, 0xe5, 0x10, 0x00, 0x94, 0xe5,
	0x01, 0x00, 0x10, 0xe3, 0xfc, 0xff, 0xff, 0x0a, 0x08, 0x00, 0x94, 0xe5, 0x00, 0x00, 0xa0, 0xe3,
	0x00, 0x00, 0x52, 0xe3, 0x0b, 0x00, 0x00, 0x9a, 0x10, 0x50, 0x94, 0xe5, 0x02, 0x00, 0x15, 0xe3,
	0xfc, 0xff, 0xff, 0x0a, 0x0c, 0x30, 0x84, 0xe5, 0x10, 0x50, 0x94, 0xe5, 0x01, 0x00, 0x15, 0xe3,
	0xfc, 0xff, 0xff, 0x0a, 0x08, 0x50, 0x94, 0xe5, 0x01, 0x50, 0xc1, 0xe4, 0x01, 0x00, 0x80, 0xe2,
	0x02, 0x00, 0x50, 0xe1, 0xf3, 0xff, 0xff, 0x3a, 0xc8, 0x00, 0xa0, 0xe3, 0x98, 0xff, 0xff, 0xeb,
	0x70, 0x40, 0xbd, 0xe8, 0x1e, 0xff, 0x2f, 0xe1, 0x01, 0x0c, 0x00, 0x02, 0x01, 0x02, 0x00, 0x02,
	0x00, 0x01, 0x00, 0x02
L
Linus Torvalds 已提交
439 440 441
};

struct atmel_private {
L
Lucas De Marchi 已提交
442
	void *card; /* Bus dependent structure varies for PCcard */
L
Linus Torvalds 已提交
443 444 445 446 447 448 449 450 451
	int (*present_callback)(void *); /* And callback which uses it */
	char firmware_id[32];
	AtmelFWType firmware_type;
	u8 *firmware;
	int firmware_length;
	struct timer_list management_timer;
	struct net_device *dev;
	struct device *sys_dev;
	struct iw_statistics wstats;
J
John Daiker 已提交
452
	spinlock_t irqlock, timerlock;	/* spinlocks */
L
Linus Torvalds 已提交
453
	enum { BUS_TYPE_PCCARD, BUS_TYPE_PCI } bus_type;
C
Carlo Perassi 已提交
454 455
	enum {
		CARD_TYPE_PARALLEL_FLASH,
L
Linus Torvalds 已提交
456
		CARD_TYPE_SPI_FLASH,
C
Carlo Perassi 已提交
457
		CARD_TYPE_EEPROM
L
Linus Torvalds 已提交
458 459 460 461 462 463 464
	} card_type;
	int do_rx_crc; /* If we need to CRC incoming packets */
	int probe_crc; /* set if we don't yet know */
	int crc_ok_cnt, crc_ko_cnt; /* counters for probing */
	u16 rx_desc_head;
	u16 tx_desc_free, tx_desc_head, tx_desc_tail, tx_desc_previous;
	u16 tx_free_mem, tx_buff_head, tx_buff_tail;
C
Carlo Perassi 已提交
465

L
Linus Torvalds 已提交
466
	u16 frag_seq, frag_len, frag_no;
C
Carlo Perassi 已提交
467 468
	u8 frag_source[6];

L
Linus Torvalds 已提交
469 470 471
	u8 wep_is_on, default_key, exclude_unencrypted, encryption_level;
	u8 group_cipher_suite, pairwise_cipher_suite;
	u8 wep_keys[MAX_ENCRYPTION_KEYS][MAX_ENCRYPTION_KEY_SIZE];
C
Carlo Perassi 已提交
472
	int wep_key_len[MAX_ENCRYPTION_KEYS];
L
Linus Torvalds 已提交
473 474 475
	int use_wpa, radio_on_broken; /* firmware dependent stuff. */

	u16 host_info_base;
C
Carlo Perassi 已提交
476
	struct host_info_struct {
L
Linus Torvalds 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
		/* NB this is matched to the hardware, don't change. */
		u8 volatile int_status;
		u8 volatile int_mask;
		u8 volatile lockout_host;
		u8 volatile lockout_mac;

		u16 tx_buff_pos;
		u16 tx_buff_size;
		u16 tx_desc_pos;
		u16 tx_desc_count;

		u16 rx_buff_pos;
		u16 rx_buff_size;
		u16 rx_desc_pos;
		u16 rx_desc_count;
C
Carlo Perassi 已提交
492

L
Linus Torvalds 已提交
493
		u16 build_version;
C
Carlo Perassi 已提交
494 495
		u16 command_pos;

L
Linus Torvalds 已提交
496 497
		u16 major_version;
		u16 minor_version;
C
Carlo Perassi 已提交
498

L
Linus Torvalds 已提交
499 500 501 502 503 504
		u16 func_ctrl;
		u16 mac_status;
		u16 generic_IRQ_type;
		u8  reserved[2];
	} host_info;

C
Carlo Perassi 已提交
505
	enum {
L
Linus Torvalds 已提交
506 507 508 509 510 511 512 513 514
		STATION_STATE_SCANNING,
		STATION_STATE_JOINNING,
		STATION_STATE_AUTHENTICATING,
		STATION_STATE_ASSOCIATING,
		STATION_STATE_READY,
		STATION_STATE_REASSOCIATING,
		STATION_STATE_DOWN,
		STATION_STATE_MGMT_ERROR
	} station_state;
C
Carlo Perassi 已提交
515

L
Linus Torvalds 已提交
516 517 518 519 520 521 522 523 524 525 526 527
	int operating_mode, power_mode;
	time_t last_qual;
	int beacons_this_sec;
	int channel;
	int reg_domain, config_reg_domain;
	int tx_rate;
	int auto_tx_rate;
	int rts_threshold;
	int frag_threshold;
	int long_retry, short_retry;
	int preamble;
	int default_beacon_period, beacon_period, listen_interval;
C
Carlo Perassi 已提交
528
	int CurrentAuthentTransactionSeqNum, ExpectedAuthentTransactionSeqNum;
L
Linus Torvalds 已提交
529 530 531 532
	int AuthenticationRequestRetryCnt, AssociationRequestRetryCnt, ReAssociationRequestRetryCnt;
	enum {
		SITE_SURVEY_IDLE,
		SITE_SURVEY_IN_PROGRESS,
C
Carlo Perassi 已提交
533
		SITE_SURVEY_COMPLETED
L
Linus Torvalds 已提交
534
	} site_survey_state;
535
	unsigned long last_survey;
L
Linus Torvalds 已提交
536 537 538

	int station_was_associated, station_is_associated;
	int fast_scan;
C
Carlo Perassi 已提交
539

L
Linus Torvalds 已提交
540 541 542 543 544 545 546 547 548 549 550 551
	struct bss_info {
		int channel;
		int SSIDsize;
		int RSSI;
		int UsingWEP;
		int preamble;
		int beacon_period;
		int BSStype;
		u8 BSSID[6];
		u8 SSID[MAX_SSID_LENGTH];
	} BSSinfo[MAX_BSS_ENTRIES];
	int BSS_list_entries, current_BSS;
C
Carlo Perassi 已提交
552
	int connect_to_any_BSS;
L
Linus Torvalds 已提交
553 554 555 556 557 558 559
	int SSID_size, new_SSID_size;
	u8 CurrentBSSID[6], BSSID[6];
	u8 SSID[MAX_SSID_LENGTH], new_SSID[MAX_SSID_LENGTH];
	u64 last_beacon_timestamp;
	u8 rx_buf[MAX_WIRELESS_BODY];
};

J
John Daiker 已提交
560
static u8 atmel_basic_rates[4] = {0x82, 0x84, 0x0b, 0x16};
L
Linus Torvalds 已提交
561 562 563 564

static const struct {
	int reg_domain;
	int min, max;
C
Carlo Perassi 已提交
565
	char *name;
L
Linus Torvalds 已提交
566 567 568 569
} channel_table[] = { { REG_DOMAIN_FCC, 1, 11, "USA" },
		      { REG_DOMAIN_DOC, 1, 11, "Canada" },
		      { REG_DOMAIN_ETSI, 1, 13, "Europe" },
		      { REG_DOMAIN_SPAIN, 10, 11, "Spain" },
C
Carlo Perassi 已提交
570
		      { REG_DOMAIN_FRANCE, 10, 13, "France" },
L
Linus Torvalds 已提交
571 572 573 574 575 576
		      { REG_DOMAIN_MKK, 14, 14, "MKK" },
		      { REG_DOMAIN_MKK1, 1, 14, "MKK1" },
		      { REG_DOMAIN_ISRAEL, 3, 9, "Israel"} };

static void build_wpa_mib(struct atmel_private *priv);
static int atmel_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
C
Carlo Perassi 已提交
577
static void atmel_copy_to_card(struct net_device *dev, u16 dest,
578
			       const unsigned char *src, u16 len);
C
Carlo Perassi 已提交
579 580
static void atmel_copy_to_host(struct net_device *dev, unsigned char *dest,
			       u16 src, u16 len);
L
Linus Torvalds 已提交
581 582 583 584 585 586
static void atmel_set_gcr(struct net_device *dev, u16 mask);
static void atmel_clear_gcr(struct net_device *dev, u16 mask);
static int atmel_lock_mac(struct atmel_private *priv);
static void atmel_wmem32(struct atmel_private *priv, u16 pos, u32 data);
static void atmel_command_irq(struct atmel_private *priv);
static int atmel_validate_channel(struct atmel_private *priv, int channel);
C
Carlo Perassi 已提交
587
static void atmel_management_frame(struct atmel_private *priv,
J
Johannes Berg 已提交
588
				   struct ieee80211_hdr *header,
L
Linus Torvalds 已提交
589 590
				   u16 frame_len, u8 rssi);
static void atmel_management_timer(u_long a);
C
Carlo Perassi 已提交
591 592 593 594 595
static void atmel_send_command(struct atmel_private *priv, int command,
			       void *cmd, int cmd_size);
static int atmel_send_command_wait(struct atmel_private *priv, int command,
				   void *cmd, int cmd_size);
static void atmel_transmit_management_frame(struct atmel_private *priv,
J
Johannes Berg 已提交
596
					    struct ieee80211_hdr *header,
L
Linus Torvalds 已提交
597 598 599
					    u8 *body, int body_len);

static u8 atmel_get_mib8(struct atmel_private *priv, u8 type, u8 index);
C
Carlo Perassi 已提交
600 601 602 603 604 605 606 607
static void atmel_set_mib8(struct atmel_private *priv, u8 type, u8 index,
			   u8 data);
static void atmel_set_mib16(struct atmel_private *priv, u8 type, u8 index,
			    u16 data);
static void atmel_set_mib(struct atmel_private *priv, u8 type, u8 index,
			  u8 *data, int data_len);
static void atmel_get_mib(struct atmel_private *priv, u8 type, u8 index,
			  u8 *data, int data_len);
L
Linus Torvalds 已提交
608 609 610 611 612
static void atmel_scan(struct atmel_private *priv, int specific_ssid);
static void atmel_join_bss(struct atmel_private *priv, int bss_index);
static void atmel_smooth_qual(struct atmel_private *priv);
static void atmel_writeAR(struct net_device *dev, u16 data);
static int probe_atmel_card(struct net_device *dev);
613
static int reset_atmel_card(struct net_device *dev);
L
Linus Torvalds 已提交
614 615 616 617 618 619 620 621 622 623 624 625 626
static void atmel_enter_state(struct atmel_private *priv, int new_state);
int atmel_open (struct net_device *dev);

static inline u16 atmel_hi(struct atmel_private *priv, u16 offset)
{
	return priv->host_info_base + offset;
}

static inline u16 atmel_co(struct atmel_private *priv, u16 offset)
{
	return priv->host_info.command_pos + offset;
}

C
Carlo Perassi 已提交
627
static inline u16 atmel_rx(struct atmel_private *priv, u16 offset, u16 desc)
L
Linus Torvalds 已提交
628 629 630 631
{
	return priv->host_info.rx_desc_pos + (sizeof(struct rx_desc) * desc) + offset;
}

C
Carlo Perassi 已提交
632
static inline u16 atmel_tx(struct atmel_private *priv, u16 offset, u16 desc)
L
Linus Torvalds 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
{
	return priv->host_info.tx_desc_pos + (sizeof(struct tx_desc) * desc) + offset;
}

static inline u8 atmel_read8(struct net_device *dev, u16 offset)
{
	return inb(dev->base_addr + offset);
}

static inline void atmel_write8(struct net_device *dev, u16 offset, u8 data)
{
	outb(data, dev->base_addr + offset);
}

static inline u16 atmel_read16(struct net_device *dev, u16 offset)
{
	return inw(dev->base_addr + offset);
}

static inline void atmel_write16(struct net_device *dev, u16 offset, u16 data)
{
	outw(data, dev->base_addr + offset);
}

static inline u8 atmel_rmem8(struct atmel_private *priv, u16 pos)
{
C
Carlo Perassi 已提交
659
	atmel_writeAR(priv->dev, pos);
L
Linus Torvalds 已提交
660 661 662 663 664
	return atmel_read8(priv->dev, DR);
}

static inline void atmel_wmem8(struct atmel_private *priv, u16 pos, u16 data)
{
C
Carlo Perassi 已提交
665
	atmel_writeAR(priv->dev, pos);
L
Linus Torvalds 已提交
666 667 668 669 670
	atmel_write8(priv->dev, DR, data);
}

static inline u16 atmel_rmem16(struct atmel_private *priv, u16 pos)
{
C
Carlo Perassi 已提交
671
	atmel_writeAR(priv->dev, pos);
L
Linus Torvalds 已提交
672 673 674 675 676
	return atmel_read16(priv->dev, DR);
}

static inline void atmel_wmem16(struct atmel_private *priv, u16 pos, u16 data)
{
C
Carlo Perassi 已提交
677
	atmel_writeAR(priv->dev, pos);
L
Linus Torvalds 已提交
678 679 680 681 682 683 684 685 686
	atmel_write16(priv->dev, DR, data);
}

static const struct iw_handler_def atmel_handler_def;

static void tx_done_irq(struct atmel_private *priv)
{
	int i;

C
Carlo Perassi 已提交
687
	for (i = 0;
L
Linus Torvalds 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
	     atmel_rmem8(priv, atmel_tx(priv, TX_DESC_FLAGS_OFFSET, priv->tx_desc_head)) == TX_DONE &&
		     i < priv->host_info.tx_desc_count;
	     i++) {
		u8 status = atmel_rmem8(priv, atmel_tx(priv, TX_DESC_STATUS_OFFSET, priv->tx_desc_head));
		u16 msdu_size = atmel_rmem16(priv, atmel_tx(priv, TX_DESC_SIZE_OFFSET, priv->tx_desc_head));
		u8 type = atmel_rmem8(priv, atmel_tx(priv, TX_DESC_PACKET_TYPE_OFFSET, priv->tx_desc_head));

		atmel_wmem8(priv, atmel_tx(priv, TX_DESC_FLAGS_OFFSET, priv->tx_desc_head), 0);

		priv->tx_free_mem += msdu_size;
		priv->tx_desc_free++;

		if (priv->tx_buff_head + msdu_size > (priv->host_info.tx_buff_pos + priv->host_info.tx_buff_size))
			priv->tx_buff_head = 0;
		else
			priv->tx_buff_head += msdu_size;
C
Carlo Perassi 已提交
704

L
Linus Torvalds 已提交
705
		if (priv->tx_desc_head < (priv->host_info.tx_desc_count - 1))
C
Carlo Perassi 已提交
706
			priv->tx_desc_head++ ;
L
Linus Torvalds 已提交
707 708
		else
			priv->tx_desc_head = 0;
C
Carlo Perassi 已提交
709

L
Linus Torvalds 已提交
710 711
		if (type == TX_PACKET_TYPE_DATA) {
			if (status == TX_STATUS_SUCCESS)
712
				priv->dev->stats.tx_packets++;
C
Carlo Perassi 已提交
713
			else
714
				priv->dev->stats.tx_errors++;
L
Linus Torvalds 已提交
715 716 717 718 719 720 721 722 723
			netif_wake_queue(priv->dev);
		}
	}
}

static u16 find_tx_buff(struct atmel_private *priv, u16 len)
{
	u16 bottom_free = priv->host_info.tx_buff_size - priv->tx_buff_tail;

C
Carlo Perassi 已提交
724
	if (priv->tx_desc_free == 3 || priv->tx_free_mem < len)
L
Linus Torvalds 已提交
725
		return 0;
C
Carlo Perassi 已提交
726

L
Linus Torvalds 已提交
727 728
	if (bottom_free >= len)
		return priv->host_info.tx_buff_pos + priv->tx_buff_tail;
C
Carlo Perassi 已提交
729

L
Linus Torvalds 已提交
730 731 732 733
	if (priv->tx_free_mem - bottom_free >= len) {
		priv->tx_buff_tail = 0;
		return priv->host_info.tx_buff_pos;
	}
C
Carlo Perassi 已提交
734

L
Linus Torvalds 已提交
735 736 737
	return 0;
}

C
Carlo Perassi 已提交
738 739
static void tx_update_descriptor(struct atmel_private *priv, int is_bcast,
				 u16 len, u16 buff, u8 type)
L
Linus Torvalds 已提交
740 741 742 743 744 745 746 747 748 749 750 751
{
	atmel_wmem16(priv, atmel_tx(priv, TX_DESC_POS_OFFSET, priv->tx_desc_tail), buff);
	atmel_wmem16(priv, atmel_tx(priv, TX_DESC_SIZE_OFFSET, priv->tx_desc_tail), len);
	if (!priv->use_wpa)
		atmel_wmem16(priv, atmel_tx(priv, TX_DESC_HOST_LENGTH_OFFSET, priv->tx_desc_tail), len);
	atmel_wmem8(priv, atmel_tx(priv, TX_DESC_PACKET_TYPE_OFFSET, priv->tx_desc_tail), type);
	atmel_wmem8(priv, atmel_tx(priv, TX_DESC_RATE_OFFSET, priv->tx_desc_tail), priv->tx_rate);
	atmel_wmem8(priv, atmel_tx(priv, TX_DESC_RETRY_OFFSET, priv->tx_desc_tail), 0);
	if (priv->use_wpa) {
		int cipher_type, cipher_length;
		if (is_bcast) {
			cipher_type = priv->group_cipher_suite;
C
Carlo Perassi 已提交
752 753
			if (cipher_type == CIPHER_SUITE_WEP_64 ||
			    cipher_type == CIPHER_SUITE_WEP_128)
L
Linus Torvalds 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766
				cipher_length = 8;
			else if (cipher_type == CIPHER_SUITE_TKIP)
				cipher_length = 12;
			else if (priv->pairwise_cipher_suite == CIPHER_SUITE_WEP_64 ||
				 priv->pairwise_cipher_suite == CIPHER_SUITE_WEP_128) {
				cipher_type = priv->pairwise_cipher_suite;
				cipher_length = 8;
			} else {
				cipher_type = CIPHER_SUITE_NONE;
				cipher_length = 0;
			}
		} else {
			cipher_type = priv->pairwise_cipher_suite;
C
Carlo Perassi 已提交
767 768
			if (cipher_type == CIPHER_SUITE_WEP_64 ||
			    cipher_type == CIPHER_SUITE_WEP_128)
L
Linus Torvalds 已提交
769 770 771 772 773 774 775 776 777 778 779 780
				cipher_length = 8;
			else if (cipher_type == CIPHER_SUITE_TKIP)
				cipher_length = 12;
			else if (priv->group_cipher_suite == CIPHER_SUITE_WEP_64 ||
				 priv->group_cipher_suite == CIPHER_SUITE_WEP_128) {
				cipher_type = priv->group_cipher_suite;
				cipher_length = 8;
			} else {
				cipher_type = CIPHER_SUITE_NONE;
				cipher_length = 0;
			}
		}
C
Carlo Perassi 已提交
781

L
Linus Torvalds 已提交
782
		atmel_wmem8(priv, atmel_tx(priv, TX_DESC_CIPHER_TYPE_OFFSET, priv->tx_desc_tail),
C
Carlo Perassi 已提交
783
			    cipher_type);
L
Linus Torvalds 已提交
784 785 786 787 788 789 790 791
		atmel_wmem8(priv, atmel_tx(priv, TX_DESC_CIPHER_LENGTH_OFFSET, priv->tx_desc_tail),
			    cipher_length);
	}
	atmel_wmem32(priv, atmel_tx(priv, TX_DESC_NEXT_OFFSET, priv->tx_desc_tail), 0x80000000L);
	atmel_wmem8(priv, atmel_tx(priv, TX_DESC_FLAGS_OFFSET, priv->tx_desc_tail), TX_FIRM_OWN);
	if (priv->tx_desc_previous != priv->tx_desc_tail)
		atmel_wmem32(priv, atmel_tx(priv, TX_DESC_NEXT_OFFSET, priv->tx_desc_previous), 0);
	priv->tx_desc_previous = priv->tx_desc_tail;
C
Carlo Perassi 已提交
792
	if (priv->tx_desc_tail < (priv->host_info.tx_desc_count - 1))
L
Linus Torvalds 已提交
793 794 795 796 797 798 799
		priv->tx_desc_tail++;
	else
		priv->tx_desc_tail = 0;
	priv->tx_desc_free--;
	priv->tx_free_mem -= len;
}

800
static netdev_tx_t start_tx(struct sk_buff *skb, struct net_device *dev)
L
Linus Torvalds 已提交
801
{
802
	static const u8 SNAP_RFC1024[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
L
Linus Torvalds 已提交
803
	struct atmel_private *priv = netdev_priv(dev);
J
Johannes Berg 已提交
804
	struct ieee80211_hdr header;
L
Linus Torvalds 已提交
805 806
	unsigned long flags;
	u16 buff, frame_ctl, len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
C
Carlo Perassi 已提交
807 808

	if (priv->card && priv->present_callback &&
L
Linus Torvalds 已提交
809
	    !(*priv->present_callback)(priv->card)) {
810
		dev->stats.tx_errors++;
L
Linus Torvalds 已提交
811
		dev_kfree_skb(skb);
812
		return NETDEV_TX_OK;
L
Linus Torvalds 已提交
813
	}
C
Carlo Perassi 已提交
814

L
Linus Torvalds 已提交
815
	if (priv->station_state != STATION_STATE_READY) {
816
		dev->stats.tx_errors++;
L
Linus Torvalds 已提交
817
		dev_kfree_skb(skb);
818
		return NETDEV_TX_OK;
L
Linus Torvalds 已提交
819
	}
C
Carlo Perassi 已提交
820

L
Linus Torvalds 已提交
821
	/* first ensure the timer func cannot run */
C
Carlo Perassi 已提交
822
	spin_lock_bh(&priv->timerlock);
L
Linus Torvalds 已提交
823
	/* then stop the hardware ISR */
C
Carlo Perassi 已提交
824
	spin_lock_irqsave(&priv->irqlock, flags);
L
Linus Torvalds 已提交
825
	/* nb doing the above in the opposite order will deadlock */
C
Carlo Perassi 已提交
826

L
Linus Torvalds 已提交
827
	/* The Wireless Header is 30 bytes. In the Ethernet packet we "cut" the
C
Carlo Perassi 已提交
828 829 830 831
	   12 first bytes (containing DA/SA) and put them in the appropriate
	   fields of the Wireless Header. Thus the packet length is then the
	   initial + 18 (+30-12) */

L
Linus Torvalds 已提交
832
	if (!(buff = find_tx_buff(priv, len + 18))) {
833
		dev->stats.tx_dropped++;
L
Linus Torvalds 已提交
834 835 836
		spin_unlock_irqrestore(&priv->irqlock, flags);
		spin_unlock_bh(&priv->timerlock);
		netif_stop_queue(dev);
837
		return NETDEV_TX_BUSY;
L
Linus Torvalds 已提交
838
	}
C
Carlo Perassi 已提交
839

J
Jeff Garzik 已提交
840
	frame_ctl = IEEE80211_FTYPE_DATA;
L
Linus Torvalds 已提交
841
	header.duration_id = 0;
J
Johannes Berg 已提交
842
	header.seq_ctrl = 0;
L
Linus Torvalds 已提交
843
	if (priv->wep_is_on)
844
		frame_ctl |= IEEE80211_FCTL_PROTECTED;
L
Linus Torvalds 已提交
845
	if (priv->operating_mode == IW_MODE_ADHOC) {
846 847 848
		skb_copy_from_linear_data(skb, &header.addr1, ETH_ALEN);
		memcpy(&header.addr2, dev->dev_addr, ETH_ALEN);
		memcpy(&header.addr3, priv->BSSID, ETH_ALEN);
L
Linus Torvalds 已提交
849
	} else {
J
Jeff Garzik 已提交
850
		frame_ctl |= IEEE80211_FCTL_TODS;
851 852 853
		memcpy(&header.addr1, priv->CurrentBSSID, ETH_ALEN);
		memcpy(&header.addr2, dev->dev_addr, ETH_ALEN);
		skb_copy_from_linear_data(skb, &header.addr3, ETH_ALEN);
L
Linus Torvalds 已提交
854
	}
C
Carlo Perassi 已提交
855

L
Linus Torvalds 已提交
856
	if (priv->use_wpa)
857
		memcpy(&header.addr4, SNAP_RFC1024, ETH_ALEN);
L
Linus Torvalds 已提交
858

J
Johannes Berg 已提交
859
	header.frame_control = cpu_to_le16(frame_ctl);
L
Linus Torvalds 已提交
860 861 862 863 864
	/* Copy the wireless header into the card */
	atmel_copy_to_card(dev, buff, (unsigned char *)&header, DATA_FRAME_WS_HEADER_SIZE);
	/* Copy the packet sans its 802.3 header addresses which have been replaced */
	atmel_copy_to_card(dev, buff + DATA_FRAME_WS_HEADER_SIZE, skb->data + 12, len - 12);
	priv->tx_buff_tail += len - 12 + DATA_FRAME_WS_HEADER_SIZE;
C
Carlo Perassi 已提交
865

L
Linus Torvalds 已提交
866 867
	/* low bit of first byte of destination tells us if broadcast */
	tx_update_descriptor(priv, *(skb->data) & 0x01, len + 18, buff, TX_PACKET_TYPE_DATA);
868
	dev->stats.tx_bytes += len;
C
Carlo Perassi 已提交
869

L
Linus Torvalds 已提交
870 871 872
	spin_unlock_irqrestore(&priv->irqlock, flags);
	spin_unlock_bh(&priv->timerlock);
	dev_kfree_skb(skb);
C
Carlo Perassi 已提交
873

874
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
875 876
}

C
Carlo Perassi 已提交
877
static void atmel_transmit_management_frame(struct atmel_private *priv,
J
Johannes Berg 已提交
878
					    struct ieee80211_hdr *header,
L
Linus Torvalds 已提交
879 880 881
					    u8 *body, int body_len)
{
	u16 buff;
C
Carlo Perassi 已提交
882 883 884
	int len = MGMT_FRAME_BODY_OFFSET + body_len;

	if (!(buff = find_tx_buff(priv, len)))
L
Linus Torvalds 已提交
885 886 887 888 889 890 891
		return;

	atmel_copy_to_card(priv->dev, buff, (u8 *)header, MGMT_FRAME_BODY_OFFSET);
	atmel_copy_to_card(priv->dev, buff + MGMT_FRAME_BODY_OFFSET, body, body_len);
	priv->tx_buff_tail += len;
	tx_update_descriptor(priv, header->addr1[0] & 0x01, len, buff, TX_PACKET_TYPE_MGMT);
}
C
Carlo Perassi 已提交
892 893

static void fast_rx_path(struct atmel_private *priv,
J
Johannes Berg 已提交
894
			 struct ieee80211_hdr *header,
L
Linus Torvalds 已提交
895 896 897
			 u16 msdu_size, u16 rx_packet_loc, u32 crc)
{
	/* fast path: unfragmented packet copy directly into skbuf */
C
Carlo Perassi 已提交
898 899
	u8 mac4[6];
	struct sk_buff	*skb;
L
Linus Torvalds 已提交
900
	unsigned char *skbp;
C
Carlo Perassi 已提交
901

L
Linus Torvalds 已提交
902 903 904
	/* get the final, mac 4 header field, this tells us encapsulation */
	atmel_copy_to_host(priv->dev, mac4, rx_packet_loc + 24, 6);
	msdu_size -= 6;
C
Carlo Perassi 已提交
905

L
Linus Torvalds 已提交
906 907 908 909
	if (priv->do_rx_crc) {
		crc = crc32_le(crc, mac4, 6);
		msdu_size -= 4;
	}
C
Carlo Perassi 已提交
910

L
Linus Torvalds 已提交
911
	if (!(skb = dev_alloc_skb(msdu_size + 14))) {
912
		priv->dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
913 914 915 916 917 918
		return;
	}

	skb_reserve(skb, 2);
	skbp = skb_put(skb, msdu_size + 12);
	atmel_copy_to_host(priv->dev, skbp + 12, rx_packet_loc + 30, msdu_size);
C
Carlo Perassi 已提交
919

L
Linus Torvalds 已提交
920 921 922 923 924
	if (priv->do_rx_crc) {
		u32 netcrc;
		crc = crc32_le(crc, skbp + 12, msdu_size);
		atmel_copy_to_host(priv->dev, (void *)&netcrc, rx_packet_loc + 30 + msdu_size, 4);
		if ((crc ^ 0xffffffff) != netcrc) {
925
			priv->dev->stats.rx_crc_errors++;
L
Linus Torvalds 已提交
926 927 928 929
			dev_kfree_skb(skb);
			return;
		}
	}
C
Carlo Perassi 已提交
930

931
	memcpy(skbp, header->addr1, ETH_ALEN); /* destination address */
J
Johannes Berg 已提交
932
	if (le16_to_cpu(header->frame_control) & IEEE80211_FCTL_FROMDS)
933
		memcpy(&skbp[ETH_ALEN], header->addr3, ETH_ALEN);
L
Linus Torvalds 已提交
934
	else
935
		memcpy(&skbp[ETH_ALEN], header->addr2, ETH_ALEN); /* source address */
C
Carlo Perassi 已提交
936

L
Linus Torvalds 已提交
937
	skb->protocol = eth_type_trans(skb, priv->dev);
C
Carlo Perassi 已提交
938
	skb->ip_summed = CHECKSUM_NONE;
L
Linus Torvalds 已提交
939
	netif_rx(skb);
940 941
	priv->dev->stats.rx_bytes += 12 + msdu_size;
	priv->dev->stats.rx_packets++;
L
Linus Torvalds 已提交
942 943 944
}

/* Test to see if the packet in card memory at packet_loc has a valid CRC
C
Carlo Perassi 已提交
945 946
   It doesn't matter that this is slow: it is only used to proble the first few
   packets. */
L
Linus Torvalds 已提交
947 948 949 950 951 952 953 954 955
static int probe_crc(struct atmel_private *priv, u16 packet_loc, u16 msdu_size)
{
	int i = msdu_size - 4;
	u32 netcrc, crc = 0xffffffff;

	if (msdu_size < 4)
		return 0;

	atmel_copy_to_host(priv->dev, (void *)&netcrc, packet_loc + i, 4);
C
Carlo Perassi 已提交
956

L
Linus Torvalds 已提交
957 958 959 960 961 962 963 964 965
	atmel_writeAR(priv->dev, packet_loc);
	while (i--) {
		u8 octet = atmel_read8(priv->dev, DR);
		crc = crc32_le(crc, &octet, 1);
	}

	return (crc ^ 0xffffffff) == netcrc;
}

C
Carlo Perassi 已提交
966
static void frag_rx_path(struct atmel_private *priv,
J
Johannes Berg 已提交
967
			 struct ieee80211_hdr *header,
C
Carlo Perassi 已提交
968 969
			 u16 msdu_size, u16 rx_packet_loc, u32 crc, u16 seq_no,
			 u8 frag_no, int more_frags)
L
Linus Torvalds 已提交
970
{
971 972
	u8 mac4[ETH_ALEN];
	u8 source[ETH_ALEN];
L
Linus Torvalds 已提交
973 974
	struct sk_buff *skb;

J
Johannes Berg 已提交
975
	if (le16_to_cpu(header->frame_control) & IEEE80211_FCTL_FROMDS)
976
		memcpy(source, header->addr3, ETH_ALEN);
L
Linus Torvalds 已提交
977
	else
978
		memcpy(source, header->addr2, ETH_ALEN);
C
Carlo Perassi 已提交
979

L
Linus Torvalds 已提交
980
	rx_packet_loc += 24; /* skip header */
C
Carlo Perassi 已提交
981

L
Linus Torvalds 已提交
982 983 984 985
	if (priv->do_rx_crc)
		msdu_size -= 4;

	if (frag_no == 0) { /* first fragment */
986 987 988
		atmel_copy_to_host(priv->dev, mac4, rx_packet_loc, ETH_ALEN);
		msdu_size -= ETH_ALEN;
		rx_packet_loc += ETH_ALEN;
L
Linus Torvalds 已提交
989

C
Carlo Perassi 已提交
990
		if (priv->do_rx_crc)
L
Linus Torvalds 已提交
991
			crc = crc32_le(crc, mac4, 6);
C
Carlo Perassi 已提交
992

L
Linus Torvalds 已提交
993 994 995
		priv->frag_seq = seq_no;
		priv->frag_no = 1;
		priv->frag_len = msdu_size;
996 997 998
		memcpy(priv->frag_source, source, ETH_ALEN);
		memcpy(&priv->rx_buf[ETH_ALEN], source, ETH_ALEN);
		memcpy(priv->rx_buf, header->addr1, ETH_ALEN);
C
Carlo Perassi 已提交
999

L
Linus Torvalds 已提交
1000 1001 1002 1003 1004 1005 1006
		atmel_copy_to_host(priv->dev, &priv->rx_buf[12], rx_packet_loc, msdu_size);

		if (priv->do_rx_crc) {
			u32 netcrc;
			crc = crc32_le(crc, &priv->rx_buf[12], msdu_size);
			atmel_copy_to_host(priv->dev, (void *)&netcrc, rx_packet_loc + msdu_size, 4);
			if ((crc ^ 0xffffffff) != netcrc) {
1007
				priv->dev->stats.rx_crc_errors++;
1008
				memset(priv->frag_source, 0xff, ETH_ALEN);
L
Linus Torvalds 已提交
1009 1010
			}
		}
C
Carlo Perassi 已提交
1011

L
Linus Torvalds 已提交
1012 1013
	} else if (priv->frag_no == frag_no &&
		   priv->frag_seq == seq_no &&
1014
		   memcmp(priv->frag_source, source, ETH_ALEN) == 0) {
C
Carlo Perassi 已提交
1015 1016

		atmel_copy_to_host(priv->dev, &priv->rx_buf[12 + priv->frag_len],
L
Linus Torvalds 已提交
1017 1018 1019
				   rx_packet_loc, msdu_size);
		if (priv->do_rx_crc) {
			u32 netcrc;
C
Carlo Perassi 已提交
1020 1021
			crc = crc32_le(crc,
				       &priv->rx_buf[12 + priv->frag_len],
L
Linus Torvalds 已提交
1022 1023 1024
				       msdu_size);
			atmel_copy_to_host(priv->dev, (void *)&netcrc, rx_packet_loc + msdu_size, 4);
			if ((crc ^ 0xffffffff) != netcrc) {
1025
				priv->dev->stats.rx_crc_errors++;
1026
				memset(priv->frag_source, 0xff, ETH_ALEN);
L
Linus Torvalds 已提交
1027 1028 1029
				more_frags = 1; /* don't send broken assembly */
			}
		}
C
Carlo Perassi 已提交
1030

L
Linus Torvalds 已提交
1031 1032 1033 1034
		priv->frag_len += msdu_size;
		priv->frag_no++;

		if (!more_frags) { /* last one */
1035
			memset(priv->frag_source, 0xff, ETH_ALEN);
L
Linus Torvalds 已提交
1036
			if (!(skb = dev_alloc_skb(priv->frag_len + 14))) {
1037
				priv->dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
1038 1039
			} else {
				skb_reserve(skb, 2);
C
Carlo Perassi 已提交
1040
				memcpy(skb_put(skb, priv->frag_len + 12),
L
Linus Torvalds 已提交
1041 1042 1043
				       priv->rx_buf,
				       priv->frag_len + 12);
				skb->protocol = eth_type_trans(skb, priv->dev);
C
Carlo Perassi 已提交
1044
				skb->ip_summed = CHECKSUM_NONE;
L
Linus Torvalds 已提交
1045
				netif_rx(skb);
1046 1047
				priv->dev->stats.rx_bytes += priv->frag_len + 12;
				priv->dev->stats.rx_packets++;
L
Linus Torvalds 已提交
1048 1049 1050 1051 1052
			}
		}
	} else
		priv->wstats.discard.fragment++;
}
C
Carlo Perassi 已提交
1053

L
Linus Torvalds 已提交
1054 1055 1056
static void rx_done_irq(struct atmel_private *priv)
{
	int i;
J
Johannes Berg 已提交
1057
	struct ieee80211_hdr header;
C
Carlo Perassi 已提交
1058 1059

	for (i = 0;
L
Linus Torvalds 已提交
1060 1061 1062
	     atmel_rmem8(priv, atmel_rx(priv, RX_DESC_FLAGS_OFFSET, priv->rx_desc_head)) == RX_DESC_FLAG_VALID &&
		     i < priv->host_info.rx_desc_count;
	     i++) {
C
Carlo Perassi 已提交
1063

L
Linus Torvalds 已提交
1064 1065 1066
		u16 msdu_size, rx_packet_loc, frame_ctl, seq_control;
		u8 status = atmel_rmem8(priv, atmel_rx(priv, RX_DESC_STATUS_OFFSET, priv->rx_desc_head));
		u32 crc = 0xffffffff;
C
Carlo Perassi 已提交
1067

L
Linus Torvalds 已提交
1068 1069 1070 1071
		if (status != RX_STATUS_SUCCESS) {
			if (status == 0xc1) /* determined by experiment */
				priv->wstats.discard.nwid++;
			else
1072
				priv->dev->stats.rx_errors++;
L
Linus Torvalds 已提交
1073 1074 1075 1076 1077
			goto next;
		}

		msdu_size = atmel_rmem16(priv, atmel_rx(priv, RX_DESC_MSDU_SIZE_OFFSET, priv->rx_desc_head));
		rx_packet_loc = atmel_rmem16(priv, atmel_rx(priv, RX_DESC_MSDU_POS_OFFSET, priv->rx_desc_head));
C
Carlo Perassi 已提交
1078

L
Linus Torvalds 已提交
1079
		if (msdu_size < 30) {
1080
			priv->dev->stats.rx_errors++;
L
Linus Torvalds 已提交
1081 1082
			goto next;
		}
C
Carlo Perassi 已提交
1083

J
Johannes Berg 已提交
1084
		/* Get header as far as end of seq_ctrl */
L
Linus Torvalds 已提交
1085
		atmel_copy_to_host(priv->dev, (char *)&header, rx_packet_loc, 24);
J
Johannes Berg 已提交
1086 1087
		frame_ctl = le16_to_cpu(header.frame_control);
		seq_control = le16_to_cpu(header.seq_ctrl);
L
Linus Torvalds 已提交
1088

C
Carlo Perassi 已提交
1089 1090 1091
		/* probe for CRC use here if needed  once five packets have
		   arrived with the same crc status, we assume we know what's
		   happening and stop probing */
L
Linus Torvalds 已提交
1092
		if (priv->probe_crc) {
1093
			if (!priv->wep_is_on || !(frame_ctl & IEEE80211_FCTL_PROTECTED)) {
L
Linus Torvalds 已提交
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
				priv->do_rx_crc = probe_crc(priv, rx_packet_loc, msdu_size);
			} else {
				priv->do_rx_crc = probe_crc(priv, rx_packet_loc + 24, msdu_size - 24);
			}
			if (priv->do_rx_crc) {
				if (priv->crc_ok_cnt++ > 5)
					priv->probe_crc = 0;
			} else {
				if (priv->crc_ko_cnt++ > 5)
					priv->probe_crc = 0;
			}
		}
C
Carlo Perassi 已提交
1106

L
Linus Torvalds 已提交
1107
		/* don't CRC header when WEP in use */
1108
		if (priv->do_rx_crc && (!priv->wep_is_on || !(frame_ctl & IEEE80211_FCTL_PROTECTED))) {
L
Linus Torvalds 已提交
1109 1110 1111 1112
			crc = crc32_le(0xffffffff, (unsigned char *)&header, 24);
		}
		msdu_size -= 24; /* header */

C
Carlo Perassi 已提交
1113
		if ((frame_ctl & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) {
J
Jeff Garzik 已提交
1114 1115 1116
			int more_fragments = frame_ctl & IEEE80211_FCTL_MOREFRAGS;
			u8 packet_fragment_no = seq_control & IEEE80211_SCTL_FRAG;
			u16 packet_sequence_no = (seq_control & IEEE80211_SCTL_SEQ) >> 4;
C
Carlo Perassi 已提交
1117 1118

			if (!more_fragments && packet_fragment_no == 0) {
L
Linus Torvalds 已提交
1119 1120 1121 1122 1123 1124
				fast_rx_path(priv, &header, msdu_size, rx_packet_loc, crc);
			} else {
				frag_rx_path(priv, &header, msdu_size, rx_packet_loc, crc,
					     packet_sequence_no, packet_fragment_no, more_fragments);
			}
		}
C
Carlo Perassi 已提交
1125

J
Jeff Garzik 已提交
1126
		if ((frame_ctl & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
L
Linus Torvalds 已提交
1127 1128
			/* copy rest of packet into buffer */
			atmel_copy_to_host(priv->dev, (unsigned char *)&priv->rx_buf, rx_packet_loc + 24, msdu_size);
C
Carlo Perassi 已提交
1129

L
Linus Torvalds 已提交
1130
			/* we use the same buffer for frag reassembly and control packets */
1131
			memset(priv->frag_source, 0xff, ETH_ALEN);
C
Carlo Perassi 已提交
1132

L
Linus Torvalds 已提交
1133 1134 1135 1136 1137
			if (priv->do_rx_crc) {
				/* last 4 octets is crc */
				msdu_size -= 4;
				crc = crc32_le(crc, (unsigned char *)&priv->rx_buf, msdu_size);
				if ((crc ^ 0xffffffff) != (*((u32 *)&priv->rx_buf[msdu_size]))) {
1138
					priv->dev->stats.rx_crc_errors++;
L
Linus Torvalds 已提交
1139 1140 1141 1142 1143 1144
					goto next;
				}
			}

			atmel_management_frame(priv, &header, msdu_size,
					       atmel_rmem8(priv, atmel_rx(priv, RX_DESC_RSSI_OFFSET, priv->rx_desc_head)));
C
Carlo Perassi 已提交
1145
		}
L
Linus Torvalds 已提交
1146

C
Carlo Perassi 已提交
1147
next:
L
Linus Torvalds 已提交
1148
		/* release descriptor */
C
Carlo Perassi 已提交
1149 1150
		atmel_wmem8(priv, atmel_rx(priv, RX_DESC_FLAGS_OFFSET, priv->rx_desc_head), RX_DESC_FLAG_CONSUMED);

L
Linus Torvalds 已提交
1151
		if (priv->rx_desc_head < (priv->host_info.rx_desc_count - 1))
C
Carlo Perassi 已提交
1152
			priv->rx_desc_head++;
L
Linus Torvalds 已提交
1153 1154 1155
		else
			priv->rx_desc_head = 0;
	}
C
Carlo Perassi 已提交
1156
}
L
Linus Torvalds 已提交
1157

1158
static irqreturn_t service_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
1159 1160 1161 1162 1163
{
	struct net_device *dev = (struct net_device *) dev_id;
	struct atmel_private *priv = netdev_priv(dev);
	u8 isr;
	int i = -1;
J
Joe Perches 已提交
1164
	static const u8 irq_order[] = {
L
Linus Torvalds 已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
		ISR_OUT_OF_RANGE,
		ISR_RxCOMPLETE,
		ISR_TxCOMPLETE,
		ISR_RxFRAMELOST,
		ISR_FATAL_ERROR,
		ISR_COMMAND_COMPLETE,
		ISR_IBSS_MERGE,
		ISR_GENERIC_IRQ
	};

C
Carlo Perassi 已提交
1175
	if (priv->card && priv->present_callback &&
L
Linus Torvalds 已提交
1176 1177 1178 1179 1180 1181
	    !(*priv->present_callback)(priv->card))
		return IRQ_HANDLED;

	/* In this state upper-level code assumes it can mess with
	   the card unhampered by interrupts which may change register state.
	   Note that even though the card shouldn't generate interrupts
C
Carlo Perassi 已提交
1182
	   the inturrupt line may be shared. This allows card setup
L
Linus Torvalds 已提交
1183 1184 1185
	   to go on without disabling interrupts for a long time. */
	if (priv->station_state == STATION_STATE_DOWN)
		return IRQ_NONE;
C
Carlo Perassi 已提交
1186

L
Linus Torvalds 已提交
1187 1188 1189 1190 1191 1192 1193 1194
	atmel_clear_gcr(dev, GCR_ENINT); /* disable interrupts */

	while (1) {
		if (!atmel_lock_mac(priv)) {
			/* failed to contact card */
			printk(KERN_ALERT "%s: failed to contact MAC.\n", dev->name);
			return IRQ_HANDLED;
		}
C
Carlo Perassi 已提交
1195

L
Linus Torvalds 已提交
1196 1197
		isr = atmel_rmem8(priv, atmel_hi(priv, IFACE_INT_STATUS_OFFSET));
		atmel_wmem8(priv, atmel_hi(priv, IFACE_LOCKOUT_MAC_OFFSET), 0);
C
Carlo Perassi 已提交
1198

L
Linus Torvalds 已提交
1199 1200 1201 1202
		if (!isr) {
			atmel_set_gcr(dev, GCR_ENINT); /* enable interrupts */
			return i == -1 ? IRQ_NONE : IRQ_HANDLED;
		}
C
Carlo Perassi 已提交
1203

L
Linus Torvalds 已提交
1204
		atmel_set_gcr(dev, GCR_ACKINT); /* acknowledge interrupt */
C
Carlo Perassi 已提交
1205

D
Dmitry Torokhov 已提交
1206
		for (i = 0; i < ARRAY_SIZE(irq_order); i++)
L
Linus Torvalds 已提交
1207 1208
			if (isr & irq_order[i])
				break;
C
Carlo Perassi 已提交
1209

L
Linus Torvalds 已提交
1210 1211 1212 1213 1214
		if (!atmel_lock_mac(priv)) {
			/* failed to contact card */
			printk(KERN_ALERT "%s: failed to contact MAC.\n", dev->name);
			return IRQ_HANDLED;
		}
C
Carlo Perassi 已提交
1215

L
Linus Torvalds 已提交
1216 1217 1218 1219
		isr = atmel_rmem8(priv, atmel_hi(priv, IFACE_INT_STATUS_OFFSET));
		isr ^= irq_order[i];
		atmel_wmem8(priv, atmel_hi(priv, IFACE_INT_STATUS_OFFSET), isr);
		atmel_wmem8(priv, atmel_hi(priv, IFACE_LOCKOUT_MAC_OFFSET), 0);
C
Carlo Perassi 已提交
1220

L
Linus Torvalds 已提交
1221
		switch (irq_order[i]) {
C
Carlo Perassi 已提交
1222 1223 1224

		case ISR_OUT_OF_RANGE:
			if (priv->operating_mode == IW_MODE_INFRA &&
L
Linus Torvalds 已提交
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
			    priv->station_state == STATION_STATE_READY) {
				priv->station_is_associated = 0;
				atmel_scan(priv, 1);
			}
			break;

		case ISR_RxFRAMELOST:
			priv->wstats.discard.misc++;
			/* fall through */
		case ISR_RxCOMPLETE:
C
Carlo Perassi 已提交
1235
			rx_done_irq(priv);
L
Linus Torvalds 已提交
1236
			break;
C
Carlo Perassi 已提交
1237

L
Linus Torvalds 已提交
1238
		case ISR_TxCOMPLETE:
C
Carlo Perassi 已提交
1239
			tx_done_irq(priv);
L
Linus Torvalds 已提交
1240
			break;
C
Carlo Perassi 已提交
1241

L
Linus Torvalds 已提交
1242 1243 1244 1245
		case ISR_FATAL_ERROR:
			printk(KERN_ALERT "%s: *** FATAL error interrupt ***\n", dev->name);
			atmel_enter_state(priv, STATION_STATE_MGMT_ERROR);
			break;
C
Carlo Perassi 已提交
1246 1247

		case ISR_COMMAND_COMPLETE:
L
Linus Torvalds 已提交
1248 1249 1250 1251
			atmel_command_irq(priv);
			break;

		case ISR_IBSS_MERGE:
C
Carlo Perassi 已提交
1252
			atmel_get_mib(priv, Mac_Mgmt_Mib_Type, MAC_MGMT_MIB_CUR_BSSID_POS,
L
Linus Torvalds 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261
				      priv->CurrentBSSID, 6);
			/* The WPA stuff cares about the current AP address */
			if (priv->use_wpa)
				build_wpa_mib(priv);
			break;
		case ISR_GENERIC_IRQ:
			printk(KERN_INFO "%s: Generic_irq received.\n", dev->name);
			break;
		}
C
Carlo Perassi 已提交
1262
	}
L
Linus Torvalds 已提交
1263 1264
}

C
Carlo Perassi 已提交
1265
static struct iw_statistics *atmel_get_wireless_stats(struct net_device *dev)
L
Linus Torvalds 已提交
1266 1267 1268
{
	struct atmel_private *priv = netdev_priv(dev);

C
Carlo Perassi 已提交
1269
	/* update the link quality here in case we are seeing no beacons
L
Linus Torvalds 已提交
1270 1271
	   at all to drive the process */
	atmel_smooth_qual(priv);
C
Carlo Perassi 已提交
1272

L
Linus Torvalds 已提交
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
	priv->wstats.status = priv->station_state;

	if (priv->operating_mode == IW_MODE_INFRA) {
		if (priv->station_state != STATION_STATE_READY) {
			priv->wstats.qual.qual = 0;
			priv->wstats.qual.level = 0;
			priv->wstats.qual.updated = (IW_QUAL_QUAL_INVALID
					| IW_QUAL_LEVEL_INVALID);
		}
		priv->wstats.qual.noise = 0;
		priv->wstats.qual.updated |= IW_QUAL_NOISE_INVALID;
	} else {
		/* Quality levels cannot be determined in ad-hoc mode,
		   because we can 'hear' more that one remote station. */
		priv->wstats.qual.qual = 0;
		priv->wstats.qual.level	= 0;
		priv->wstats.qual.noise	= 0;
		priv->wstats.qual.updated = IW_QUAL_QUAL_INVALID
					| IW_QUAL_LEVEL_INVALID
					| IW_QUAL_NOISE_INVALID;
		priv->wstats.miss.beacon = 0;
	}
C
Carlo Perassi 已提交
1295 1296

	return &priv->wstats;
L
Linus Torvalds 已提交
1297 1298 1299 1300
}

static int atmel_change_mtu(struct net_device *dev, int new_mtu)
{
J
John Daiker 已提交
1301 1302 1303 1304
	if ((new_mtu < 68) || (new_mtu > 2312))
		return -EINVAL;
	dev->mtu = new_mtu;
	return 0;
L
Linus Torvalds 已提交
1305 1306 1307 1308 1309
}

static int atmel_set_mac_address(struct net_device *dev, void *p)
{
	struct sockaddr *addr = p;
C
Carlo Perassi 已提交
1310

J
John Daiker 已提交
1311
	memcpy (dev->dev_addr, addr->sa_data, dev->addr_len);
L
Linus Torvalds 已提交
1312 1313 1314 1315 1316
	return atmel_open(dev);
}

EXPORT_SYMBOL(atmel_open);

C
Carlo Perassi 已提交
1317
int atmel_open(struct net_device *dev)
L
Linus Torvalds 已提交
1318 1319
{
	struct atmel_private *priv = netdev_priv(dev);
1320
	int i, channel, err;
L
Linus Torvalds 已提交
1321 1322 1323

	/* any scheduled timer is no longer needed and might screw things up.. */
	del_timer_sync(&priv->management_timer);
C
Carlo Perassi 已提交
1324

L
Linus Torvalds 已提交
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
	/* Interrupts will not touch the card once in this state... */
	priv->station_state = STATION_STATE_DOWN;

	if (priv->new_SSID_size) {
		memcpy(priv->SSID, priv->new_SSID, priv->new_SSID_size);
		priv->SSID_size = priv->new_SSID_size;
		priv->new_SSID_size = 0;
	}
	priv->BSS_list_entries = 0;

	priv->AuthenticationRequestRetryCnt = 0;
	priv->AssociationRequestRetryCnt = 0;
	priv->ReAssociationRequestRetryCnt = 0;
	priv->CurrentAuthentTransactionSeqNum = 0x0001;
	priv->ExpectedAuthentTransactionSeqNum = 0x0002;

	priv->site_survey_state = SITE_SURVEY_IDLE;
	priv->station_is_associated = 0;

1344 1345 1346
	err = reset_atmel_card(dev);
	if (err)
		return err;
L
Linus Torvalds 已提交
1347 1348 1349 1350 1351 1352

	if (priv->config_reg_domain) {
		priv->reg_domain = priv->config_reg_domain;
		atmel_set_mib8(priv, Phy_Mib_Type, PHY_MIB_REG_DOMAIN_POS, priv->reg_domain);
	} else {
		priv->reg_domain = atmel_get_mib8(priv, Phy_Mib_Type, PHY_MIB_REG_DOMAIN_POS);
D
Dmitry Torokhov 已提交
1353
		for (i = 0; i < ARRAY_SIZE(channel_table); i++)
L
Linus Torvalds 已提交
1354 1355
			if (priv->reg_domain == channel_table[i].reg_domain)
				break;
D
Dmitry Torokhov 已提交
1356
		if (i == ARRAY_SIZE(channel_table)) {
L
Linus Torvalds 已提交
1357 1358
			priv->reg_domain = REG_DOMAIN_MKK1;
			printk(KERN_ALERT "%s: failed to get regulatory domain: assuming MKK1.\n", dev->name);
C
Carlo Perassi 已提交
1359
		}
L
Linus Torvalds 已提交
1360
	}
C
Carlo Perassi 已提交
1361

L
Linus Torvalds 已提交
1362 1363 1364
	if ((channel = atmel_validate_channel(priv, priv->channel)))
		priv->channel = channel;

C
Carlo Perassi 已提交
1365 1366
	/* this moves station_state on.... */
	atmel_scan(priv, 1);
L
Linus Torvalds 已提交
1367 1368 1369 1370 1371

	atmel_set_gcr(priv->dev, GCR_ENINT); /* enable interrupts */
	return 0;
}

C
Carlo Perassi 已提交
1372
static int atmel_close(struct net_device *dev)
L
Linus Torvalds 已提交
1373 1374
{
	struct atmel_private *priv = netdev_priv(dev);
C
Carlo Perassi 已提交
1375

1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
	/* Send event to userspace that we are disassociating */
	if (priv->station_state == STATION_STATE_READY) {
		union iwreq_data wrqu;

		wrqu.data.length = 0;
		wrqu.data.flags = 0;
		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
		memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
		wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
	}

L
Linus Torvalds 已提交
1387
	atmel_enter_state(priv, STATION_STATE_DOWN);
C
Carlo Perassi 已提交
1388 1389

	if (priv->bus_type == BUS_TYPE_PCCARD)
L
Linus Torvalds 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
		atmel_write16(dev, GCR, 0x0060);
	atmel_write16(dev, GCR, 0x0040);
	return 0;
}

static int atmel_validate_channel(struct atmel_private *priv, int channel)
{
	/* check that channel is OK, if so return zero,
	   else return suitable default channel */
	int i;

D
Dmitry Torokhov 已提交
1401
	for (i = 0; i < ARRAY_SIZE(channel_table); i++)
L
Linus Torvalds 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
		if (priv->reg_domain == channel_table[i].reg_domain) {
			if (channel >= channel_table[i].min &&
			    channel <= channel_table[i].max)
				return 0;
			else
				return channel_table[i].min;
		}
	return 0;
}

1412
static int atmel_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
1413
{
1414
	struct atmel_private *priv = m->private;
L
Linus Torvalds 已提交
1415 1416
	int i;
	char *s, *r, *c;
C
Carlo Perassi 已提交
1417

1418
	seq_printf(m, "Driver version:\t\t%d.%d\n", DRIVER_MAJOR, DRIVER_MINOR);
C
Carlo Perassi 已提交
1419

L
Linus Torvalds 已提交
1420
	if (priv->station_state != STATION_STATE_DOWN) {
1421 1422 1423 1424 1425 1426
		seq_printf(m,
			   "Firmware version:\t%d.%d build %d\n"
			   "Firmware location:\t",
			   priv->host_info.major_version,
			   priv->host_info.minor_version,
			   priv->host_info.build_version);
C
Carlo Perassi 已提交
1427 1428

		if (priv->card_type != CARD_TYPE_EEPROM)
1429
			seq_puts(m, "on card\n");
C
Carlo Perassi 已提交
1430
		else if (priv->firmware)
1431
			seq_printf(m, "%s loaded by host\n", priv->firmware_id);
L
Linus Torvalds 已提交
1432
		else
1433
			seq_printf(m, "%s loaded by hotplug\n", priv->firmware_id);
C
Carlo Perassi 已提交
1434 1435

		switch (priv->card_type) {
J
John Daiker 已提交
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
		case CARD_TYPE_PARALLEL_FLASH:
			c = "Parallel flash";
			break;
		case CARD_TYPE_SPI_FLASH:
			c = "SPI flash\n";
			break;
		case CARD_TYPE_EEPROM:
			c = "EEPROM";
			break;
		default:
			c = "<unknown>";
L
Linus Torvalds 已提交
1447 1448 1449
		}

		r = "<unknown>";
D
Dmitry Torokhov 已提交
1450
		for (i = 0; i < ARRAY_SIZE(channel_table); i++)
L
Linus Torvalds 已提交
1451 1452
			if (priv->reg_domain == channel_table[i].reg_domain)
				r = channel_table[i].name;
C
Carlo Perassi 已提交
1453

1454 1455 1456 1457 1458 1459
		seq_printf(m, "MAC memory type:\t%s\n", c);
		seq_printf(m, "Regulatory domain:\t%s\n", r);
		seq_printf(m, "Host CRC checking:\t%s\n",
			 priv->do_rx_crc ? "On" : "Off");
		seq_printf(m, "WPA-capable firmware:\t%s\n",
			 priv->use_wpa ? "Yes" : "No");
L
Linus Torvalds 已提交
1460
	}
C
Carlo Perassi 已提交
1461

J
John Daiker 已提交
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
	switch (priv->station_state) {
	case STATION_STATE_SCANNING:
		s = "Scanning";
		break;
	case STATION_STATE_JOINNING:
		s = "Joining";
		break;
	case STATION_STATE_AUTHENTICATING:
		s = "Authenticating";
		break;
	case STATION_STATE_ASSOCIATING:
		s = "Associating";
		break;
	case STATION_STATE_READY:
		s = "Ready";
		break;
	case STATION_STATE_REASSOCIATING:
		s = "Reassociating";
		break;
	case STATION_STATE_MGMT_ERROR:
		s = "Management error";
		break;
	case STATION_STATE_DOWN:
		s = "Down";
		break;
	default:
		s = "<unknown>";
L
Linus Torvalds 已提交
1489
	}
C
Carlo Perassi 已提交
1490

1491 1492
	seq_printf(m, "Current state:\t\t%s\n", s);
	return 0;
L
Linus Torvalds 已提交
1493 1494
}

1495
static int atmel_proc_open(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
1496
{
1497
	return single_open(file, atmel_proc_show, PDE_DATA(inode));
L
Linus Torvalds 已提交
1498 1499
}

1500 1501 1502 1503
static const struct file_operations atmel_proc_fops = {
	.open		= atmel_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
A
Al Viro 已提交
1504
	.release	= single_release,
1505 1506
};

1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
static const struct net_device_ops atmel_netdev_ops = {
	.ndo_open 		= atmel_open,
	.ndo_stop		= atmel_close,
	.ndo_change_mtu 	= atmel_change_mtu,
	.ndo_set_mac_address 	= atmel_set_mac_address,
	.ndo_start_xmit 	= start_tx,
	.ndo_do_ioctl 		= atmel_ioctl,
	.ndo_validate_addr	= eth_validate_addr,
};

C
Carlo Perassi 已提交
1517 1518 1519 1520
struct net_device *init_atmel_card(unsigned short irq, unsigned long port,
				   const AtmelFWType fw_type,
				   struct device *sys_dev,
				   int (*card_present)(void *), void *card)
L
Linus Torvalds 已提交
1521 1522 1523 1524 1525 1526
{
	struct net_device *dev;
	struct atmel_private *priv;
	int rc;

	/* Create the network device object. */
J
John Daiker 已提交
1527
	dev = alloc_etherdev(sizeof(*priv));
1528
	if (!dev)
L
Linus Torvalds 已提交
1529
		return NULL;
1530

L
Linus Torvalds 已提交
1531
	if (dev_alloc_name(dev, dev->name) < 0) {
C
Carlo Perassi 已提交
1532
		printk(KERN_ERR "atmel: Couldn't get name!\n");
L
Linus Torvalds 已提交
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
		goto err_out_free;
	}

	priv = netdev_priv(dev);
	priv->dev = dev;
	priv->sys_dev = sys_dev;
	priv->present_callback = card_present;
	priv->card = card;
	priv->firmware = NULL;
	priv->firmware_id[0] = '\0';
	priv->firmware_type = fw_type;
	if (firmware) /* module parameter */
		strcpy(priv->firmware_id, firmware);
	priv->bus_type = card_present ? BUS_TYPE_PCCARD : BUS_TYPE_PCI;
	priv->station_state = STATION_STATE_DOWN;
	priv->do_rx_crc = 0;
	/* For PCMCIA cards, some chips need CRC, some don't
	   so we have to probe. */
	if (priv->bus_type == BUS_TYPE_PCCARD) {
		priv->probe_crc = 1;
		priv->crc_ok_cnt = priv->crc_ko_cnt = 0;
	} else
		priv->probe_crc = 0;
	priv->last_qual = jiffies;
	priv->last_beacon_timestamp = 0;
	memset(priv->frag_source, 0xff, sizeof(priv->frag_source));
1559
	memset(priv->BSSID, 0, ETH_ALEN);
L
Linus Torvalds 已提交
1560 1561
	priv->CurrentBSSID[0] = 0xFF; /* Initialize to something invalid.... */
	priv->station_was_associated = 0;
C
Carlo Perassi 已提交
1562

L
Linus Torvalds 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
	priv->last_survey = jiffies;
	priv->preamble = LONG_PREAMBLE;
	priv->operating_mode = IW_MODE_INFRA;
	priv->connect_to_any_BSS = 0;
	priv->config_reg_domain = 0;
	priv->reg_domain = 0;
	priv->tx_rate = 3;
	priv->auto_tx_rate = 1;
	priv->channel = 4;
	priv->power_mode = 0;
	priv->SSID[0] = '\0';
	priv->SSID_size = 0;
	priv->new_SSID_size = 0;
	priv->frag_threshold = 2346;
	priv->rts_threshold = 2347;
	priv->short_retry = 7;
	priv->long_retry = 4;

	priv->wep_is_on = 0;
	priv->default_key = 0;
	priv->encryption_level = 0;
	priv->exclude_unencrypted = 0;
	priv->group_cipher_suite = priv->pairwise_cipher_suite = CIPHER_SUITE_NONE;
	priv->use_wpa = 0;
	memset(priv->wep_keys, 0, sizeof(priv->wep_keys));
	memset(priv->wep_key_len, 0, sizeof(priv->wep_key_len));

	priv->default_beacon_period = priv->beacon_period = 100;
	priv->listen_interval = 1;

	init_timer(&priv->management_timer);
	spin_lock_init(&priv->irqlock);
	spin_lock_init(&priv->timerlock);
	priv->management_timer.function = atmel_management_timer;
	priv->management_timer.data = (unsigned long) dev;
C
Carlo Perassi 已提交
1598

1599 1600
	dev->netdev_ops = &atmel_netdev_ops;
	dev->wireless_handlers = &atmel_handler_def;
L
Linus Torvalds 已提交
1601 1602
	dev->irq = irq;
	dev->base_addr = port;
C
Carlo Perassi 已提交
1603

L
Linus Torvalds 已提交
1604
	SET_NETDEV_DEV(dev, sys_dev);
C
Carlo Perassi 已提交
1605

1606
	if ((rc = request_irq(dev->irq, service_interrupt, IRQF_SHARED, dev->name, dev))) {
C
Carlo Perassi 已提交
1607
		printk(KERN_ERR "%s: register interrupt %d failed, rc %d\n", dev->name, irq, rc);
L
Linus Torvalds 已提交
1608 1609 1610
		goto err_out_free;
	}

C
Carlo Perassi 已提交
1611
	if (!request_region(dev->base_addr, 32,
1612
			    priv->bus_type == BUS_TYPE_PCCARD ?  "atmel_cs" : "atmel_pci")) {
L
Linus Torvalds 已提交
1613 1614
		goto err_out_irq;
	}
C
Carlo Perassi 已提交
1615

L
Linus Torvalds 已提交
1616 1617
	if (register_netdev(dev))
		goto err_out_res;
C
Carlo Perassi 已提交
1618

J
John Daiker 已提交
1619
	if (!probe_atmel_card(dev)) {
L
Linus Torvalds 已提交
1620 1621 1622
		unregister_netdev(dev);
		goto err_out_res;
	}
C
Carlo Perassi 已提交
1623

L
Linus Torvalds 已提交
1624
	netif_carrier_off(dev);
C
Carlo Perassi 已提交
1625

1626
	if (!proc_create_data("driver/atmel", 0, NULL, &atmel_proc_fops, priv))
1627
		printk(KERN_WARNING "atmel: unable to create /proc entry.\n");
C
Carlo Perassi 已提交
1628

J
Johannes Berg 已提交
1629 1630
	printk(KERN_INFO "%s: Atmel at76c50x. Version %d.%d. MAC %pM\n",
	       dev->name, DRIVER_MAJOR, DRIVER_MINOR, dev->dev_addr);
C
Carlo Perassi 已提交
1631

L
Linus Torvalds 已提交
1632
	return dev;
C
Carlo Perassi 已提交
1633 1634

err_out_res:
J
John Daiker 已提交
1635
	release_region(dev->base_addr, 32);
C
Carlo Perassi 已提交
1636
err_out_irq:
L
Linus Torvalds 已提交
1637
	free_irq(dev->irq, dev);
C
Carlo Perassi 已提交
1638
err_out_free:
L
Linus Torvalds 已提交
1639 1640 1641 1642 1643 1644
	free_netdev(dev);
	return NULL;
}

EXPORT_SYMBOL(init_atmel_card);

1645
void stop_atmel_card(struct net_device *dev)
L
Linus Torvalds 已提交
1646 1647
{
	struct atmel_private *priv = netdev_priv(dev);
C
Carlo Perassi 已提交
1648

L
Linus Torvalds 已提交
1649
	/* put a brick on it... */
C
Carlo Perassi 已提交
1650
	if (priv->bus_type == BUS_TYPE_PCCARD)
L
Linus Torvalds 已提交
1651 1652
		atmel_write16(dev, GCR, 0x0060);
	atmel_write16(dev, GCR, 0x0040);
C
Carlo Perassi 已提交
1653

L
Linus Torvalds 已提交
1654 1655 1656 1657
	del_timer_sync(&priv->management_timer);
	unregister_netdev(dev);
	remove_proc_entry("driver/atmel", NULL);
	free_irq(dev->irq, dev);
1658
	kfree(priv->firmware);
1659
	release_region(dev->base_addr, 32);
L
Linus Torvalds 已提交
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
	free_netdev(dev);
}

EXPORT_SYMBOL(stop_atmel_card);

static int atmel_set_essid(struct net_device *dev,
			   struct iw_request_info *info,
			   struct iw_point *dwrq,
			   char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);

	/* Check if we asked for `any' */
J
John Daiker 已提交
1673
	if (dwrq->flags == 0) {
L
Linus Torvalds 已提交
1674 1675 1676 1677 1678
		priv->connect_to_any_BSS = 1;
	} else {
		int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;

		priv->connect_to_any_BSS = 0;
C
Carlo Perassi 已提交
1679

L
Linus Torvalds 已提交
1680
		/* Check the size of the string */
J
Jean Tourrilhes 已提交
1681
		if (dwrq->length > MAX_SSID_LENGTH)
C
Carlo Perassi 已提交
1682
			 return -E2BIG;
L
Linus Torvalds 已提交
1683 1684
		if (index != 0)
			return -EINVAL;
C
Carlo Perassi 已提交
1685

J
Jean Tourrilhes 已提交
1686 1687
		memcpy(priv->new_SSID, extra, dwrq->length);
		priv->new_SSID_size = dwrq->length;
L
Linus Torvalds 已提交
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
	}

	return -EINPROGRESS;
}

static int atmel_get_essid(struct net_device *dev,
			   struct iw_request_info *info,
			   struct iw_point *dwrq,
			   char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);

	/* Get the current SSID */
	if (priv->new_SSID_size != 0) {
		memcpy(extra, priv->new_SSID, priv->new_SSID_size);
1703
		dwrq->length = priv->new_SSID_size;
L
Linus Torvalds 已提交
1704 1705
	} else {
		memcpy(extra, priv->SSID, priv->SSID_size);
1706
		dwrq->length = priv->SSID_size;
L
Linus Torvalds 已提交
1707
	}
C
Carlo Perassi 已提交
1708

L
Linus Torvalds 已提交
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
	dwrq->flags = !priv->connect_to_any_BSS; /* active */

	return 0;
}

static int atmel_get_wap(struct net_device *dev,
			 struct iw_request_info *info,
			 struct sockaddr *awrq,
			 char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
1720
	memcpy(awrq->sa_data, priv->CurrentBSSID, ETH_ALEN);
L
Linus Torvalds 已提交
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
	awrq->sa_family = ARPHRD_ETHER;

	return 0;
}

static int atmel_set_encode(struct net_device *dev,
			    struct iw_request_info *info,
			    struct iw_point *dwrq,
			    char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);

	/* 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) {
		int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
		int current_index = priv->default_key;
		/* Check the size of the key */
		if (dwrq->length > 13) {
			return -EINVAL;
		}
		/* Check the index (none -> use current) */
		if (index < 0 || index >= 4)
			index = current_index;
		else
			priv->default_key = index;
		/* Set the length */
		if (dwrq->length > 5)
			priv->wep_key_len[index] = 13;
		else
			if (dwrq->length > 0)
				priv->wep_key_len[index] = 5;
			else
				/* Disable the key */
				priv->wep_key_len[index] = 0;
		/* Check if the key is not marked as invalid */
1761
		if (!(dwrq->flags & IW_ENCODE_NOKEY)) {
L
Linus Torvalds 已提交
1762 1763 1764 1765 1766 1767 1768 1769
			/* Cleanup */
			memset(priv->wep_keys[index], 0, 13);
			/* Copy the key in the driver */
			memcpy(priv->wep_keys[index], extra, dwrq->length);
		}
		/* 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 */
C
Carlo Perassi 已提交
1770
		if (index == current_index &&
L
Linus Torvalds 已提交
1771 1772 1773 1774
		    priv->wep_key_len[index] > 0) {
			priv->wep_is_on = 1;
			priv->exclude_unencrypted = 1;
			if (priv->wep_key_len[index] > 5) {
1775
				priv->pairwise_cipher_suite = CIPHER_SUITE_WEP_128;
L
Linus Torvalds 已提交
1776 1777
				priv->encryption_level = 2;
			} else {
1778
				priv->pairwise_cipher_suite = CIPHER_SUITE_WEP_64;
L
Linus Torvalds 已提交
1779 1780 1781 1782 1783 1784
				priv->encryption_level = 1;
			}
		}
	} else {
		/* Do we want to just set the transmit key index ? */
		int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
C
Carlo Perassi 已提交
1785
		if (index >= 0 && index < 4) {
L
Linus Torvalds 已提交
1786 1787 1788
			priv->default_key = index;
		} else
			/* Don't complain if only change the mode */
J
Jeff Garzik 已提交
1789
			if (!(dwrq->flags & IW_ENCODE_MODE))
L
Linus Torvalds 已提交
1790 1791 1792
				return -EINVAL;
	}
	/* Read the flags */
C
Carlo Perassi 已提交
1793
	if (dwrq->flags & IW_ENCODE_DISABLED) {
L
Linus Torvalds 已提交
1794
		priv->wep_is_on = 0;
C
Carlo Perassi 已提交
1795
		priv->encryption_level = 0;
L
Linus Torvalds 已提交
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
		priv->pairwise_cipher_suite = CIPHER_SUITE_NONE;
	} else {
		priv->wep_is_on = 1;
		if (priv->wep_key_len[priv->default_key] > 5) {
			priv->pairwise_cipher_suite = CIPHER_SUITE_WEP_128;
			priv->encryption_level = 2;
		} else {
			priv->pairwise_cipher_suite = CIPHER_SUITE_WEP_64;
			priv->encryption_level = 1;
		}
	}
C
Carlo Perassi 已提交
1807
	if (dwrq->flags & IW_ENCODE_RESTRICTED)
L
Linus Torvalds 已提交
1808
		priv->exclude_unencrypted = 1;
J
John Daiker 已提交
1809
	if (dwrq->flags & IW_ENCODE_OPEN)
L
Linus Torvalds 已提交
1810
		priv->exclude_unencrypted = 0;
C
Carlo Perassi 已提交
1811

L
Linus Torvalds 已提交
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
	return -EINPROGRESS;		/* Call commit handler */
}

static int atmel_get_encode(struct net_device *dev,
			    struct iw_request_info *info,
			    struct iw_point *dwrq,
			    char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
	int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
C
Carlo Perassi 已提交
1822

L
Linus Torvalds 已提交
1823 1824
	if (!priv->wep_is_on)
		dwrq->flags = IW_ENCODE_DISABLED;
1825 1826 1827 1828 1829 1830
	else {
		if (priv->exclude_unencrypted)
			dwrq->flags = IW_ENCODE_RESTRICTED;
		else
			dwrq->flags = IW_ENCODE_OPEN;
	}
L
Linus Torvalds 已提交
1831 1832 1833 1834 1835 1836 1837
		/* Which key do we want ? -1 -> tx index */
	if (index < 0 || index >= 4)
		index = priv->default_key;
	dwrq->flags |= index + 1;
	/* Copy the key to the user buffer */
	dwrq->length = priv->wep_key_len[index];
	if (dwrq->length > 16) {
J
John Daiker 已提交
1838
		dwrq->length = 0;
L
Linus Torvalds 已提交
1839 1840 1841 1842
	} else {
		memset(extra, 0, 16);
		memcpy(extra, priv->wep_keys[index], dwrq->length);
	}
C
Carlo Perassi 已提交
1843

L
Linus Torvalds 已提交
1844 1845 1846
	return 0;
}

1847 1848 1849 1850 1851 1852 1853 1854
static int atmel_set_encodeext(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu,
			    char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
	struct iw_point *encoding = &wrqu->encoding;
	struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1855
	int idx, key_len, alg = ext->alg, set_key = 1;
1856 1857 1858 1859

	/* Determine and validate the key index */
	idx = encoding->flags & IW_ENCODE_INDEX;
	if (idx) {
J
Johannes Berg 已提交
1860
		if (idx < 1 || idx > 4)
1861 1862 1863 1864 1865
			return -EINVAL;
		idx--;
	} else
		idx = priv->default_key;

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

1869
	if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
1870
		priv->default_key = idx;
1871 1872
		set_key = ext->key_len > 0 ? 1 : 0;
	}
1873

1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
	if (set_key) {
		/* Set the requested key first */
		switch (alg) {
		case IW_ENCODE_ALG_NONE:
			priv->wep_is_on = 0;
			priv->encryption_level = 0;
			priv->pairwise_cipher_suite = CIPHER_SUITE_NONE;
			break;
		case IW_ENCODE_ALG_WEP:
			if (ext->key_len > 5) {
				priv->wep_key_len[idx] = 13;
				priv->pairwise_cipher_suite = CIPHER_SUITE_WEP_128;
				priv->encryption_level = 2;
			} else if (ext->key_len > 0) {
				priv->wep_key_len[idx] = 5;
				priv->pairwise_cipher_suite = CIPHER_SUITE_WEP_64;
				priv->encryption_level = 1;
			} else {
				return -EINVAL;
			}
			priv->wep_is_on = 1;
			memset(priv->wep_keys[idx], 0, 13);
			key_len = min ((int)ext->key_len, priv->wep_key_len[idx]);
			memcpy(priv->wep_keys[idx], ext->key, key_len);
			break;
		default:
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
			return -EINVAL;
		}
	}

	return -EINPROGRESS;
}

static int atmel_get_encodeext(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu,
			    char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
	struct iw_point *encoding = &wrqu->encoding;
	struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
	int idx, max_key_len;

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

	idx = encoding->flags & IW_ENCODE_INDEX;
	if (idx) {
J
Johannes Berg 已提交
1923
		if (idx < 1 || idx > 4)
1924 1925 1926 1927 1928 1929 1930
			return -EINVAL;
		idx--;
	} else
		idx = priv->default_key;

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

1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
	if (!priv->wep_is_on) {
		ext->alg = IW_ENCODE_ALG_NONE;
		ext->key_len = 0;
		encoding->flags |= IW_ENCODE_DISABLED;
	} else {
		if (priv->encryption_level > 0)
			ext->alg = IW_ENCODE_ALG_WEP;
		else
			return -EINVAL;

		ext->key_len = priv->wep_key_len[idx];
		memcpy(ext->key, priv->wep_keys[idx], ext->key_len);
		encoding->flags |= IW_ENCODE_ENABLED;
	}

	return 0;
}

static int atmel_set_auth(struct net_device *dev,
			       struct iw_request_info *info,
			       union iwreq_data *wrqu, char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
	struct iw_param *param = &wrqu->param;

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

	case IW_AUTH_DROP_UNENCRYPTED:
		priv->exclude_unencrypted = param->value ? 1 : 0;
		break;

	case IW_AUTH_80211_AUTH_ALG: {
			if (param->value & IW_AUTH_ALG_SHARED_KEY) {
				priv->exclude_unencrypted = 1;
			} else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM) {
				priv->exclude_unencrypted = 0;
			} else
				return -EINVAL;
			break;
		}

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

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

static int atmel_get_auth(struct net_device *dev,
			       struct iw_request_info *info,
			       union iwreq_data *wrqu, char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
	struct iw_param *param = &wrqu->param;

	switch (param->flags & IW_AUTH_INDEX) {
	case IW_AUTH_DROP_UNENCRYPTED:
		param->value = priv->exclude_unencrypted;
		break;

	case IW_AUTH_80211_AUTH_ALG:
		if (priv->exclude_unencrypted == 1)
			param->value = IW_AUTH_ALG_SHARED_KEY;
		else
			param->value = IW_AUTH_ALG_OPEN_SYSTEM;
		break;

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

	default:
		return -EOPNOTSUPP;
	}
	return 0;
}


L
Linus Torvalds 已提交
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
static int atmel_get_name(struct net_device *dev,
			  struct iw_request_info *info,
			  char *cwrq,
			  char *extra)
{
	strcpy(cwrq, "IEEE 802.11-DS");
	return 0;
}

static int atmel_set_rate(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_param *vwrq,
			  char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
C
Carlo Perassi 已提交
2040

L
Linus Torvalds 已提交
2041 2042 2043 2044 2045
	if (vwrq->fixed == 0) {
		priv->tx_rate = 3;
		priv->auto_tx_rate = 1;
	} else {
		priv->auto_tx_rate = 0;
C
Carlo Perassi 已提交
2046

L
Linus Torvalds 已提交
2047
		/* Which type of value ? */
C
Carlo Perassi 已提交
2048
		if ((vwrq->value < 4) && (vwrq->value >= 0)) {
L
Linus Torvalds 已提交
2049
			/* Setting by rate index */
C
Carlo Perassi 已提交
2050
			priv->tx_rate = vwrq->value;
L
Linus Torvalds 已提交
2051 2052 2053
		} else {
		/* Setting by frequency value */
			switch (vwrq->value) {
J
John Daiker 已提交
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
			case  1000000:
				priv->tx_rate = 0;
				break;
			case  2000000:
				priv->tx_rate = 1;
				break;
			case  5500000:
				priv->tx_rate = 2;
				break;
			case 11000000:
				priv->tx_rate = 3;
				break;
			default:
				return -EINVAL;
L
Linus Torvalds 已提交
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085
			}
		}
	}

	return -EINPROGRESS;
}

static int atmel_set_mode(struct net_device *dev,
			  struct iw_request_info *info,
			  __u32 *uwrq,
			  char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);

	if (*uwrq != IW_MODE_ADHOC && *uwrq != IW_MODE_INFRA)
		return -EINVAL;

	priv->operating_mode = *uwrq;
C
Carlo Perassi 已提交
2086
	return -EINPROGRESS;
L
Linus Torvalds 已提交
2087 2088 2089 2090 2091 2092 2093 2094
}

static int atmel_get_mode(struct net_device *dev,
			  struct iw_request_info *info,
			  __u32 *uwrq,
			  char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
C
Carlo Perassi 已提交
2095

L
Linus Torvalds 已提交
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
	*uwrq = priv->operating_mode;
	return 0;
}

static int atmel_get_rate(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_param *vwrq,
			 char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);

	if (priv->auto_tx_rate) {
		vwrq->fixed = 0;
		vwrq->value = 11000000;
	} else {
		vwrq->fixed = 1;
J
John Daiker 已提交
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124
		switch (priv->tx_rate) {
		case 0:
			vwrq->value =  1000000;
			break;
		case 1:
			vwrq->value =  2000000;
			break;
		case 2:
			vwrq->value =  5500000;
			break;
		case 3:
			vwrq->value = 11000000;
			break;
L
Linus Torvalds 已提交
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
		}
	}
	return 0;
}

static int atmel_set_power(struct net_device *dev,
			   struct iw_request_info *info,
			   struct iw_param *vwrq,
			   char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
	priv->power_mode = vwrq->disabled ? 0 : 1;
	return -EINPROGRESS;
}

static int atmel_get_power(struct net_device *dev,
			   struct iw_request_info *info,
			   struct iw_param *vwrq,
			   char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
	vwrq->disabled = priv->power_mode ? 0 : 1;
	vwrq->flags = IW_POWER_ON;
	return 0;
}

static int atmel_set_retry(struct net_device *dev,
			   struct iw_request_info *info,
			   struct iw_param *vwrq,
			   char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
C
Carlo Perassi 已提交
2157 2158

	if (!vwrq->disabled && (vwrq->flags & IW_RETRY_LIMIT)) {
J
Jean Tourrilhes 已提交
2159
		if (vwrq->flags & IW_RETRY_LONG)
L
Linus Torvalds 已提交
2160
			priv->long_retry = vwrq->value;
J
Jean Tourrilhes 已提交
2161
		else if (vwrq->flags & IW_RETRY_SHORT)
L
Linus Torvalds 已提交
2162 2163 2164 2165 2166 2167
			priv->short_retry = vwrq->value;
		else {
			/* No modifier : set both */
			priv->long_retry = vwrq->value;
			priv->short_retry = vwrq->value;
		}
C
Carlo Perassi 已提交
2168
		return -EINPROGRESS;
L
Linus Torvalds 已提交
2169
	}
C
Carlo Perassi 已提交
2170

L
Linus Torvalds 已提交
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182
	return -EINVAL;
}

static int atmel_get_retry(struct net_device *dev,
			   struct iw_request_info *info,
			   struct iw_param *vwrq,
			   char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);

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

J
Jean Tourrilhes 已提交
2183 2184 2185
	/* Note : by default, display the short retry number */
	if (vwrq->flags & IW_RETRY_LONG) {
		vwrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
L
Linus Torvalds 已提交
2186 2187 2188 2189
		vwrq->value = priv->long_retry;
	} else {
		vwrq->flags = IW_RETRY_LIMIT;
		vwrq->value = priv->short_retry;
C
Carlo Perassi 已提交
2190
		if (priv->long_retry != priv->short_retry)
J
Jean Tourrilhes 已提交
2191
			vwrq->flags |= IW_RETRY_SHORT;
L
Linus Torvalds 已提交
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
	}

	return 0;
}

static int atmel_set_rts(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_param *vwrq,
			 char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
	int rthr = vwrq->value;

C
Carlo Perassi 已提交
2205
	if (vwrq->disabled)
L
Linus Torvalds 已提交
2206
		rthr = 2347;
C
Carlo Perassi 已提交
2207
	if ((rthr < 0) || (rthr > 2347)) {
L
Linus Torvalds 已提交
2208 2209 2210
		return -EINVAL;
	}
	priv->rts_threshold = rthr;
C
Carlo Perassi 已提交
2211

L
Linus Torvalds 已提交
2212 2213 2214 2215 2216 2217 2218 2219 2220
	return -EINPROGRESS;		/* Call commit handler */
}

static int atmel_get_rts(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_param *vwrq,
			 char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
C
Carlo Perassi 已提交
2221

L
Linus Torvalds 已提交
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
	vwrq->value = priv->rts_threshold;
	vwrq->disabled = (vwrq->value >= 2347);
	vwrq->fixed = 1;

	return 0;
}

static int atmel_set_frag(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_param *vwrq,
			  char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
	int fthr = vwrq->value;

C
Carlo Perassi 已提交
2237
	if (vwrq->disabled)
L
Linus Torvalds 已提交
2238
		fthr = 2346;
C
Carlo Perassi 已提交
2239
	if ((fthr < 256) || (fthr > 2346)) {
L
Linus Torvalds 已提交
2240 2241 2242 2243
		return -EINVAL;
	}
	fthr &= ~0x1;	/* Get an even value - is it really needed ??? */
	priv->frag_threshold = fthr;
C
Carlo Perassi 已提交
2244

L
Linus Torvalds 已提交
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
	return -EINPROGRESS;		/* Call commit handler */
}

static int atmel_get_frag(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_param *vwrq,
			  char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);

	vwrq->value = priv->frag_threshold;
	vwrq->disabled = (vwrq->value >= 2346);
	vwrq->fixed = 1;

	return 0;
}

static int atmel_set_freq(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_freq *fwrq,
			  char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
	int rc = -EINPROGRESS;		/* Call commit handler */
C
Carlo Perassi 已提交
2269

L
Linus Torvalds 已提交
2270
	/* If setting by frequency, convert to a channel */
2271
	if (fwrq->e == 1) {
L
Linus Torvalds 已提交
2272
		int f = fwrq->m / 100000;
2273

L
Linus Torvalds 已提交
2274 2275
		/* Hack to fall through... */
		fwrq->e = 0;
2276
		fwrq->m = ieee80211_freq_to_dsss_chan(f);
L
Linus Torvalds 已提交
2277 2278
	}
	/* Setting by channel number */
C
Carlo Perassi 已提交
2279
	if ((fwrq->m > 1000) || (fwrq->e > 0))
L
Linus Torvalds 已提交
2280 2281 2282 2283 2284 2285 2286
		rc = -EOPNOTSUPP;
	else {
		int channel = fwrq->m;
		if (atmel_validate_channel(priv, channel) == 0) {
			priv->channel = channel;
		} else {
			rc = -EINVAL;
C
Carlo Perassi 已提交
2287
		}
L
Linus Torvalds 已提交
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
	}
	return rc;
}

static int atmel_get_freq(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_freq *fwrq,
			  char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);

	fwrq->m = priv->channel;
	fwrq->e = 0;
	return 0;
}

static int atmel_set_scan(struct net_device *dev,
			  struct iw_request_info *info,
2306
			  struct iw_point *dwrq,
L
Linus Torvalds 已提交
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
			  char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
	unsigned long flags;

	/* Note : you may have realised that, as this is a SET operation,
	 * this is privileged and therefore a normal user can't
	 * perform scanning.
	 * This is not an error, while the device perform scanning,
	 * traffic doesn't flow, so it's a perfect DoS...
	 * Jean II */
C
Carlo Perassi 已提交
2318

L
Linus Torvalds 已提交
2319 2320 2321 2322
	if (priv->station_state == STATION_STATE_DOWN)
		return -EAGAIN;

	/* Timeout old surveys. */
2323
	if (time_after(jiffies, priv->last_survey + 20 * HZ))
L
Linus Torvalds 已提交
2324 2325 2326 2327 2328 2329
		priv->site_survey_state = SITE_SURVEY_IDLE;
	priv->last_survey = jiffies;

	/* Initiate a scan command */
	if (priv->site_survey_state == SITE_SURVEY_IN_PROGRESS)
		return -EBUSY;
C
Carlo Perassi 已提交
2330

L
Linus Torvalds 已提交
2331 2332
	del_timer_sync(&priv->management_timer);
	spin_lock_irqsave(&priv->irqlock, flags);
C
Carlo Perassi 已提交
2333

L
Linus Torvalds 已提交
2334 2335 2336 2337
	priv->site_survey_state = SITE_SURVEY_IN_PROGRESS;
	priv->fast_scan = 0;
	atmel_scan(priv, 0);
	spin_unlock_irqrestore(&priv->irqlock, flags);
C
Carlo Perassi 已提交
2338

L
Linus Torvalds 已提交
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350
	return 0;
}

static int atmel_get_scan(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_point *dwrq,
			  char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
	int i;
	char *current_ev = extra;
	struct iw_event	iwe;
C
Carlo Perassi 已提交
2351

L
Linus Torvalds 已提交
2352 2353
	if (priv->site_survey_state != SITE_SURVEY_COMPLETED)
		return -EAGAIN;
C
Carlo Perassi 已提交
2354 2355

	for (i = 0; i < priv->BSS_list_entries; i++) {
L
Linus Torvalds 已提交
2356 2357
		iwe.cmd = SIOCGIWAP;
		iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2358
		memcpy(iwe.u.ap_addr.sa_data, priv->BSSinfo[i].BSSID, ETH_ALEN);
2359 2360 2361
		current_ev = iwe_stream_add_event(info, current_ev,
						  extra + IW_SCAN_MAX_DATA,
						  &iwe, IW_EV_ADDR_LEN);
L
Linus Torvalds 已提交
2362 2363 2364 2365 2366 2367

		iwe.u.data.length =  priv->BSSinfo[i].SSIDsize;
		if (iwe.u.data.length > 32)
			iwe.u.data.length = 32;
		iwe.cmd = SIOCGIWESSID;
		iwe.u.data.flags = 1;
2368 2369 2370
		current_ev = iwe_stream_add_point(info, current_ev,
						  extra + IW_SCAN_MAX_DATA,
						  &iwe, priv->BSSinfo[i].SSID);
C
Carlo Perassi 已提交
2371

L
Linus Torvalds 已提交
2372 2373
		iwe.cmd = SIOCGIWMODE;
		iwe.u.mode = priv->BSSinfo[i].BSStype;
2374 2375 2376
		current_ev = iwe_stream_add_event(info, current_ev,
						  extra + IW_SCAN_MAX_DATA,
						  &iwe, IW_EV_UINT_LEN);
C
Carlo Perassi 已提交
2377

L
Linus Torvalds 已提交
2378 2379 2380
		iwe.cmd = SIOCGIWFREQ;
		iwe.u.freq.m = priv->BSSinfo[i].channel;
		iwe.u.freq.e = 0;
2381 2382 2383
		current_ev = iwe_stream_add_event(info, current_ev,
						  extra + IW_SCAN_MAX_DATA,
						  &iwe, IW_EV_FREQ_LEN);
C
Carlo Perassi 已提交
2384

2385 2386 2387 2388 2389
		/* Add quality statistics */
		iwe.cmd = IWEVQUAL;
		iwe.u.qual.level = priv->BSSinfo[i].RSSI;
		iwe.u.qual.qual  = iwe.u.qual.level;
		/* iwe.u.qual.noise  = SOMETHING */
2390 2391 2392
		current_ev = iwe_stream_add_event(info, current_ev,
						  extra + IW_SCAN_MAX_DATA,
						  &iwe, IW_EV_QUAL_LEN);
2393 2394


L
Linus Torvalds 已提交
2395 2396 2397 2398 2399 2400
		iwe.cmd = SIOCGIWENCODE;
		if (priv->BSSinfo[i].UsingWEP)
			iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
		else
			iwe.u.data.flags = IW_ENCODE_DISABLED;
		iwe.u.data.length = 0;
2401 2402 2403
		current_ev = iwe_stream_add_point(info, current_ev,
						  extra + IW_SCAN_MAX_DATA,
						  &iwe, NULL);
L
Linus Torvalds 已提交
2404 2405 2406 2407
	}

	/* Length of data */
	dwrq->length = (current_ev - extra);
C
Carlo Perassi 已提交
2408 2409
	dwrq->flags = 0;

L
Linus Torvalds 已提交
2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
	return 0;
}

static int atmel_get_range(struct net_device *dev,
			   struct iw_request_info *info,
			   struct iw_point *dwrq,
			   char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
	struct iw_range *range = (struct iw_range *) extra;
C
Carlo Perassi 已提交
2420
	int k, i, j;
L
Linus Torvalds 已提交
2421 2422

	dwrq->length = sizeof(struct iw_range);
2423
	memset(range, 0, sizeof(struct iw_range));
L
Linus Torvalds 已提交
2424 2425 2426
	range->min_nwid = 0x0000;
	range->max_nwid = 0x0000;
	range->num_channels = 0;
D
Dmitry Torokhov 已提交
2427
	for (j = 0; j < ARRAY_SIZE(channel_table); j++)
L
Linus Torvalds 已提交
2428 2429 2430 2431 2432
		if (priv->reg_domain == channel_table[j].reg_domain) {
			range->num_channels = channel_table[j].max - channel_table[j].min + 1;
			break;
		}
	if (range->num_channels != 0) {
C
Carlo Perassi 已提交
2433
		for (k = 0, i = channel_table[j].min; i <= channel_table[j].max; i++) {
L
Linus Torvalds 已提交
2434
			range->freq[k].i = i; /* List index */
2435 2436 2437 2438 2439

			/* Values in MHz -> * 10^5 * 10 */
			range->freq[k].m = (ieee80211_dsss_chan_to_freq(i) *
					    100000);
			range->freq[k++].e = 1;
L
Linus Torvalds 已提交
2440 2441 2442
		}
		range->num_frequency = k;
	}
C
Carlo Perassi 已提交
2443

L
Linus Torvalds 已提交
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
	range->max_qual.qual = 100;
	range->max_qual.level = 100;
	range->max_qual.noise = 0;
	range->max_qual.updated = IW_QUAL_NOISE_INVALID;

	range->avg_qual.qual = 50;
	range->avg_qual.level = 50;
	range->avg_qual.noise = 0;
	range->avg_qual.updated = IW_QUAL_NOISE_INVALID;

	range->sensitivity = 0;

	range->bitrate[0] =  1000000;
	range->bitrate[1] =  2000000;
	range->bitrate[2] =  5500000;
	range->bitrate[3] = 11000000;
	range->num_bitrates = 4;

	range->min_rts = 0;
	range->max_rts = 2347;
	range->min_frag = 256;
	range->max_frag = 2346;

	range->encoding_size[0] = 5;
	range->encoding_size[1] = 13;
	range->num_encoding_sizes = 2;
	range->max_encoding_tokens = 4;
C
Carlo Perassi 已提交
2471

L
Linus Torvalds 已提交
2472 2473 2474
	range->pmp_flags = IW_POWER_ON;
	range->pmt_flags = IW_POWER_ON;
	range->pm_capa = 0;
C
Carlo Perassi 已提交
2475

L
Linus Torvalds 已提交
2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
	range->we_version_source = WIRELESS_EXT;
	range->we_version_compiled = WIRELESS_EXT;
	range->retry_capa = IW_RETRY_LIMIT ;
	range->retry_flags = IW_RETRY_LIMIT;
	range->r_time_flags = 0;
	range->min_retry = 1;
	range->max_retry = 65535;

	return 0;
}

static int atmel_set_wap(struct net_device *dev,
			 struct iw_request_info *info,
			 struct sockaddr *awrq,
			 char *extra)
{
	struct atmel_private *priv = netdev_priv(dev);
	int i;
2494 2495
	static const u8 any[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
	static const u8 off[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
L
Linus Torvalds 已提交
2496 2497 2498 2499
	unsigned long flags;

	if (awrq->sa_family != ARPHRD_ETHER)
		return -EINVAL;
C
Carlo Perassi 已提交
2500

2501 2502
	if (!memcmp(any, awrq->sa_data, 6) ||
	    !memcmp(off, awrq->sa_data, 6)) {
L
Linus Torvalds 已提交
2503 2504 2505 2506 2507 2508
		del_timer_sync(&priv->management_timer);
		spin_lock_irqsave(&priv->irqlock, flags);
		atmel_scan(priv, 1);
		spin_unlock_irqrestore(&priv->irqlock, flags);
		return 0;
	}
C
Carlo Perassi 已提交
2509 2510

	for (i = 0; i < priv->BSS_list_entries; i++) {
L
Linus Torvalds 已提交
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524
		if (memcmp(priv->BSSinfo[i].BSSID, awrq->sa_data, 6) == 0) {
			if (!priv->wep_is_on && priv->BSSinfo[i].UsingWEP) {
				return -EINVAL;
			} else if  (priv->wep_is_on && !priv->BSSinfo[i].UsingWEP) {
				return -EINVAL;
			} else {
				del_timer_sync(&priv->management_timer);
				spin_lock_irqsave(&priv->irqlock, flags);
				atmel_join_bss(priv, i);
				spin_unlock_irqrestore(&priv->irqlock, flags);
				return 0;
			}
		}
	}
C
Carlo Perassi 已提交
2525

L
Linus Torvalds 已提交
2526 2527
	return -EINVAL;
}
C
Carlo Perassi 已提交
2528

L
Linus Torvalds 已提交
2529 2530 2531 2532 2533 2534 2535 2536
static int atmel_config_commit(struct net_device *dev,
			       struct iw_request_info *info,	/* NULL */
			       void *zwrq,			/* NULL */
			       char *extra)			/* NULL */
{
	return atmel_open(dev);
}

C
Carlo Perassi 已提交
2537
static const iw_handler atmel_handler[] =
L
Linus Torvalds 已提交
2538 2539
{
	(iw_handler) atmel_config_commit,	/* SIOCSIWCOMMIT */
C
Carlo Perassi 已提交
2540
	(iw_handler) atmel_get_name,		/* SIOCGIWNAME */
L
Linus Torvalds 已提交
2541 2542 2543 2544 2545 2546
	(iw_handler) NULL,			/* SIOCSIWNWID */
	(iw_handler) NULL,			/* SIOCGIWNWID */
	(iw_handler) atmel_set_freq,		/* SIOCSIWFREQ */
	(iw_handler) atmel_get_freq,		/* SIOCGIWFREQ */
	(iw_handler) atmel_set_mode,		/* SIOCSIWMODE */
	(iw_handler) atmel_get_mode,		/* SIOCGIWMODE */
C
Carlo Perassi 已提交
2547 2548
	(iw_handler) NULL,			/* SIOCSIWSENS */
	(iw_handler) NULL,			/* SIOCGIWSENS */
L
Linus Torvalds 已提交
2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561
	(iw_handler) NULL,			/* SIOCSIWRANGE */
	(iw_handler) atmel_get_range,           /* SIOCGIWRANGE */
	(iw_handler) NULL,			/* SIOCSIWPRIV */
	(iw_handler) NULL,			/* SIOCGIWPRIV */
	(iw_handler) NULL,			/* SIOCSIWSTATS */
	(iw_handler) NULL,			/* SIOCGIWSTATS */
	(iw_handler) NULL,			/* SIOCSIWSPY */
	(iw_handler) NULL,			/* SIOCGIWSPY */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) atmel_set_wap,		/* SIOCSIWAP */
	(iw_handler) atmel_get_wap,		/* SIOCGIWAP */
	(iw_handler) NULL,			/* -- hole -- */
C
Carlo Perassi 已提交
2562
	(iw_handler) NULL,			/* SIOCGIWAPLIST */
L
Linus Torvalds 已提交
2563 2564 2565 2566
	(iw_handler) atmel_set_scan,		/* SIOCSIWSCAN */
	(iw_handler) atmel_get_scan,		/* SIOCGIWSCAN */
	(iw_handler) atmel_set_essid,		/* SIOCSIWESSID */
	(iw_handler) atmel_get_essid,		/* SIOCGIWESSID */
C
Carlo Perassi 已提交
2567 2568
	(iw_handler) NULL,			/* SIOCSIWNICKN */
	(iw_handler) NULL,			/* SIOCGIWNICKN */
L
Linus Torvalds 已提交
2569 2570 2571 2572 2573 2574 2575 2576
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) atmel_set_rate,		/* SIOCSIWRATE */
	(iw_handler) atmel_get_rate,		/* SIOCGIWRATE */
	(iw_handler) atmel_set_rts,		/* SIOCSIWRTS */
	(iw_handler) atmel_get_rts,		/* SIOCGIWRTS */
	(iw_handler) atmel_set_frag,		/* SIOCSIWFRAG */
	(iw_handler) atmel_get_frag,		/* SIOCGIWFRAG */
C
Carlo Perassi 已提交
2577 2578
	(iw_handler) NULL,			/* SIOCSIWTXPOW */
	(iw_handler) NULL,			/* SIOCGIWTXPOW */
L
Linus Torvalds 已提交
2579 2580 2581 2582 2583 2584
	(iw_handler) atmel_set_retry,		/* SIOCSIWRETRY */
	(iw_handler) atmel_get_retry,		/* SIOCGIWRETRY */
	(iw_handler) atmel_set_encode,		/* SIOCSIWENCODE */
	(iw_handler) atmel_get_encode,		/* SIOCGIWENCODE */
	(iw_handler) atmel_set_power,		/* SIOCSIWPOWER */
	(iw_handler) atmel_get_power,		/* SIOCGIWPOWER */
2585 2586 2587 2588 2589 2590 2591 2592 2593
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) NULL,			/* SIOCSIWGENIE */
	(iw_handler) NULL,			/* SIOCGIWGENIE */
	(iw_handler) atmel_set_auth,		/* SIOCSIWAUTH */
	(iw_handler) atmel_get_auth,		/* SIOCGIWAUTH */
	(iw_handler) atmel_set_encodeext,	/* SIOCSIWENCODEEXT */
	(iw_handler) atmel_get_encodeext,	/* SIOCGIWENCODEEXT */
	(iw_handler) NULL,			/* SIOCSIWPMKSA */
L
Linus Torvalds 已提交
2594 2595
};

C
Carlo Perassi 已提交
2596
static const iw_handler atmel_private_handler[] =
L
Linus Torvalds 已提交
2597 2598 2599 2600 2601 2602
{
	NULL,				/* SIOCIWFIRSTPRIV */
};

typedef struct atmel_priv_ioctl {
	char id[32];
C
Carlo Perassi 已提交
2603 2604
	unsigned char __user *data;
	unsigned short len;
L
Linus Torvalds 已提交
2605 2606
} atmel_priv_ioctl;

C
Carlo Perassi 已提交
2607 2608 2609 2610
#define ATMELFWL	SIOCIWFIRSTPRIV
#define ATMELIDIFC	ATMELFWL + 1
#define ATMELRD		ATMELFWL + 2
#define ATMELMAGIC 0x51807
L
Linus Torvalds 已提交
2611 2612 2613
#define REGDOMAINSZ 20

static const struct iw_priv_args atmel_private_args[] = {
C
Carlo Perassi 已提交
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631
	{
		.cmd = ATMELFWL,
		.set_args = IW_PRIV_TYPE_BYTE
				| IW_PRIV_SIZE_FIXED
				| sizeof (atmel_priv_ioctl),
		.get_args = IW_PRIV_TYPE_NONE,
		.name = "atmelfwl"
	}, {
		.cmd = ATMELIDIFC,
		.set_args = IW_PRIV_TYPE_NONE,
		.get_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
		.name = "atmelidifc"
	}, {
		.cmd = ATMELRD,
		.set_args = IW_PRIV_TYPE_CHAR | REGDOMAINSZ,
		.get_args = IW_PRIV_TYPE_NONE,
		.name = "regdomain"
	},
L
Linus Torvalds 已提交
2632 2633
};

J
John Daiker 已提交
2634
static const struct iw_handler_def atmel_handler_def = {
D
Dmitry Torokhov 已提交
2635 2636 2637
	.num_standard	= ARRAY_SIZE(atmel_handler),
	.num_private	= ARRAY_SIZE(atmel_private_handler),
	.num_private_args = ARRAY_SIZE(atmel_private_args),
L
Linus Torvalds 已提交
2638
	.standard	= (iw_handler *) atmel_handler,
C
Carlo Perassi 已提交
2639
	.private	= (iw_handler *) atmel_private_handler,
2640 2641
	.private_args	= (struct iw_priv_args *) atmel_private_args,
	.get_wireless_stats = atmel_get_wireless_stats
L
Linus Torvalds 已提交
2642 2643 2644 2645 2646 2647 2648 2649 2650
};

static int atmel_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
	int i, rc = 0;
	struct atmel_private *priv = netdev_priv(dev);
	atmel_priv_ioctl com;
	struct iwreq *wrq = (struct iwreq *) rq;
	unsigned char *new_firmware;
C
Carlo Perassi 已提交
2651
	char domain[REGDOMAINSZ + 1];
L
Linus Torvalds 已提交
2652 2653 2654

	switch (cmd) {
	case ATMELIDIFC:
C
Carlo Perassi 已提交
2655
		wrq->u.param.value = ATMELMAGIC;
L
Linus Torvalds 已提交
2656
		break;
C
Carlo Perassi 已提交
2657

L
Linus Torvalds 已提交
2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679
	case ATMELFWL:
		if (copy_from_user(&com, rq->ifr_data, sizeof(com))) {
			rc = -EFAULT;
			break;
		}

		if (!capable(CAP_NET_ADMIN)) {
			rc = -EPERM;
			break;
		}

		if (!(new_firmware = kmalloc(com.len, GFP_KERNEL))) {
			rc = -ENOMEM;
			break;
		}

		if (copy_from_user(new_firmware, com.data, com.len)) {
			kfree(new_firmware);
			rc = -EFAULT;
			break;
		}

2680
		kfree(priv->firmware);
C
Carlo Perassi 已提交
2681

L
Linus Torvalds 已提交
2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692
		priv->firmware = new_firmware;
		priv->firmware_length = com.len;
		strncpy(priv->firmware_id, com.id, 31);
		priv->firmware_id[31] = '\0';
		break;

	case ATMELRD:
		if (copy_from_user(domain, rq->ifr_data, REGDOMAINSZ)) {
			rc = -EFAULT;
			break;
		}
C
Carlo Perassi 已提交
2693

L
Linus Torvalds 已提交
2694 2695 2696 2697 2698 2699 2700
		if (!capable(CAP_NET_ADMIN)) {
			rc = -EPERM;
			break;
		}

		domain[REGDOMAINSZ] = 0;
		rc = -EINVAL;
D
Dmitry Torokhov 已提交
2701
		for (i = 0; i < ARRAY_SIZE(channel_table); i++) {
L
Linus Torvalds 已提交
2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715
			/* strcasecmp doesn't exist in the library */
			char *a = channel_table[i].name;
			char *b = domain;
			while (*a) {
				char c1 = *a++;
				char c2 = *b++;
				if (tolower(c1) != tolower(c2))
					break;
			}
			if (!*a && !*b) {
				priv->config_reg_domain = channel_table[i].reg_domain;
				rc = 0;
			}
		}
C
Carlo Perassi 已提交
2716

L
Linus Torvalds 已提交
2717 2718 2719
		if (rc == 0 &&  priv->station_state != STATION_STATE_DOWN)
			rc = atmel_open(dev);
		break;
C
Carlo Perassi 已提交
2720

L
Linus Torvalds 已提交
2721 2722 2723
	default:
		rc = -EOPNOTSUPP;
	}
C
Carlo Perassi 已提交
2724

L
Linus Torvalds 已提交
2725 2726 2727 2728
	return rc;
}

struct auth_body {
A
Al Viro 已提交
2729 2730 2731
	__le16 alg;
	__le16 trans_seq;
	__le16 status;
L
Linus Torvalds 已提交
2732 2733 2734
	u8 el_id;
	u8 chall_text_len;
	u8 chall_text[253];
C
Carlo Perassi 已提交
2735
};
L
Linus Torvalds 已提交
2736 2737 2738 2739

static void atmel_enter_state(struct atmel_private *priv, int new_state)
{
	int old_state = priv->station_state;
C
Carlo Perassi 已提交
2740

L
Linus Torvalds 已提交
2741 2742
	if (new_state == old_state)
		return;
C
Carlo Perassi 已提交
2743

L
Linus Torvalds 已提交
2744
	priv->station_state = new_state;
C
Carlo Perassi 已提交
2745

L
Linus Torvalds 已提交
2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761
	if (new_state == STATION_STATE_READY) {
		netif_start_queue(priv->dev);
		netif_carrier_on(priv->dev);
	}

	if (old_state == STATION_STATE_READY) {
		netif_carrier_off(priv->dev);
		if (netif_running(priv->dev))
			netif_stop_queue(priv->dev);
		priv->last_beacon_timestamp = 0;
	}
}

static void atmel_scan(struct atmel_private *priv, int specific_ssid)
{
	struct {
2762
		u8 BSSID[ETH_ALEN];
L
Linus Torvalds 已提交
2763 2764 2765
		u8 SSID[MAX_SSID_LENGTH];
		u8 scan_type;
		u8 channel;
A
Al Viro 已提交
2766 2767 2768
		__le16 BSS_type;
		__le16 min_channel_time;
		__le16 max_channel_time;
L
Linus Torvalds 已提交
2769 2770 2771
		u8 options;
		u8 SSID_size;
	} cmd;
C
Carlo Perassi 已提交
2772

2773
	memset(cmd.BSSID, 0xff, ETH_ALEN);
L
Linus Torvalds 已提交
2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785

	if (priv->fast_scan) {
		cmd.SSID_size = priv->SSID_size;
		memcpy(cmd.SSID, priv->SSID, priv->SSID_size);
		cmd.min_channel_time = cpu_to_le16(10);
		cmd.max_channel_time = cpu_to_le16(50);
	} else {
		priv->BSS_list_entries = 0;
		cmd.SSID_size = 0;
		cmd.min_channel_time = cpu_to_le16(10);
		cmd.max_channel_time = cpu_to_le16(120);
	}
C
Carlo Perassi 已提交
2786

L
Linus Torvalds 已提交
2787
	cmd.options = 0;
C
Carlo Perassi 已提交
2788

L
Linus Torvalds 已提交
2789 2790
	if (!specific_ssid)
		cmd.options |= SCAN_OPTIONS_SITE_SURVEY;
C
Carlo Perassi 已提交
2791 2792

	cmd.channel = (priv->channel & 0x7f);
L
Linus Torvalds 已提交
2793
	cmd.scan_type = SCAN_TYPE_ACTIVE;
C
Carlo Perassi 已提交
2794
	cmd.BSS_type = cpu_to_le16(priv->operating_mode == IW_MODE_ADHOC ?
L
Linus Torvalds 已提交
2795
		BSS_TYPE_AD_HOC : BSS_TYPE_INFRASTRUCTURE);
C
Carlo Perassi 已提交
2796

L
Linus Torvalds 已提交
2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810
	atmel_send_command(priv, CMD_Scan, &cmd, sizeof(cmd));

	/* This must come after all hardware access to avoid being messed up
	   by stuff happening in interrupt context after we leave STATE_DOWN */
	atmel_enter_state(priv, STATION_STATE_SCANNING);
}

static void join(struct atmel_private *priv, int type)
{
	struct {
		u8 BSSID[6];
		u8 SSID[MAX_SSID_LENGTH];
		u8 BSS_type; /* this is a short in a scan command - weird */
		u8 channel;
A
Al Viro 已提交
2811
		__le16 timeout;
L
Linus Torvalds 已提交
2812 2813 2814 2815 2816 2817
		u8 SSID_size;
		u8 reserved;
	} cmd;

	cmd.SSID_size = priv->SSID_size;
	memcpy(cmd.SSID, priv->SSID, priv->SSID_size);
2818
	memcpy(cmd.BSSID, priv->CurrentBSSID, ETH_ALEN);
L
Linus Torvalds 已提交
2819 2820 2821 2822
	cmd.channel = (priv->channel & 0x7f);
	cmd.BSS_type = type;
	cmd.timeout = cpu_to_le16(2000);

C
Carlo Perassi 已提交
2823
	atmel_send_command(priv, CMD_Join, &cmd, sizeof(cmd));
L
Linus Torvalds 已提交
2824 2825 2826 2827 2828 2829 2830
}

static void start(struct atmel_private *priv, int type)
{
	struct {
		u8 BSSID[6];
		u8 SSID[MAX_SSID_LENGTH];
C
Carlo Perassi 已提交
2831
		u8 BSS_type;
L
Linus Torvalds 已提交
2832 2833 2834 2835 2836 2837 2838
		u8 channel;
		u8 SSID_size;
		u8 reserved[3];
	} cmd;

	cmd.SSID_size = priv->SSID_size;
	memcpy(cmd.SSID, priv->SSID, priv->SSID_size);
2839
	memcpy(cmd.BSSID, priv->BSSID, ETH_ALEN);
L
Linus Torvalds 已提交
2840 2841 2842
	cmd.BSS_type = type;
	cmd.channel = (priv->channel & 0x7f);

C
Carlo Perassi 已提交
2843
	atmel_send_command(priv, CMD_Start, &cmd, sizeof(cmd));
L
Linus Torvalds 已提交
2844 2845
}

C
Carlo Perassi 已提交
2846 2847
static void handle_beacon_probe(struct atmel_private *priv, u16 capability,
				u8 channel)
L
Linus Torvalds 已提交
2848 2849
{
	int rejoin = 0;
J
Johannes Berg 已提交
2850
	int new = capability & WLAN_CAPABILITY_SHORT_PREAMBLE ?
L
Linus Torvalds 已提交
2851 2852 2853 2854 2855 2856 2857
		SHORT_PREAMBLE : LONG_PREAMBLE;

	if (priv->preamble != new) {
		priv->preamble = new;
		rejoin = 1;
		atmel_set_mib8(priv, Local_Mib_Type, LOCAL_MIB_PREAMBLE_TYPE, new);
	}
C
Carlo Perassi 已提交
2858

L
Linus Torvalds 已提交
2859 2860 2861 2862 2863
	if (priv->channel != channel) {
		priv->channel = channel;
		rejoin = 1;
		atmel_set_mib8(priv, Phy_Mib_Type, PHY_MIB_CHANNEL_POS, channel);
	}
C
Carlo Perassi 已提交
2864

L
Linus Torvalds 已提交
2865 2866 2867
	if (rejoin) {
		priv->station_is_associated = 0;
		atmel_enter_state(priv, STATION_STATE_JOINNING);
C
Carlo Perassi 已提交
2868

L
Linus Torvalds 已提交
2869 2870
		if (priv->operating_mode == IW_MODE_INFRA)
			join(priv, BSS_TYPE_INFRASTRUCTURE);
C
Carlo Perassi 已提交
2871
		else
L
Linus Torvalds 已提交
2872
			join(priv, BSS_TYPE_AD_HOC);
C
Carlo Perassi 已提交
2873
	}
L
Linus Torvalds 已提交
2874 2875
}

C
Carlo Perassi 已提交
2876 2877
static void send_authentication_request(struct atmel_private *priv, u16 system,
					u8 *challenge, int challenge_len)
L
Linus Torvalds 已提交
2878
{
J
Johannes Berg 已提交
2879
	struct ieee80211_hdr header;
L
Linus Torvalds 已提交
2880
	struct auth_body auth;
C
Carlo Perassi 已提交
2881

J
Johannes Berg 已提交
2882
	header.frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_AUTH);
C
Carlo Perassi 已提交
2883
	header.duration_id = cpu_to_le16(0x8000);
J
Johannes Berg 已提交
2884
	header.seq_ctrl = 0;
2885 2886 2887
	memcpy(header.addr1, priv->CurrentBSSID, ETH_ALEN);
	memcpy(header.addr2, priv->dev->dev_addr, ETH_ALEN);
	memcpy(header.addr3, priv->CurrentBSSID, ETH_ALEN);
C
Carlo Perassi 已提交
2888 2889

	if (priv->wep_is_on && priv->CurrentAuthentTransactionSeqNum != 1)
L
Linus Torvalds 已提交
2890
		/* no WEP for authentication frames with TrSeqNo 1 */
J
John Daiker 已提交
2891
		header.frame_control |=  cpu_to_le16(IEEE80211_FCTL_PROTECTED);
C
Carlo Perassi 已提交
2892 2893

	auth.alg = cpu_to_le16(system);
L
Linus Torvalds 已提交
2894 2895 2896

	auth.status = 0;
	auth.trans_seq = cpu_to_le16(priv->CurrentAuthentTransactionSeqNum);
C
Carlo Perassi 已提交
2897
	priv->ExpectedAuthentTransactionSeqNum = priv->CurrentAuthentTransactionSeqNum+1;
L
Linus Torvalds 已提交
2898
	priv->CurrentAuthentTransactionSeqNum += 2;
C
Carlo Perassi 已提交
2899

L
Linus Torvalds 已提交
2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913
	if (challenge_len != 0)	{
		auth.el_id = 16; /* challenge_text */
		auth.chall_text_len = challenge_len;
		memcpy(auth.chall_text, challenge, challenge_len);
		atmel_transmit_management_frame(priv, &header, (u8 *)&auth, 8 + challenge_len);
	} else {
		atmel_transmit_management_frame(priv, &header, (u8 *)&auth, 6);
	}
}

static void send_association_request(struct atmel_private *priv, int is_reassoc)
{
	u8 *ssid_el_p;
	int bodysize;
J
Johannes Berg 已提交
2914
	struct ieee80211_hdr header;
L
Linus Torvalds 已提交
2915
	struct ass_req_format {
A
Al Viro 已提交
2916 2917
		__le16 capability;
		__le16 listen_interval;
2918
		u8 ap[ETH_ALEN]; /* nothing after here directly accessible */
L
Linus Torvalds 已提交
2919 2920 2921 2922 2923 2924 2925
		u8 ssid_el_id;
		u8 ssid_len;
		u8 ssid[MAX_SSID_LENGTH];
		u8 sup_rates_el_id;
		u8 sup_rates_len;
		u8 rates[4];
	} body;
C
Carlo Perassi 已提交
2926

J
Johannes Berg 已提交
2927
	header.frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
J
Jeff Garzik 已提交
2928
		(is_reassoc ? IEEE80211_STYPE_REASSOC_REQ : IEEE80211_STYPE_ASSOC_REQ));
L
Linus Torvalds 已提交
2929
	header.duration_id = cpu_to_le16(0x8000);
J
Johannes Berg 已提交
2930
	header.seq_ctrl = 0;
L
Linus Torvalds 已提交
2931

2932 2933 2934
	memcpy(header.addr1, priv->CurrentBSSID, ETH_ALEN);
	memcpy(header.addr2, priv->dev->dev_addr, ETH_ALEN);
	memcpy(header.addr3, priv->CurrentBSSID, ETH_ALEN);
L
Linus Torvalds 已提交
2935

2936
	body.capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
L
Linus Torvalds 已提交
2937
	if (priv->wep_is_on)
2938
		body.capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
L
Linus Torvalds 已提交
2939
	if (priv->preamble == SHORT_PREAMBLE)
J
Johannes Berg 已提交
2940
		body.capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
L
Linus Torvalds 已提交
2941 2942

	body.listen_interval = cpu_to_le16(priv->listen_interval * priv->beacon_period);
C
Carlo Perassi 已提交
2943

L
Linus Torvalds 已提交
2944 2945
	/* current AP address - only in reassoc frame */
	if (is_reassoc) {
2946
		memcpy(body.ap, priv->CurrentBSSID, ETH_ALEN);
2947
		ssid_el_p = &body.ssid_el_id;
L
Linus Torvalds 已提交
2948 2949
		bodysize = 18 + priv->SSID_size;
	} else {
2950
		ssid_el_p = &body.ap[0];
L
Linus Torvalds 已提交
2951 2952
		bodysize = 12 + priv->SSID_size;
	}
C
Carlo Perassi 已提交
2953

J
Johannes Berg 已提交
2954
	ssid_el_p[0] = WLAN_EID_SSID;
L
Linus Torvalds 已提交
2955 2956
	ssid_el_p[1] = priv->SSID_size;
	memcpy(ssid_el_p + 2, priv->SSID, priv->SSID_size);
J
Johannes Berg 已提交
2957
	ssid_el_p[2 + priv->SSID_size] = WLAN_EID_SUPP_RATES;
2958
	ssid_el_p[3 + priv->SSID_size] = 4; /* len of supported rates */
L
Linus Torvalds 已提交
2959 2960 2961 2962 2963
	memcpy(ssid_el_p + 4 + priv->SSID_size, atmel_basic_rates, 4);

	atmel_transmit_management_frame(priv, &header, (void *)&body, bodysize);
}

C
Carlo Perassi 已提交
2964
static int is_frame_from_current_bss(struct atmel_private *priv,
J
Johannes Berg 已提交
2965
				     struct ieee80211_hdr *header)
L
Linus Torvalds 已提交
2966
{
J
Johannes Berg 已提交
2967
	if (le16_to_cpu(header->frame_control) & IEEE80211_FCTL_FROMDS)
L
Linus Torvalds 已提交
2968 2969 2970 2971 2972 2973 2974 2975 2976 2977
		return memcmp(header->addr3, priv->CurrentBSSID, 6) == 0;
	else
		return memcmp(header->addr2, priv->CurrentBSSID, 6) == 0;
}

static int retrieve_bss(struct atmel_private *priv)
{
	int i;
	int max_rssi = -128;
	int max_index = -1;
C
Carlo Perassi 已提交
2978

L
Linus Torvalds 已提交
2979 2980
	if (priv->BSS_list_entries == 0)
		return -1;
C
Carlo Perassi 已提交
2981

L
Linus Torvalds 已提交
2982
	if (priv->connect_to_any_BSS) {
C
Carlo Perassi 已提交
2983 2984 2985 2986
		/* Select a BSS with the max-RSSI but of the same type and of
		   the same WEP mode and that it is not marked as 'bad' (i.e.
		   we had previously failed to connect to this BSS with the
		   settings that we currently use) */
L
Linus Torvalds 已提交
2987
		priv->current_BSS = 0;
C
Carlo Perassi 已提交
2988
		for (i = 0; i < priv->BSS_list_entries; i++) {
L
Linus Torvalds 已提交
2989
			if (priv->operating_mode == priv->BSSinfo[i].BSStype &&
C
Carlo Perassi 已提交
2990
			    ((!priv->wep_is_on && !priv->BSSinfo[i].UsingWEP) ||
L
Linus Torvalds 已提交
2991 2992 2993 2994 2995 2996 2997 2998
			     (priv->wep_is_on && priv->BSSinfo[i].UsingWEP)) &&
			    !(priv->BSSinfo[i].channel & 0x80)) {
				max_rssi = priv->BSSinfo[i].RSSI;
				priv->current_BSS = max_index = i;
			}
		}
		return max_index;
	}
C
Carlo Perassi 已提交
2999 3000

	for (i = 0; i < priv->BSS_list_entries; i++) {
L
Linus Torvalds 已提交
3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013
		if (priv->SSID_size == priv->BSSinfo[i].SSIDsize &&
		    memcmp(priv->SSID, priv->BSSinfo[i].SSID, priv->SSID_size) == 0 &&
		    priv->operating_mode == priv->BSSinfo[i].BSStype &&
		    atmel_validate_channel(priv, priv->BSSinfo[i].channel) == 0) {
			if (priv->BSSinfo[i].RSSI >= max_rssi) {
				max_rssi = priv->BSSinfo[i].RSSI;
				max_index = i;
			}
		}
	}
	return max_index;
}

C
Carlo Perassi 已提交
3014
static void store_bss_info(struct atmel_private *priv,
J
Johannes Berg 已提交
3015
			   struct ieee80211_hdr *header, u16 capability,
C
Carlo Perassi 已提交
3016 3017
			   u16 beacon_period, u8 channel, u8 rssi, u8 ssid_len,
			   u8 *ssid, int is_beacon)
L
Linus Torvalds 已提交
3018
{
3019
	u8 *bss = capability & WLAN_CAPABILITY_ESS ? header->addr2 : header->addr3;
L
Linus Torvalds 已提交
3020
	int i, index;
C
Carlo Perassi 已提交
3021 3022

	for (index = -1, i = 0; i < priv->BSS_list_entries; i++)
3023
		if (memcmp(bss, priv->BSSinfo[i].BSSID, ETH_ALEN) == 0)
L
Linus Torvalds 已提交
3024 3025
			index = i;

J
John Daiker 已提交
3026
	/* If we process a probe and an entry from this BSS exists
L
Linus Torvalds 已提交
3027 3028 3029 3030 3031 3032 3033
	   we will update the BSS entry with the info from this BSS.
	   If we process a beacon we will only update RSSI */

	if (index == -1) {
		if (priv->BSS_list_entries == MAX_BSS_ENTRIES)
			return;
		index = priv->BSS_list_entries++;
3034
		memcpy(priv->BSSinfo[index].BSSID, bss, ETH_ALEN);
L
Linus Torvalds 已提交
3035 3036 3037 3038 3039 3040 3041 3042 3043 3044
		priv->BSSinfo[index].RSSI = rssi;
	} else {
		if (rssi > priv->BSSinfo[index].RSSI)
			priv->BSSinfo[index].RSSI = rssi;
		if (is_beacon)
			return;
	}

	priv->BSSinfo[index].channel = channel;
	priv->BSSinfo[index].beacon_period = beacon_period;
3045
	priv->BSSinfo[index].UsingWEP = capability & WLAN_CAPABILITY_PRIVACY;
L
Linus Torvalds 已提交
3046 3047 3048
	memcpy(priv->BSSinfo[index].SSID, ssid, ssid_len);
	priv->BSSinfo[index].SSIDsize = ssid_len;

3049
	if (capability & WLAN_CAPABILITY_IBSS)
L
Linus Torvalds 已提交
3050
		priv->BSSinfo[index].BSStype = IW_MODE_ADHOC;
3051
	else if (capability & WLAN_CAPABILITY_ESS)
J
John Daiker 已提交
3052
		priv->BSSinfo[index].BSStype = IW_MODE_INFRA;
C
Carlo Perassi 已提交
3053

J
Johannes Berg 已提交
3054
	priv->BSSinfo[index].preamble = capability & WLAN_CAPABILITY_SHORT_PREAMBLE ?
L
Linus Torvalds 已提交
3055 3056 3057 3058 3059 3060 3061 3062
		SHORT_PREAMBLE : LONG_PREAMBLE;
}

static void authenticate(struct atmel_private *priv, u16 frame_len)
{
	struct auth_body *auth = (struct auth_body *)priv->rx_buf;
	u16 status = le16_to_cpu(auth->status);
	u16 trans_seq_no = le16_to_cpu(auth->trans_seq);
3063
	u16 system = le16_to_cpu(auth->alg);
C
Carlo Perassi 已提交
3064

3065
	if (status == WLAN_STATUS_SUCCESS && !priv->wep_is_on) {
L
Linus Torvalds 已提交
3066 3067 3068 3069 3070 3071 3072 3073 3074
		/* no WEP */
		if (priv->station_was_associated) {
			atmel_enter_state(priv, STATION_STATE_REASSOCIATING);
			send_association_request(priv, 1);
			return;
		} else {
			atmel_enter_state(priv, STATION_STATE_ASSOCIATING);
			send_association_request(priv, 0);
			return;
C
Carlo Perassi 已提交
3075
		}
L
Linus Torvalds 已提交
3076
	}
C
Carlo Perassi 已提交
3077

3078
	if (status == WLAN_STATUS_SUCCESS && priv->wep_is_on) {
3079
		int should_associate = 0;
L
Linus Torvalds 已提交
3080 3081 3082
		/* WEP */
		if (trans_seq_no != priv->ExpectedAuthentTransactionSeqNum)
			return;
C
Carlo Perassi 已提交
3083

3084
		if (system == WLAN_AUTH_OPEN) {
3085 3086 3087
			if (trans_seq_no == 0x0002) {
				should_associate = 1;
			}
3088
		} else if (system == WLAN_AUTH_SHARED_KEY) {
3089
			if (trans_seq_no == 0x0002 &&
J
Johannes Berg 已提交
3090
			    auth->el_id == WLAN_EID_CHALLENGE) {
3091 3092 3093 3094 3095
				send_authentication_request(priv, system, auth->chall_text, auth->chall_text_len);
				return;
			} else if (trans_seq_no == 0x0004) {
				should_associate = 1;
			}
L
Linus Torvalds 已提交
3096
		}
C
Carlo Perassi 已提交
3097

3098
		if (should_associate) {
J
John Daiker 已提交
3099
			if (priv->station_was_associated) {
L
Linus Torvalds 已提交
3100 3101 3102 3103 3104 3105 3106
				atmel_enter_state(priv, STATION_STATE_REASSOCIATING);
				send_association_request(priv, 1);
				return;
			} else {
				atmel_enter_state(priv, STATION_STATE_ASSOCIATING);
				send_association_request(priv, 0);
				return;
C
Carlo Perassi 已提交
3107
			}
L
Linus Torvalds 已提交
3108
		}
C
Carlo Perassi 已提交
3109 3110
	}

3111
	if (status == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
3112 3113 3114
		/* Flip back and forth between WEP auth modes until the max
		 * authentication tries has been exceeded.
		 */
3115
		if (system == WLAN_AUTH_OPEN) {
3116
			priv->CurrentAuthentTransactionSeqNum = 0x001;
3117 3118 3119
			priv->exclude_unencrypted = 1;
			send_authentication_request(priv, WLAN_AUTH_SHARED_KEY, NULL, 0);
			return;
J
John Daiker 已提交
3120 3121
		} else if (system == WLAN_AUTH_SHARED_KEY
			   && priv->wep_is_on) {
3122 3123 3124 3125
			priv->CurrentAuthentTransactionSeqNum = 0x001;
			priv->exclude_unencrypted = 0;
			send_authentication_request(priv, WLAN_AUTH_OPEN, NULL, 0);
			return;
3126 3127
		} else if (priv->connect_to_any_BSS) {
			int bss_index;
C
Carlo Perassi 已提交
3128

3129
			priv->BSSinfo[(int)(priv->current_BSS)].channel |= 0x80;
C
Carlo Perassi 已提交
3130

3131 3132 3133 3134
			if ((bss_index  = retrieve_bss(priv)) != -1) {
				atmel_join_bss(priv, bss_index);
				return;
			}
L
Linus Torvalds 已提交
3135 3136
		}
	}
C
Carlo Perassi 已提交
3137

L
Linus Torvalds 已提交
3138 3139 3140 3141 3142 3143 3144 3145
	priv->AuthenticationRequestRetryCnt = 0;
	atmel_enter_state(priv,  STATION_STATE_MGMT_ERROR);
	priv->station_is_associated = 0;
}

static void associate(struct atmel_private *priv, u16 frame_len, u16 subtype)
{
	struct ass_resp_format {
A
Al Viro 已提交
3146 3147 3148
		__le16 capability;
		__le16 status;
		__le16 ass_id;
L
Linus Torvalds 已提交
3149 3150 3151 3152
		u8 el_id;
		u8 length;
		u8 rates[4];
	} *ass_resp = (struct ass_resp_format *)priv->rx_buf;
C
Carlo Perassi 已提交
3153 3154

	u16 status = le16_to_cpu(ass_resp->status);
L
Linus Torvalds 已提交
3155
	u16 ass_id = le16_to_cpu(ass_resp->ass_id);
C
Carlo Perassi 已提交
3156 3157
	u16 rates_len = ass_resp->length > 4 ? 4 : ass_resp->length;

3158 3159
	union iwreq_data wrqu;

L
Linus Torvalds 已提交
3160 3161
	if (frame_len < 8 + rates_len)
		return;
C
Carlo Perassi 已提交
3162

3163 3164
	if (status == WLAN_STATUS_SUCCESS) {
		if (subtype == IEEE80211_STYPE_ASSOC_RESP)
L
Linus Torvalds 已提交
3165 3166 3167
			priv->AssociationRequestRetryCnt = 0;
		else
			priv->ReAssociationRequestRetryCnt = 0;
C
Carlo Perassi 已提交
3168 3169 3170 3171 3172

		atmel_set_mib16(priv, Mac_Mgmt_Mib_Type,
				MAC_MGMT_MIB_STATION_ID_POS, ass_id & 0x3fff);
		atmel_set_mib(priv, Phy_Mib_Type,
			      PHY_MIB_RATE_SET_POS, ass_resp->rates, rates_len);
L
Linus Torvalds 已提交
3173 3174
		if (priv->power_mode == 0) {
			priv->listen_interval = 1;
C
Carlo Perassi 已提交
3175 3176 3177 3178
			atmel_set_mib8(priv, Mac_Mgmt_Mib_Type,
				       MAC_MGMT_MIB_PS_MODE_POS, ACTIVE_MODE);
			atmel_set_mib16(priv, Mac_Mgmt_Mib_Type,
					MAC_MGMT_MIB_LISTEN_INTERVAL_POS, 1);
L
Linus Torvalds 已提交
3179 3180
		} else {
			priv->listen_interval = 2;
C
Carlo Perassi 已提交
3181 3182 3183 3184
			atmel_set_mib8(priv, Mac_Mgmt_Mib_Type,
				       MAC_MGMT_MIB_PS_MODE_POS,  PS_MODE);
			atmel_set_mib16(priv, Mac_Mgmt_Mib_Type,
					MAC_MGMT_MIB_LISTEN_INTERVAL_POS, 2);
L
Linus Torvalds 已提交
3185
		}
C
Carlo Perassi 已提交
3186

L
Linus Torvalds 已提交
3187 3188 3189
		priv->station_is_associated = 1;
		priv->station_was_associated = 1;
		atmel_enter_state(priv, STATION_STATE_READY);
3190 3191 3192 3193 3194 3195 3196 3197

		/* Send association event to userspace */
		wrqu.data.length = 0;
		wrqu.data.flags = 0;
		memcpy(wrqu.ap_addr.sa_data, priv->CurrentBSSID, ETH_ALEN);
		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
		wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);

L
Linus Torvalds 已提交
3198 3199
		return;
	}
C
Carlo Perassi 已提交
3200

3201 3202 3203
	if (subtype == IEEE80211_STYPE_ASSOC_RESP &&
	    status != WLAN_STATUS_ASSOC_DENIED_RATES &&
	    status != WLAN_STATUS_CAPS_UNSUPPORTED &&
L
Linus Torvalds 已提交
3204 3205 3206 3207 3208 3209
	    priv->AssociationRequestRetryCnt < MAX_ASSOCIATION_RETRIES) {
		mod_timer(&priv->management_timer, jiffies + MGMT_JIFFIES);
		priv->AssociationRequestRetryCnt++;
		send_association_request(priv, 0);
		return;
	}
C
Carlo Perassi 已提交
3210

3211 3212 3213
	if (subtype == IEEE80211_STYPE_REASSOC_RESP &&
	    status != WLAN_STATUS_ASSOC_DENIED_RATES &&
	    status != WLAN_STATUS_CAPS_UNSUPPORTED &&
3214
	    priv->ReAssociationRequestRetryCnt < MAX_ASSOCIATION_RETRIES) {
L
Linus Torvalds 已提交
3215 3216 3217 3218 3219
		mod_timer(&priv->management_timer, jiffies + MGMT_JIFFIES);
		priv->ReAssociationRequestRetryCnt++;
		send_association_request(priv, 1);
		return;
	}
C
Carlo Perassi 已提交
3220

L
Linus Torvalds 已提交
3221 3222
	atmel_enter_state(priv,  STATION_STATE_MGMT_ERROR);
	priv->station_is_associated = 0;
C
Carlo Perassi 已提交
3223 3224

	if (priv->connect_to_any_BSS) {
L
Linus Torvalds 已提交
3225 3226
		int bss_index;
		priv->BSSinfo[(int)(priv->current_BSS)].channel |= 0x80;
C
Carlo Perassi 已提交
3227 3228

		if ((bss_index = retrieve_bss(priv)) != -1)
L
Linus Torvalds 已提交
3229 3230 3231 3232
			atmel_join_bss(priv, bss_index);
	}
}

3233
static void atmel_join_bss(struct atmel_private *priv, int bss_index)
L
Linus Torvalds 已提交
3234 3235 3236
{
	struct bss_info *bss =  &priv->BSSinfo[bss_index];

3237
	memcpy(priv->CurrentBSSID, bss->BSSID, ETH_ALEN);
L
Linus Torvalds 已提交
3238 3239 3240 3241 3242
	memcpy(priv->SSID, bss->SSID, priv->SSID_size = bss->SSIDsize);

	/* The WPA stuff cares about the current AP address */
	if (priv->use_wpa)
		build_wpa_mib(priv);
C
Carlo Perassi 已提交
3243

L
Linus Torvalds 已提交
3244 3245 3246 3247 3248 3249 3250
	/* When switching to AdHoc turn OFF Power Save if needed */

	if (bss->BSStype == IW_MODE_ADHOC &&
	    priv->operating_mode != IW_MODE_ADHOC &&
	    priv->power_mode) {
		priv->power_mode = 0;
		priv->listen_interval = 1;
C
Carlo Perassi 已提交
3251 3252 3253 3254
		atmel_set_mib8(priv, Mac_Mgmt_Mib_Type,
			       MAC_MGMT_MIB_PS_MODE_POS,  ACTIVE_MODE);
		atmel_set_mib16(priv, Mac_Mgmt_Mib_Type,
				MAC_MGMT_MIB_LISTEN_INTERVAL_POS, 1);
L
Linus Torvalds 已提交
3255
	}
C
Carlo Perassi 已提交
3256

L
Linus Torvalds 已提交
3257
	priv->operating_mode = bss->BSStype;
C
Carlo Perassi 已提交
3258
	priv->channel = bss->channel & 0x7f;
L
Linus Torvalds 已提交
3259
	priv->beacon_period = bss->beacon_period;
C
Carlo Perassi 已提交
3260

L
Linus Torvalds 已提交
3261 3262
	if (priv->preamble != bss->preamble) {
		priv->preamble = bss->preamble;
C
Carlo Perassi 已提交
3263 3264
		atmel_set_mib8(priv, Local_Mib_Type,
			       LOCAL_MIB_PREAMBLE_TYPE, bss->preamble);
L
Linus Torvalds 已提交
3265
	}
C
Carlo Perassi 已提交
3266

L
Linus Torvalds 已提交
3267 3268 3269 3270 3271
	if (!priv->wep_is_on && bss->UsingWEP) {
		atmel_enter_state(priv, STATION_STATE_MGMT_ERROR);
		priv->station_is_associated = 0;
		return;
	}
C
Carlo Perassi 已提交
3272

L
Linus Torvalds 已提交
3273 3274 3275 3276 3277 3278 3279
	if (priv->wep_is_on && !bss->UsingWEP) {
		atmel_enter_state(priv, STATION_STATE_MGMT_ERROR);
		priv->station_is_associated = 0;
		return;
	}

	atmel_enter_state(priv, STATION_STATE_JOINNING);
C
Carlo Perassi 已提交
3280

L
Linus Torvalds 已提交
3281 3282
	if (priv->operating_mode == IW_MODE_INFRA)
		join(priv, BSS_TYPE_INFRASTRUCTURE);
C
Carlo Perassi 已提交
3283
	else
L
Linus Torvalds 已提交
3284 3285 3286 3287 3288 3289
		join(priv, BSS_TYPE_AD_HOC);
}

static void restart_search(struct atmel_private *priv)
{
	int bss_index;
C
Carlo Perassi 已提交
3290

L
Linus Torvalds 已提交
3291 3292 3293 3294
	if (!priv->connect_to_any_BSS) {
		atmel_scan(priv, 1);
	} else {
		priv->BSSinfo[(int)(priv->current_BSS)].channel |= 0x80;
C
Carlo Perassi 已提交
3295 3296

		if ((bss_index = retrieve_bss(priv)) != -1)
L
Linus Torvalds 已提交
3297 3298 3299
			atmel_join_bss(priv, bss_index);
		else
			atmel_scan(priv, 0);
C
Carlo Perassi 已提交
3300 3301
	}
}
L
Linus Torvalds 已提交
3302 3303 3304 3305 3306 3307 3308

static void smooth_rssi(struct atmel_private *priv, u8 rssi)
{
	u8 old = priv->wstats.qual.level;
	u8 max_rssi = 42; /* 502-rmfd-revd max by experiment, default for now */

	switch (priv->firmware_type) {
J
John Daiker 已提交
3309 3310 3311 3312 3313
	case ATMEL_FW_TYPE_502E:
		max_rssi = 63; /* 502-rmfd-reve max by experiment */
		break;
	default:
		break;
L
Linus Torvalds 已提交
3314 3315 3316
	}

	rssi = rssi * 100 / max_rssi;
C
Carlo Perassi 已提交
3317 3318
	if ((rssi + old) % 2)
		priv->wstats.qual.level = (rssi + old) / 2 + 1;
L
Linus Torvalds 已提交
3319
	else
C
Carlo Perassi 已提交
3320
		priv->wstats.qual.level = (rssi + old) / 2;
L
Linus Torvalds 已提交
3321 3322 3323 3324 3325 3326
	priv->wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
	priv->wstats.qual.updated &= ~IW_QUAL_LEVEL_INVALID;
}

static void atmel_smooth_qual(struct atmel_private *priv)
{
C
Carlo Perassi 已提交
3327
	unsigned long time_diff = (jiffies - priv->last_qual) / HZ;
L
Linus Torvalds 已提交
3328 3329
	while (time_diff--) {
		priv->last_qual += HZ;
C
Carlo Perassi 已提交
3330 3331
		priv->wstats.qual.qual = priv->wstats.qual.qual / 2;
		priv->wstats.qual.qual +=
L
Linus Torvalds 已提交
3332 3333 3334 3335 3336 3337 3338
			priv->beacons_this_sec * priv->beacon_period * (priv->wstats.qual.level + 100) / 4000;
		priv->beacons_this_sec = 0;
	}
	priv->wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
	priv->wstats.qual.updated &= ~IW_QUAL_QUAL_INVALID;
}

3339
/* deals with incoming management frames. */
C
Carlo Perassi 已提交
3340
static void atmel_management_frame(struct atmel_private *priv,
J
Johannes Berg 已提交
3341
				   struct ieee80211_hdr *header,
C
Carlo Perassi 已提交
3342
				   u16 frame_len, u8 rssi)
L
Linus Torvalds 已提交
3343 3344
{
	u16 subtype;
C
Carlo Perassi 已提交
3345

J
Johannes Berg 已提交
3346
	subtype = le16_to_cpu(header->frame_control) & IEEE80211_FCTL_STYPE;
C
Carlo Perassi 已提交
3347
	switch (subtype) {
3348 3349
	case IEEE80211_STYPE_BEACON:
	case IEEE80211_STYPE_PROBE_RESP:
C
Carlo Perassi 已提交
3350

L
Linus Torvalds 已提交
3351 3352 3353 3354
		/* beacon frame has multiple variable-length fields -
		   never let an engineer loose with a data structure design. */
		{
			struct beacon_format {
A
Al Viro 已提交
3355 3356 3357
				__le64 timestamp;
				__le16 interval;
				__le16 capability;
L
Linus Torvalds 已提交
3358 3359 3360 3361 3362 3363 3364 3365 3366 3367
				u8 ssid_el_id;
				u8 ssid_length;
				/* ssid here */
				u8 rates_el_id;
				u8 rates_length;
				/* rates here */
				u8 ds_el_id;
				u8 ds_length;
				/* ds here */
			} *beacon = (struct beacon_format *)priv->rx_buf;
C
Carlo Perassi 已提交
3368

L
Linus Torvalds 已提交
3369 3370 3371 3372 3373 3374 3375
			u8 channel, rates_length, ssid_length;
			u64 timestamp = le64_to_cpu(beacon->timestamp);
			u16 beacon_interval = le16_to_cpu(beacon->interval);
			u16 capability = le16_to_cpu(beacon->capability);
			u8 *beaconp = priv->rx_buf;
			ssid_length = beacon->ssid_length;
			/* this blows chunks. */
C
Carlo Perassi 已提交
3376
			if (frame_len < 14 || frame_len < ssid_length + 15)
L
Linus Torvalds 已提交
3377 3378 3379 3380 3381 3382 3383
				return;
			rates_length = beaconp[beacon->ssid_length + 15];
			if (frame_len < ssid_length + rates_length + 18)
				return;
			if (ssid_length >  MAX_SSID_LENGTH)
				return;
			channel = beaconp[ssid_length + rates_length + 18];
C
Carlo Perassi 已提交
3384

L
Linus Torvalds 已提交
3385 3386
			if (priv->station_state == STATION_STATE_READY) {
				smooth_rssi(priv, rssi);
C
Carlo Perassi 已提交
3387
				if (is_frame_from_current_bss(priv, header)) {
L
Linus Torvalds 已提交
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400
					priv->beacons_this_sec++;
					atmel_smooth_qual(priv);
					if (priv->last_beacon_timestamp) {
						/* Note truncate this to 32 bits - kernel can't divide a long long */
						u32 beacon_delay = timestamp - priv->last_beacon_timestamp;
						int beacons = beacon_delay / (beacon_interval * 1000);
						if (beacons > 1)
							priv->wstats.miss.beacon += beacons - 1;
					}
					priv->last_beacon_timestamp = timestamp;
					handle_beacon_probe(priv, capability, channel);
				}
			}
C
Carlo Perassi 已提交
3401 3402 3403 3404 3405 3406

			if (priv->station_state == STATION_STATE_SCANNING)
				store_bss_info(priv, header, capability,
					       beacon_interval, channel, rssi,
					       ssid_length,
					       &beacon->rates_el_id,
3407
					       subtype == IEEE80211_STYPE_BEACON);
L
Linus Torvalds 已提交
3408 3409
		}
		break;
C
Carlo Perassi 已提交
3410

3411
	case IEEE80211_STYPE_AUTH:
L
Linus Torvalds 已提交
3412 3413 3414

		if (priv->station_state == STATION_STATE_AUTHENTICATING)
			authenticate(priv, frame_len);
C
Carlo Perassi 已提交
3415

L
Linus Torvalds 已提交
3416
		break;
C
Carlo Perassi 已提交
3417

3418 3419
	case IEEE80211_STYPE_ASSOC_RESP:
	case IEEE80211_STYPE_REASSOC_RESP:
C
Carlo Perassi 已提交
3420 3421

		if (priv->station_state == STATION_STATE_ASSOCIATING ||
L
Linus Torvalds 已提交
3422 3423
		    priv->station_state == STATION_STATE_REASSOCIATING)
			associate(priv, frame_len, subtype);
C
Carlo Perassi 已提交
3424

L
Linus Torvalds 已提交
3425 3426
		break;

3427
	case IEEE80211_STYPE_DISASSOC:
C
Carlo Perassi 已提交
3428 3429
		if (priv->station_is_associated &&
		    priv->operating_mode == IW_MODE_INFRA &&
L
Linus Torvalds 已提交
3430 3431 3432
		    is_frame_from_current_bss(priv, header)) {
			priv->station_was_associated = 0;
			priv->station_is_associated = 0;
C
Carlo Perassi 已提交
3433

L
Linus Torvalds 已提交
3434 3435 3436
			atmel_enter_state(priv, STATION_STATE_JOINNING);
			join(priv, BSS_TYPE_INFRASTRUCTURE);
		}
C
Carlo Perassi 已提交
3437

L
Linus Torvalds 已提交
3438 3439
		break;

3440
	case IEEE80211_STYPE_DEAUTH:
L
Linus Torvalds 已提交
3441 3442 3443 3444 3445 3446 3447
		if (priv->operating_mode == IW_MODE_INFRA &&
		    is_frame_from_current_bss(priv, header)) {
			priv->station_was_associated = 0;

			atmel_enter_state(priv, STATION_STATE_JOINNING);
			join(priv, BSS_TYPE_INFRASTRUCTURE);
		}
C
Carlo Perassi 已提交
3448

L
Linus Torvalds 已提交
3449 3450 3451 3452 3453 3454 3455
		break;
	}
}

/* run when timer expires */
static void atmel_management_timer(u_long a)
{
C
Carlo Perassi 已提交
3456 3457 3458
	struct net_device *dev = (struct net_device *) a;
	struct atmel_private *priv = netdev_priv(dev);
	unsigned long flags;
L
Linus Torvalds 已提交
3459

C
Carlo Perassi 已提交
3460 3461 3462 3463
	/* Check if the card has been yanked. */
	if (priv->card && priv->present_callback &&
		!(*priv->present_callback)(priv->card))
		return;
L
Linus Torvalds 已提交
3464

C
Carlo Perassi 已提交
3465 3466 3467
	spin_lock_irqsave(&priv->irqlock, flags);

	switch (priv->station_state) {
L
Linus Torvalds 已提交
3468

C
Carlo Perassi 已提交
3469 3470 3471 3472 3473 3474 3475
	case STATION_STATE_AUTHENTICATING:
		if (priv->AuthenticationRequestRetryCnt >= MAX_AUTHENTICATION_RETRIES) {
			atmel_enter_state(priv, STATION_STATE_MGMT_ERROR);
			priv->station_is_associated = 0;
			priv->AuthenticationRequestRetryCnt = 0;
			restart_search(priv);
		} else {
3476
			int auth = WLAN_AUTH_OPEN;
C
Carlo Perassi 已提交
3477 3478 3479
			priv->AuthenticationRequestRetryCnt++;
			priv->CurrentAuthentTransactionSeqNum = 0x0001;
			mod_timer(&priv->management_timer, jiffies + MGMT_JIFFIES);
3480
			if (priv->wep_is_on && priv->exclude_unencrypted)
3481
				auth = WLAN_AUTH_SHARED_KEY;
3482
			send_authentication_request(priv, auth, NULL, 0);
C
Carlo Perassi 已提交
3483
	  }
L
Linus Torvalds 已提交
3484
	  break;
C
Carlo Perassi 已提交
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496

	case STATION_STATE_ASSOCIATING:
		if (priv->AssociationRequestRetryCnt == MAX_ASSOCIATION_RETRIES) {
			atmel_enter_state(priv, STATION_STATE_MGMT_ERROR);
			priv->station_is_associated = 0;
			priv->AssociationRequestRetryCnt = 0;
			restart_search(priv);
		} else {
			priv->AssociationRequestRetryCnt++;
			mod_timer(&priv->management_timer, jiffies + MGMT_JIFFIES);
			send_association_request(priv, 0);
		}
L
Linus Torvalds 已提交
3497
	  break;
C
Carlo Perassi 已提交
3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516

	case STATION_STATE_REASSOCIATING:
		if (priv->ReAssociationRequestRetryCnt == MAX_ASSOCIATION_RETRIES) {
			atmel_enter_state(priv, STATION_STATE_MGMT_ERROR);
			priv->station_is_associated = 0;
			priv->ReAssociationRequestRetryCnt = 0;
			restart_search(priv);
		} else {
			priv->ReAssociationRequestRetryCnt++;
			mod_timer(&priv->management_timer, jiffies + MGMT_JIFFIES);
			send_association_request(priv, 1);
		}
		break;

	default:
		break;
	}

	spin_unlock_irqrestore(&priv->irqlock, flags);
L
Linus Torvalds 已提交
3517
}
C
Carlo Perassi 已提交
3518

L
Linus Torvalds 已提交
3519 3520 3521 3522 3523
static void atmel_command_irq(struct atmel_private *priv)
{
	u8 status = atmel_rmem8(priv, atmel_co(priv, CMD_BLOCK_STATUS_OFFSET));
	u8 command = atmel_rmem8(priv, atmel_co(priv, CMD_BLOCK_COMMAND_OFFSET));
	int fast_scan;
3524
	union iwreq_data wrqu;
C
Carlo Perassi 已提交
3525 3526

	if (status == CMD_STATUS_IDLE ||
L
Linus Torvalds 已提交
3527 3528 3529
	    status == CMD_STATUS_IN_PROGRESS)
		return;

J
John Daiker 已提交
3530
	switch (command) {
L
Linus Torvalds 已提交
3531 3532 3533 3534 3535 3536
	case CMD_Start:
		if (status == CMD_STATUS_COMPLETE) {
			priv->station_was_associated = priv->station_is_associated;
			atmel_get_mib(priv, Mac_Mgmt_Mib_Type, MAC_MGMT_MIB_CUR_BSSID_POS,
				      (u8 *)priv->CurrentBSSID, 6);
			atmel_enter_state(priv, STATION_STATE_READY);
C
Carlo Perassi 已提交
3537
		}
L
Linus Torvalds 已提交
3538
		break;
C
Carlo Perassi 已提交
3539

L
Linus Torvalds 已提交
3540 3541 3542
	case CMD_Scan:
		fast_scan = priv->fast_scan;
		priv->fast_scan = 0;
C
Carlo Perassi 已提交
3543

L
Linus Torvalds 已提交
3544 3545 3546 3547
		if (status != CMD_STATUS_COMPLETE) {
			atmel_scan(priv, 1);
		} else {
			int bss_index = retrieve_bss(priv);
3548
			int notify_scan_complete = 1;
L
Linus Torvalds 已提交
3549 3550
			if (bss_index != -1) {
				atmel_join_bss(priv, bss_index);
C
Carlo Perassi 已提交
3551
			} else if (priv->operating_mode == IW_MODE_ADHOC &&
L
Linus Torvalds 已提交
3552 3553 3554 3555 3556
				   priv->SSID_size != 0) {
				start(priv, BSS_TYPE_AD_HOC);
			} else {
				priv->fast_scan = !fast_scan;
				atmel_scan(priv, 1);
3557
				notify_scan_complete = 0;
L
Linus Torvalds 已提交
3558 3559
			}
			priv->site_survey_state = SITE_SURVEY_COMPLETED;
3560 3561 3562 3563 3564
			if (notify_scan_complete) {
				wrqu.data.length = 0;
				wrqu.data.flags = 0;
				wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
			}
L
Linus Torvalds 已提交
3565 3566
		}
		break;
C
Carlo Perassi 已提交
3567

L
Linus Torvalds 已提交
3568 3569
	case CMD_SiteSurvey:
		priv->fast_scan = 0;
C
Carlo Perassi 已提交
3570

L
Linus Torvalds 已提交
3571 3572
		if (status != CMD_STATUS_COMPLETE)
			return;
C
Carlo Perassi 已提交
3573

L
Linus Torvalds 已提交
3574 3575
		priv->site_survey_state = SITE_SURVEY_COMPLETED;
		if (priv->station_is_associated) {
C
Carlo Perassi 已提交
3576
			atmel_enter_state(priv, STATION_STATE_READY);
3577 3578 3579
			wrqu.data.length = 0;
			wrqu.data.flags = 0;
			wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
L
Linus Torvalds 已提交
3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590
		} else {
			atmel_scan(priv, 1);
		}
		break;

	case CMD_Join:
		if (status == CMD_STATUS_COMPLETE) {
			if (priv->operating_mode == IW_MODE_ADHOC) {
				priv->station_was_associated = priv->station_is_associated;
				atmel_enter_state(priv, STATION_STATE_READY);
			} else {
3591
				int auth = WLAN_AUTH_OPEN;
L
Linus Torvalds 已提交
3592 3593
				priv->AuthenticationRequestRetryCnt = 0;
				atmel_enter_state(priv, STATION_STATE_AUTHENTICATING);
C
Carlo Perassi 已提交
3594

L
Linus Torvalds 已提交
3595 3596
				mod_timer(&priv->management_timer, jiffies + MGMT_JIFFIES);
				priv->CurrentAuthentTransactionSeqNum = 0x0001;
3597
				if (priv->wep_is_on && priv->exclude_unencrypted)
3598
					auth = WLAN_AUTH_SHARED_KEY;
3599
				send_authentication_request(priv, auth, NULL, 0);
L
Linus Torvalds 已提交
3600 3601 3602
			}
			return;
		}
C
Carlo Perassi 已提交
3603

L
Linus Torvalds 已提交
3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615
		atmel_scan(priv, 1);
	}
}

static int atmel_wakeup_firmware(struct atmel_private *priv)
{
	struct host_info_struct *iface = &priv->host_info;
	u16 mr1, mr3;
	int i;

	if (priv->card_type == CARD_TYPE_SPI_FLASH)
		atmel_set_gcr(priv->dev, GCR_REMAP);
C
Carlo Perassi 已提交
3616

L
Linus Torvalds 已提交
3617 3618 3619
	/* wake up on-board processor */
	atmel_clear_gcr(priv->dev, 0x0040);
	atmel_write16(priv->dev, BSR, BSS_SRAM);
C
Carlo Perassi 已提交
3620

L
Linus Torvalds 已提交
3621 3622 3623 3624
	if (priv->card_type == CARD_TYPE_SPI_FLASH)
		mdelay(100);

	/* and wait for it */
C
Carlo Perassi 已提交
3625
	for (i = LOOP_RETRY_LIMIT; i; i--) {
L
Linus Torvalds 已提交
3626 3627
		mr1 = atmel_read16(priv->dev, MR1);
		mr3 = atmel_read16(priv->dev, MR3);
C
Carlo Perassi 已提交
3628 3629

		if (mr3 & MAC_BOOT_COMPLETE)
L
Linus Torvalds 已提交
3630 3631 3632 3633 3634 3635 3636 3637
			break;
		if (mr1 & MAC_BOOT_COMPLETE &&
		    priv->bus_type == BUS_TYPE_PCCARD)
			break;
	}

	if (i == 0) {
		printk(KERN_ALERT "%s: MAC failed to boot.\n", priv->dev->name);
3638
		return -EIO;
L
Linus Torvalds 已提交
3639
	}
C
Carlo Perassi 已提交
3640

L
Linus Torvalds 已提交
3641 3642
	if ((priv->host_info_base = atmel_read16(priv->dev, MR2)) == 0xffff) {
		printk(KERN_ALERT "%s: card missing.\n", priv->dev->name);
3643
		return -ENODEV;
L
Linus Torvalds 已提交
3644
	}
C
Carlo Perassi 已提交
3645 3646 3647 3648 3649 3650

	/* now check for completion of MAC initialization through
	   the FunCtrl field of the IFACE, poll MR1 to detect completion of
	   MAC initialization, check completion status, set interrupt mask,
	   enables interrupts and calls Tx and Rx initialization functions */

L
Linus Torvalds 已提交
3651
	atmel_wmem8(priv, atmel_hi(priv, IFACE_FUNC_CTRL_OFFSET), FUNC_CTRL_INIT_COMPLETE);
C
Carlo Perassi 已提交
3652 3653

	for (i = LOOP_RETRY_LIMIT; i; i--) {
L
Linus Torvalds 已提交
3654 3655
		mr1 = atmel_read16(priv->dev, MR1);
		mr3 = atmel_read16(priv->dev, MR3);
C
Carlo Perassi 已提交
3656 3657

		if (mr3 & MAC_INIT_COMPLETE)
L
Linus Torvalds 已提交
3658 3659 3660 3661 3662
			break;
		if (mr1 & MAC_INIT_COMPLETE &&
		    priv->bus_type == BUS_TYPE_PCCARD)
			break;
	}
C
Carlo Perassi 已提交
3663

L
Linus Torvalds 已提交
3664
	if (i == 0) {
C
Carlo Perassi 已提交
3665 3666
		printk(KERN_ALERT "%s: MAC failed to initialise.\n",
				priv->dev->name);
3667
		return -EIO;
L
Linus Torvalds 已提交
3668
	}
C
Carlo Perassi 已提交
3669

L
Linus Torvalds 已提交
3670 3671 3672 3673
	/* Check for MAC_INIT_OK only on the register that the MAC_INIT_OK was set */
	if ((mr3 & MAC_INIT_COMPLETE) &&
	    !(atmel_read16(priv->dev, MR3) & MAC_INIT_OK)) {
		printk(KERN_ALERT "%s: MAC failed MR3 self-test.\n", priv->dev->name);
3674
		return -EIO;
L
Linus Torvalds 已提交
3675 3676 3677 3678
	}
	if ((mr1 & MAC_INIT_COMPLETE) &&
	    !(atmel_read16(priv->dev, MR1) & MAC_INIT_OK)) {
		printk(KERN_ALERT "%s: MAC failed MR1 self-test.\n", priv->dev->name);
3679
		return -EIO;
L
Linus Torvalds 已提交
3680 3681
	}

C
Carlo Perassi 已提交
3682
	atmel_copy_to_host(priv->dev, (unsigned char *)iface,
L
Linus Torvalds 已提交
3683
			   priv->host_info_base, sizeof(*iface));
C
Carlo Perassi 已提交
3684

L
Linus Torvalds 已提交
3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699
	iface->tx_buff_pos = le16_to_cpu(iface->tx_buff_pos);
	iface->tx_buff_size = le16_to_cpu(iface->tx_buff_size);
	iface->tx_desc_pos = le16_to_cpu(iface->tx_desc_pos);
	iface->tx_desc_count = le16_to_cpu(iface->tx_desc_count);
	iface->rx_buff_pos = le16_to_cpu(iface->rx_buff_pos);
	iface->rx_buff_size = le16_to_cpu(iface->rx_buff_size);
	iface->rx_desc_pos = le16_to_cpu(iface->rx_desc_pos);
	iface->rx_desc_count = le16_to_cpu(iface->rx_desc_count);
	iface->build_version = le16_to_cpu(iface->build_version);
	iface->command_pos = le16_to_cpu(iface->command_pos);
	iface->major_version = le16_to_cpu(iface->major_version);
	iface->minor_version = le16_to_cpu(iface->minor_version);
	iface->func_ctrl = le16_to_cpu(iface->func_ctrl);
	iface->mac_status = le16_to_cpu(iface->mac_status);

3700
	return 0;
L
Linus Torvalds 已提交
3701 3702 3703 3704 3705 3706 3707
}

/* determine type of memory and MAC address */
static int probe_atmel_card(struct net_device *dev)
{
	int rc = 0;
	struct atmel_private *priv = netdev_priv(dev);
C
Carlo Perassi 已提交
3708

L
Linus Torvalds 已提交
3709
	/* reset pccard */
C
Carlo Perassi 已提交
3710
	if (priv->bus_type == BUS_TYPE_PCCARD)
L
Linus Torvalds 已提交
3711
		atmel_write16(dev, GCR, 0x0060);
C
Carlo Perassi 已提交
3712

L
Linus Torvalds 已提交
3713 3714
	atmel_write16(dev, GCR, 0x0040);
	mdelay(500);
C
Carlo Perassi 已提交
3715

L
Linus Torvalds 已提交
3716
	if (atmel_read16(dev, MR2) == 0) {
C
Carlo Perassi 已提交
3717
		/* No stored firmware so load a small stub which just
L
Linus Torvalds 已提交
3718 3719 3720 3721 3722 3723 3724 3725
		   tells us the MAC address */
		int i;
		priv->card_type = CARD_TYPE_EEPROM;
		atmel_write16(dev, BSR, BSS_IRAM);
		atmel_copy_to_card(dev, 0, mac_reader, sizeof(mac_reader));
		atmel_set_gcr(dev, GCR_REMAP);
		atmel_clear_gcr(priv->dev, 0x0040);
		atmel_write16(dev, BSR, BSS_SRAM);
C
Carlo Perassi 已提交
3726
		for (i = LOOP_RETRY_LIMIT; i; i--)
L
Linus Torvalds 已提交
3727 3728 3729 3730 3731 3732 3733 3734
			if (atmel_read16(dev, MR3) & MAC_BOOT_COMPLETE)
				break;
		if (i == 0) {
			printk(KERN_ALERT "%s: MAC failed to boot MAC address reader.\n", dev->name);
		} else {
			atmel_copy_to_host(dev, dev->dev_addr, atmel_read16(dev, MR2), 6);
			/* got address, now squash it again until the network
			   interface is opened */
C
Carlo Perassi 已提交
3735
			if (priv->bus_type == BUS_TYPE_PCCARD)
L
Linus Torvalds 已提交
3736 3737 3738 3739 3740 3741 3742
				atmel_write16(dev, GCR, 0x0060);
			atmel_write16(dev, GCR, 0x0040);
			rc = 1;
		}
	} else if (atmel_read16(dev, MR4) == 0) {
		/* Mac address easy in this case. */
		priv->card_type = CARD_TYPE_PARALLEL_FLASH;
C
Carlo Perassi 已提交
3743
		atmel_write16(dev,  BSR, 1);
L
Linus Torvalds 已提交
3744 3745 3746 3747 3748 3749 3750
		atmel_copy_to_host(dev, dev->dev_addr, 0xc000, 6);
		atmel_write16(dev,  BSR, 0x200);
		rc = 1;
	} else {
		/* Standard firmware in flash, boot it up and ask
		   for the Mac Address */
		priv->card_type = CARD_TYPE_SPI_FLASH;
3751
		if (atmel_wakeup_firmware(priv) == 0) {
L
Linus Torvalds 已提交
3752
			atmel_get_mib(priv, Mac_Address_Mib_Type, 0, dev->dev_addr, 6);
C
Carlo Perassi 已提交
3753

L
Linus Torvalds 已提交
3754 3755
			/* got address, now squash it again until the network
			   interface is opened */
C
Carlo Perassi 已提交
3756
			if (priv->bus_type == BUS_TYPE_PCCARD)
L
Linus Torvalds 已提交
3757 3758 3759 3760 3761
				atmel_write16(dev, GCR, 0x0060);
			atmel_write16(dev, GCR, 0x0040);
			rc = 1;
		}
	}
C
Carlo Perassi 已提交
3762

L
Linus Torvalds 已提交
3763 3764
	if (rc) {
		if (dev->dev_addr[0] == 0xFF) {
J
Joe Perches 已提交
3765 3766 3767
			static const u8 default_mac[] = {
				0x00, 0x04, 0x25, 0x00, 0x00, 0x00
			};
L
Linus Torvalds 已提交
3768
			printk(KERN_ALERT "%s: *** Invalid MAC address. UPGRADE Firmware ****\n", dev->name);
3769
			memcpy(dev->dev_addr, default_mac, ETH_ALEN);
L
Linus Torvalds 已提交
3770 3771
		}
	}
C
Carlo Perassi 已提交
3772

L
Linus Torvalds 已提交
3773 3774 3775 3776 3777 3778
	return rc;
}

/* Move the encyption information on the MIB structure.
   This routine is for the pre-WPA firmware: later firmware has
   a different format MIB and a different routine. */
C
Carlo Perassi 已提交
3779
static void build_wep_mib(struct atmel_private *priv)
L
Linus Torvalds 已提交
3780 3781
{
	struct { /* NB this is matched to the hardware, don't change. */
C
Carlo Perassi 已提交
3782
		u8 wep_is_on;
L
Linus Torvalds 已提交
3783 3784 3785
		u8 default_key; /* 0..3 */
		u8 reserved;
		u8 exclude_unencrypted;
C
Carlo Perassi 已提交
3786

L
Linus Torvalds 已提交
3787 3788
		u32 WEPICV_error_count;
		u32 WEP_excluded_count;
C
Carlo Perassi 已提交
3789

L
Linus Torvalds 已提交
3790
		u8 wep_keys[MAX_ENCRYPTION_KEYS][13];
C
Carlo Perassi 已提交
3791 3792
		u8 encryption_level; /* 0, 1, 2 */
		u8 reserved2[3];
L
Linus Torvalds 已提交
3793 3794 3795 3796 3797 3798 3799 3800
	} mib;
	int i;

	mib.wep_is_on = priv->wep_is_on;
	if (priv->wep_is_on) {
		if (priv->wep_key_len[priv->default_key] > 5)
			mib.encryption_level = 2;
		else
C
Carlo Perassi 已提交
3801
			mib.encryption_level = 1;
L
Linus Torvalds 已提交
3802 3803 3804 3805 3806 3807
	} else {
		mib.encryption_level = 0;
	}

	mib.default_key = priv->default_key;
	mib.exclude_unencrypted = priv->exclude_unencrypted;
C
Carlo Perassi 已提交
3808 3809

	for (i = 0; i < MAX_ENCRYPTION_KEYS; i++)
L
Linus Torvalds 已提交
3810
		memcpy(mib.wep_keys[i], priv->wep_keys[i], 13);
C
Carlo Perassi 已提交
3811

L
Linus Torvalds 已提交
3812 3813 3814 3815 3816
	atmel_set_mib(priv, Mac_Wep_Mib_Type, 0, (u8 *)&mib, sizeof(mib));
}

static void build_wpa_mib(struct atmel_private *priv)
{
C
Carlo Perassi 已提交
3817
	/* This is for the later (WPA enabled) firmware. */
L
Linus Torvalds 已提交
3818 3819 3820

	struct { /* NB this is matched to the hardware, don't change. */
		u8 cipher_default_key_value[MAX_ENCRYPTION_KEYS][MAX_ENCRYPTION_KEY_SIZE];
3821
		u8 receiver_address[ETH_ALEN];
C
Carlo Perassi 已提交
3822
		u8 wep_is_on;
L
Linus Torvalds 已提交
3823 3824 3825 3826 3827
		u8 default_key; /* 0..3 */
		u8 group_key;
		u8 exclude_unencrypted;
		u8 encryption_type;
		u8 reserved;
C
Carlo Perassi 已提交
3828

L
Linus Torvalds 已提交
3829 3830
		u32 WEPICV_error_count;
		u32 WEP_excluded_count;
C
Carlo Perassi 已提交
3831

L
Linus Torvalds 已提交
3832 3833
		u8 key_RSC[4][8];
	} mib;
C
Carlo Perassi 已提交
3834

L
Linus Torvalds 已提交
3835 3836 3837 3838
	int i;

	mib.wep_is_on = priv->wep_is_on;
	mib.exclude_unencrypted = priv->exclude_unencrypted;
3839
	memcpy(mib.receiver_address, priv->CurrentBSSID, ETH_ALEN);
C
Carlo Perassi 已提交
3840

L
Linus Torvalds 已提交
3841 3842
	/* zero all the keys before adding in valid ones. */
	memset(mib.cipher_default_key_value, 0, sizeof(mib.cipher_default_key_value));
C
Carlo Perassi 已提交
3843

L
Linus Torvalds 已提交
3844
	if (priv->wep_is_on) {
C
Carlo Perassi 已提交
3845 3846 3847
		/* There's a comment in the Atmel code to the effect that this
		   is only valid when still using WEP, it may need to be set to
		   something to use WPA */
L
Linus Torvalds 已提交
3848
		memset(mib.key_RSC, 0, sizeof(mib.key_RSC));
C
Carlo Perassi 已提交
3849

L
Linus Torvalds 已提交
3850 3851 3852 3853 3854 3855 3856
		mib.default_key = mib.group_key = 255;
		for (i = 0; i < MAX_ENCRYPTION_KEYS; i++) {
			if (priv->wep_key_len[i] > 0) {
				memcpy(mib.cipher_default_key_value[i], priv->wep_keys[i], MAX_ENCRYPTION_KEY_SIZE);
				if (i == priv->default_key) {
					mib.default_key = i;
					mib.cipher_default_key_value[i][MAX_ENCRYPTION_KEY_SIZE-1] = 7;
C
Carlo Perassi 已提交
3857
					mib.cipher_default_key_value[i][MAX_ENCRYPTION_KEY_SIZE-2] = priv->pairwise_cipher_suite;
L
Linus Torvalds 已提交
3858 3859 3860
				} else {
					mib.group_key = i;
					priv->group_cipher_suite = priv->pairwise_cipher_suite;
J
John Daiker 已提交
3861
					mib.cipher_default_key_value[i][MAX_ENCRYPTION_KEY_SIZE-1] = 1;
C
Carlo Perassi 已提交
3862
					mib.cipher_default_key_value[i][MAX_ENCRYPTION_KEY_SIZE-2] = priv->group_cipher_suite;
L
Linus Torvalds 已提交
3863 3864 3865 3866 3867 3868 3869
				}
			}
		}
		if (mib.default_key == 255)
			mib.default_key = mib.group_key != 255 ? mib.group_key : 0;
		if (mib.group_key == 255)
			mib.group_key = mib.default_key;
C
Carlo Perassi 已提交
3870

L
Linus Torvalds 已提交
3871
	}
C
Carlo Perassi 已提交
3872

L
Linus Torvalds 已提交
3873 3874
	atmel_set_mib(priv, Mac_Wep_Mib_Type, 0, (u8 *)&mib, sizeof(mib));
}
C
Carlo Perassi 已提交
3875 3876

static int reset_atmel_card(struct net_device *dev)
L
Linus Torvalds 已提交
3877 3878 3879 3880
{
	/* do everything necessary to wake up the hardware, including
	   waiting for the lightning strike and throwing the knife switch....

C
Carlo Perassi 已提交
3881
	   set all the Mib values which matter in the card to match
L
Linus Torvalds 已提交
3882 3883 3884 3885 3886
	   their settings in the atmel_private structure. Some of these
	   can be altered on the fly, but many (WEP, infrastucture or ad-hoc)
	   can only be changed by tearing down the world and coming back through
	   here.

C
Carlo Perassi 已提交
3887 3888
	   This routine is also responsible for initialising some
	   hardware-specific fields in the atmel_private structure,
L
Lucas De Marchi 已提交
3889
	   including a copy of the firmware's hostinfo structure
3890
	   which is the route into the rest of the firmware datastructures. */
L
Linus Torvalds 已提交
3891 3892 3893

	struct atmel_private *priv = netdev_priv(dev);
	u8 configuration;
3894
	int old_state = priv->station_state;
3895
	int err = 0;
C
Carlo Perassi 已提交
3896

L
Linus Torvalds 已提交
3897 3898
	/* data to add to the firmware names, in priority order
	   this implemenents firmware versioning */
C
Carlo Perassi 已提交
3899

L
Linus Torvalds 已提交
3900 3901 3902 3903 3904
	static char *firmware_modifier[] = {
		"-wpa",
		"",
		NULL
	};
C
Carlo Perassi 已提交
3905

L
Linus Torvalds 已提交
3906
	/* reset pccard */
C
Carlo Perassi 已提交
3907
	if (priv->bus_type == BUS_TYPE_PCCARD)
L
Linus Torvalds 已提交
3908
		atmel_write16(priv->dev, GCR, 0x0060);
C
Carlo Perassi 已提交
3909

L
Linus Torvalds 已提交
3910 3911
	/* stop card , disable interrupts */
	atmel_write16(priv->dev, GCR, 0x0040);
C
Carlo Perassi 已提交
3912

L
Linus Torvalds 已提交
3913 3914 3915
	if (priv->card_type == CARD_TYPE_EEPROM) {
		/* copy in firmware if needed */
		const struct firmware *fw_entry = NULL;
3916
		const unsigned char *fw;
L
Linus Torvalds 已提交
3917 3918 3919 3920 3921 3922 3923 3924
		int len = priv->firmware_length;
		if (!(fw = priv->firmware)) {
			if (priv->firmware_type == ATMEL_FW_TYPE_NONE) {
				if (strlen(priv->firmware_id) == 0) {
					printk(KERN_INFO
					       "%s: card type is unknown: assuming at76c502 firmware is OK.\n",
					       dev->name);
					printk(KERN_INFO
C
Carlo Perassi 已提交
3925
					       "%s: if not, use the firmware= module parameter.\n",
L
Linus Torvalds 已提交
3926 3927 3928
					       dev->name);
					strcpy(priv->firmware_id, "atmel_at76c502.bin");
				}
3929 3930
				err = request_firmware(&fw_entry, priv->firmware_id, priv->sys_dev);
				if (err != 0) {
C
Carlo Perassi 已提交
3931 3932
					printk(KERN_ALERT
					       "%s: firmware %s is missing, cannot continue.\n",
L
Linus Torvalds 已提交
3933
					       dev->name, priv->firmware_id);
3934
					return err;
L
Linus Torvalds 已提交
3935 3936 3937 3938 3939 3940 3941 3942 3943
				}
			} else {
				int fw_index = 0;
				int success = 0;

				/* get firmware filename entry based on firmware type ID */
				while (fw_table[fw_index].fw_type != priv->firmware_type
						&& fw_table[fw_index].fw_type != ATMEL_FW_TYPE_NONE)
					fw_index++;
C
Carlo Perassi 已提交
3944

L
Linus Torvalds 已提交
3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958
				/* construct the actual firmware file name */
				if (fw_table[fw_index].fw_type != ATMEL_FW_TYPE_NONE) {
					int i;
					for (i = 0; firmware_modifier[i]; i++) {
						snprintf(priv->firmware_id, 32, "%s%s.%s", fw_table[fw_index].fw_file,
							firmware_modifier[i], fw_table[fw_index].fw_file_ext);
						priv->firmware_id[31] = '\0';
						if (request_firmware(&fw_entry, priv->firmware_id, priv->sys_dev) == 0) {
							success = 1;
							break;
						}
					}
				}
				if (!success) {
C
Carlo Perassi 已提交
3959 3960
					printk(KERN_ALERT
					       "%s: firmware %s is missing, cannot start.\n",
L
Linus Torvalds 已提交
3961 3962
					       dev->name, priv->firmware_id);
					priv->firmware_id[0] = '\0';
3963
					return -ENOENT;
L
Linus Torvalds 已提交
3964 3965
				}
			}
C
Carlo Perassi 已提交
3966

L
Linus Torvalds 已提交
3967 3968 3969
			fw = fw_entry->data;
			len = fw_entry->size;
		}
C
Carlo Perassi 已提交
3970

J
John Daiker 已提交
3971
		if (len <= 0x6000) {
L
Linus Torvalds 已提交
3972 3973 3974 3975
			atmel_write16(priv->dev, BSR, BSS_IRAM);
			atmel_copy_to_card(priv->dev, 0, fw, len);
			atmel_set_gcr(priv->dev, GCR_REMAP);
		} else {
C
Carlo Perassi 已提交
3976
			/* Remap */
L
Linus Torvalds 已提交
3977 3978 3979 3980 3981 3982 3983
			atmel_set_gcr(priv->dev, GCR_REMAP);
			atmel_write16(priv->dev, BSR, BSS_IRAM);
			atmel_copy_to_card(priv->dev, 0, fw, 0x6000);
			atmel_write16(priv->dev, BSR, 0x2ff);
			atmel_copy_to_card(priv->dev, 0x8000, &fw[0x6000], len - 0x6000);
		}

3984
		release_firmware(fw_entry);
L
Linus Torvalds 已提交
3985 3986
	}

3987 3988 3989
	err = atmel_wakeup_firmware(priv);
	if (err != 0)
		return err;
L
Linus Torvalds 已提交
3990 3991 3992 3993 3994 3995 3996 3997

	/* Check the version and set the correct flag for wpa stuff,
	   old and new firmware is incompatible.
	   The pre-wpa 3com firmware reports major version 5,
	   the wpa 3com firmware is major version 4 and doesn't need
	   the 3com broken-ness filter. */
	priv->use_wpa = (priv->host_info.major_version == 4);
	priv->radio_on_broken = (priv->host_info.major_version == 5);
C
Carlo Perassi 已提交
3998

J
John Daiker 已提交
3999
	/* unmask all irq sources */
L
Linus Torvalds 已提交
4000
	atmel_wmem8(priv, atmel_hi(priv, IFACE_INT_MASK_OFFSET), 0xff);
C
Carlo Perassi 已提交
4001

L
Linus Torvalds 已提交
4002 4003 4004 4005 4006 4007
	/* int Tx system and enable Tx */
	atmel_wmem8(priv, atmel_tx(priv, TX_DESC_FLAGS_OFFSET, 0), 0);
	atmel_wmem32(priv, atmel_tx(priv, TX_DESC_NEXT_OFFSET, 0), 0x80000000L);
	atmel_wmem16(priv, atmel_tx(priv, TX_DESC_POS_OFFSET, 0), 0);
	atmel_wmem16(priv, atmel_tx(priv, TX_DESC_SIZE_OFFSET, 0), 0);

C
Carlo Perassi 已提交
4008 4009 4010
	priv->tx_desc_free = priv->host_info.tx_desc_count;
	priv->tx_desc_head = 0;
	priv->tx_desc_tail = 0;
L
Linus Torvalds 已提交
4011 4012
	priv->tx_desc_previous = 0;
	priv->tx_free_mem = priv->host_info.tx_buff_size;
C
Carlo Perassi 已提交
4013 4014 4015 4016 4017
	priv->tx_buff_head = 0;
	priv->tx_buff_tail = 0;

	configuration = atmel_rmem8(priv, atmel_hi(priv, IFACE_FUNC_CTRL_OFFSET));
	atmel_wmem8(priv, atmel_hi(priv, IFACE_FUNC_CTRL_OFFSET),
L
Linus Torvalds 已提交
4018 4019 4020 4021
				   configuration | FUNC_CTRL_TxENABLE);

	/* init Rx system and enable */
	priv->rx_desc_head = 0;
C
Carlo Perassi 已提交
4022 4023 4024

	configuration = atmel_rmem8(priv, atmel_hi(priv, IFACE_FUNC_CTRL_OFFSET));
	atmel_wmem8(priv, atmel_hi(priv, IFACE_FUNC_CTRL_OFFSET),
L
Linus Torvalds 已提交
4025
				   configuration | FUNC_CTRL_RxENABLE);
C
Carlo Perassi 已提交
4026

L
Linus Torvalds 已提交
4027
	if (!priv->radio_on_broken) {
C
Carlo Perassi 已提交
4028
		if (atmel_send_command_wait(priv, CMD_EnableRadio, NULL, 0) ==
L
Linus Torvalds 已提交
4029
		    CMD_STATUS_REJECTED_RADIO_OFF) {
4030
			printk(KERN_INFO "%s: cannot turn the radio on.\n",
L
Linus Torvalds 已提交
4031
			       dev->name);
J
John Daiker 已提交
4032
			return -EIO;
L
Linus Torvalds 已提交
4033 4034
		}
	}
C
Carlo Perassi 已提交
4035

L
Linus Torvalds 已提交
4036 4037 4038 4039 4040 4041 4042 4043
	/* set up enough MIB values to run. */
	atmel_set_mib8(priv, Local_Mib_Type, LOCAL_MIB_AUTO_TX_RATE_POS, priv->auto_tx_rate);
	atmel_set_mib8(priv, Local_Mib_Type,  LOCAL_MIB_TX_PROMISCUOUS_POS,  PROM_MODE_OFF);
	atmel_set_mib16(priv, Mac_Mib_Type, MAC_MIB_RTS_THRESHOLD_POS, priv->rts_threshold);
	atmel_set_mib16(priv, Mac_Mib_Type, MAC_MIB_FRAG_THRESHOLD_POS, priv->frag_threshold);
	atmel_set_mib8(priv, Mac_Mib_Type, MAC_MIB_SHORT_RETRY_POS, priv->short_retry);
	atmel_set_mib8(priv, Mac_Mib_Type, MAC_MIB_LONG_RETRY_POS, priv->long_retry);
	atmel_set_mib8(priv, Local_Mib_Type, LOCAL_MIB_PREAMBLE_TYPE, priv->preamble);
C
Carlo Perassi 已提交
4044
	atmel_set_mib(priv, Mac_Address_Mib_Type, MAC_ADDR_MIB_MAC_ADDR_POS,
L
Linus Torvalds 已提交
4045 4046 4047 4048 4049 4050 4051 4052 4053 4054
		      priv->dev->dev_addr, 6);
	atmel_set_mib8(priv, Mac_Mgmt_Mib_Type, MAC_MGMT_MIB_PS_MODE_POS, ACTIVE_MODE);
	atmel_set_mib16(priv, Mac_Mgmt_Mib_Type, MAC_MGMT_MIB_LISTEN_INTERVAL_POS, 1);
	atmel_set_mib16(priv, Mac_Mgmt_Mib_Type, MAC_MGMT_MIB_BEACON_PER_POS, priv->default_beacon_period);
	atmel_set_mib(priv, Phy_Mib_Type, PHY_MIB_RATE_SET_POS, atmel_basic_rates, 4);
	atmel_set_mib8(priv, Mac_Mgmt_Mib_Type, MAC_MGMT_MIB_CUR_PRIVACY_POS, priv->wep_is_on);
	if (priv->use_wpa)
		build_wpa_mib(priv);
	else
		build_wep_mib(priv);
C
Carlo Perassi 已提交
4055

J
John Daiker 已提交
4056
	if (old_state == STATION_STATE_READY) {
4057 4058 4059 4060 4061 4062 4063 4064 4065
		union iwreq_data wrqu;

		wrqu.data.length = 0;
		wrqu.data.flags = 0;
		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
		memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
		wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
	}

4066
	return 0;
L
Linus Torvalds 已提交
4067 4068
}

C
Carlo Perassi 已提交
4069 4070
static void atmel_send_command(struct atmel_private *priv, int command,
			       void *cmd, int cmd_size)
L
Linus Torvalds 已提交
4071 4072
{
	if (cmd)
C
Carlo Perassi 已提交
4073
		atmel_copy_to_card(priv->dev, atmel_co(priv, CMD_BLOCK_PARAMETERS_OFFSET),
L
Linus Torvalds 已提交
4074
				   cmd, cmd_size);
C
Carlo Perassi 已提交
4075

L
Linus Torvalds 已提交
4076 4077 4078
	atmel_wmem8(priv, atmel_co(priv, CMD_BLOCK_COMMAND_OFFSET), command);
	atmel_wmem8(priv, atmel_co(priv, CMD_BLOCK_STATUS_OFFSET), 0);
}
C
Carlo Perassi 已提交
4079 4080 4081

static int atmel_send_command_wait(struct atmel_private *priv, int command,
				   void *cmd, int cmd_size)
L
Linus Torvalds 已提交
4082 4083
{
	int i, status;
C
Carlo Perassi 已提交
4084

L
Linus Torvalds 已提交
4085
	atmel_send_command(priv, command, cmd, cmd_size);
C
Carlo Perassi 已提交
4086

L
Linus Torvalds 已提交
4087 4088
	for (i = 5000; i; i--) {
		status = atmel_rmem8(priv, atmel_co(priv, CMD_BLOCK_STATUS_OFFSET));
C
Carlo Perassi 已提交
4089
		if (status != CMD_STATUS_IDLE &&
L
Linus Torvalds 已提交
4090 4091 4092 4093
		    status != CMD_STATUS_IN_PROGRESS)
			break;
		udelay(20);
	}
C
Carlo Perassi 已提交
4094

L
Linus Torvalds 已提交
4095 4096 4097
	if (i == 0) {
		printk(KERN_ALERT "%s: failed to contact MAC.\n", priv->dev->name);
		status =  CMD_STATUS_HOST_ERROR;
C
Carlo Perassi 已提交
4098
	} else {
L
Linus Torvalds 已提交
4099 4100 4101
		if (command != CMD_EnableRadio)
			status = CMD_STATUS_COMPLETE;
	}
C
Carlo Perassi 已提交
4102

L
Linus Torvalds 已提交
4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127
	return status;
}

static u8 atmel_get_mib8(struct atmel_private *priv, u8 type, u8 index)
{
	struct get_set_mib m;
	m.type = type;
	m.size = 1;
	m.index = index;

	atmel_send_command_wait(priv, CMD_Get_MIB_Vars, &m, MIB_HEADER_SIZE + 1);
	return atmel_rmem8(priv, atmel_co(priv, CMD_BLOCK_PARAMETERS_OFFSET + MIB_HEADER_SIZE));
}

static void atmel_set_mib8(struct atmel_private *priv, u8 type, u8 index, u8 data)
{
	struct get_set_mib m;
	m.type = type;
	m.size = 1;
	m.index = index;
	m.data[0] = data;

	atmel_send_command_wait(priv, CMD_Set_MIB_Vars, &m, MIB_HEADER_SIZE + 1);
}

C
Carlo Perassi 已提交
4128 4129
static void atmel_set_mib16(struct atmel_private *priv, u8 type, u8 index,
			    u16 data)
L
Linus Torvalds 已提交
4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140
{
	struct get_set_mib m;
	m.type = type;
	m.size = 2;
	m.index = index;
	m.data[0] = data;
	m.data[1] = data >> 8;

	atmel_send_command_wait(priv, CMD_Set_MIB_Vars, &m, MIB_HEADER_SIZE + 2);
}

C
Carlo Perassi 已提交
4141 4142
static void atmel_set_mib(struct atmel_private *priv, u8 type, u8 index,
			  u8 *data, int data_len)
L
Linus Torvalds 已提交
4143 4144 4145 4146 4147 4148 4149 4150
{
	struct get_set_mib m;
	m.type = type;
	m.size = data_len;
	m.index = index;

	if (data_len > MIB_MAX_DATA_BYTES)
		printk(KERN_ALERT "%s: MIB buffer too small.\n", priv->dev->name);
C
Carlo Perassi 已提交
4151

L
Linus Torvalds 已提交
4152 4153 4154 4155
	memcpy(m.data, data, data_len);
	atmel_send_command_wait(priv, CMD_Set_MIB_Vars, &m, MIB_HEADER_SIZE + data_len);
}

C
Carlo Perassi 已提交
4156 4157
static void atmel_get_mib(struct atmel_private *priv, u8 type, u8 index,
			  u8 *data, int data_len)
L
Linus Torvalds 已提交
4158 4159 4160 4161 4162
{
	struct get_set_mib m;
	m.type = type;
	m.size = data_len;
	m.index = index;
C
Carlo Perassi 已提交
4163

L
Linus Torvalds 已提交
4164 4165
	if (data_len > MIB_MAX_DATA_BYTES)
		printk(KERN_ALERT "%s: MIB buffer too small.\n", priv->dev->name);
C
Carlo Perassi 已提交
4166

L
Linus Torvalds 已提交
4167
	atmel_send_command_wait(priv, CMD_Get_MIB_Vars, &m, MIB_HEADER_SIZE + data_len);
C
Carlo Perassi 已提交
4168
	atmel_copy_to_host(priv->dev, data,
L
Linus Torvalds 已提交
4169 4170 4171 4172 4173 4174 4175 4176
			   atmel_co(priv, CMD_BLOCK_PARAMETERS_OFFSET + MIB_HEADER_SIZE), data_len);
}

static void atmel_writeAR(struct net_device *dev, u16 data)
{
	int i;
	outw(data, dev->base_addr + AR);
	/* Address register appears to need some convincing..... */
C
Carlo Perassi 已提交
4177
	for (i = 0; data != inw(dev->base_addr + AR) && i < 10; i++)
L
Linus Torvalds 已提交
4178 4179 4180
		outw(data, dev->base_addr + AR);
}

C
Carlo Perassi 已提交
4181
static void atmel_copy_to_card(struct net_device *dev, u16 dest,
4182
			       const unsigned char *src, u16 len)
L
Linus Torvalds 已提交
4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198
{
	int i;
	atmel_writeAR(dev, dest);
	if (dest % 2) {
		atmel_write8(dev, DR, *src);
		src++; len--;
	}
	for (i = len; i > 1 ; i -= 2) {
		u8 lb = *src++;
		u8 hb = *src++;
		atmel_write16(dev, DR, lb | (hb << 8));
	}
	if (i)
		atmel_write8(dev, DR, *src);
}

C
Carlo Perassi 已提交
4199 4200
static void atmel_copy_to_host(struct net_device *dev, unsigned char *dest,
			       u16 src, u16 len)
L
Linus Torvalds 已提交
4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235
{
	int i;
	atmel_writeAR(dev, src);
	if (src % 2) {
		*dest = atmel_read8(dev, DR);
		dest++; len--;
	}
	for (i = len; i > 1 ; i -= 2) {
		u16 hw = atmel_read16(dev, DR);
		*dest++ = hw;
		*dest++ = hw >> 8;
	}
	if (i)
		*dest = atmel_read8(dev, DR);
}

static void atmel_set_gcr(struct net_device *dev, u16 mask)
{
	outw(inw(dev->base_addr + GCR) | mask, dev->base_addr + GCR);
}

static void atmel_clear_gcr(struct net_device *dev, u16 mask)
{
	outw(inw(dev->base_addr + GCR) & ~mask, dev->base_addr + GCR);
}

static int atmel_lock_mac(struct atmel_private *priv)
{
	int i, j = 20;
 retry:
	for (i = 5000; i; i--) {
		if (!atmel_rmem8(priv, atmel_hi(priv, IFACE_LOCKOUT_HOST_OFFSET)))
			break;
		udelay(20);
	}
C
Carlo Perassi 已提交
4236 4237 4238 4239

	if (!i)
		return 0; /* timed out */

L
Linus Torvalds 已提交
4240 4241 4242
	atmel_wmem8(priv, atmel_hi(priv, IFACE_LOCKOUT_MAC_OFFSET), 1);
	if (atmel_rmem8(priv, atmel_hi(priv, IFACE_LOCKOUT_HOST_OFFSET))) {
		atmel_wmem8(priv, atmel_hi(priv, IFACE_LOCKOUT_MAC_OFFSET), 0);
C
Carlo Perassi 已提交
4243 4244
		if (!j--)
			return 0; /* timed out */
L
Linus Torvalds 已提交
4245 4246
		goto retry;
	}
C
Carlo Perassi 已提交
4247

L
Linus Torvalds 已提交
4248 4249 4250 4251 4252
	return 1;
}

static void atmel_wmem32(struct atmel_private *priv, u16 pos, u32 data)
{
C
Carlo Perassi 已提交
4253
	atmel_writeAR(priv->dev, pos);
L
Linus Torvalds 已提交
4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279
	atmel_write16(priv->dev, DR, data); /* card is little-endian */
	atmel_write16(priv->dev, DR, data >> 16);
}

/***************************************************************************/
/* There follows the source form of the MAC address reading firmware       */
/***************************************************************************/
#if 0

/* Copyright 2003 Matthew T. Russotto                                      */
/* But derived from the Atmel 76C502 firmware written by Atmel and         */
/* included in "atmel wireless lan drivers" package                        */
/**
    This file is part of net.russotto.AtmelMACFW, hereto referred to
    as AtmelMACFW

    AtmelMACFW is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2
    as published by the Free Software Foundation.

    AtmelMACFW 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.

    You should have received a copy of the GNU General Public License
4280
    along with AtmelMACFW; if not, see <http://www.gnu.org/licenses/>.
L
Linus Torvalds 已提交
4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323

****************************************************************************/
/* This firmware should work on the 76C502 RFMD, RFMD_D, and RFMD_E        */
/* It will probably work on the 76C504 and 76C502 RFMD_3COM                */
/* It only works on SPI EEPROM versions of the card.                       */

/* This firmware initializes the SPI controller and clock, reads the MAC   */
/* address from the EEPROM into SRAM, and puts the SRAM offset of the MAC  */
/* address in MR2, and sets MR3 to 0x10 to indicate it is done             */
/* It also puts a complete copy of the EEPROM in SRAM with the offset in   */
/* MR4, for investigational purposes (maybe we can determine chip type     */
/* from that?)                                                             */

	.org 0
    .set MRBASE, 0x8000000
	.set CPSR_INITIAL, 0xD3 /* IRQ/FIQ disabled, ARM mode, Supervisor state */
	.set CPSR_USER, 0xD1 /* IRQ/FIQ disabled, ARM mode, USER state */
	.set SRAM_BASE,  0x02000000
	.set SP_BASE,    0x0F300000
	.set UNK_BASE,   0x0F000000 /* Some internal device, but which one? */
	.set SPI_CGEN_BASE,  0x0E000000 /* Some internal device, but which one? */
	.set UNK3_BASE,  0x02014000 /* Some internal device, but which one? */
	.set STACK_BASE, 0x5600
	.set SP_SR, 0x10
	.set SP_TDRE, 2 /* status register bit -- TDR empty */
	.set SP_RDRF, 1 /* status register bit -- RDR full */
	.set SP_SWRST, 0x80
	.set SP_SPIEN, 0x1
	.set SP_CR, 0   /* control register */
	.set SP_MR, 4   /* mode register */
	.set SP_RDR, 0x08 /* Read Data Register */
	.set SP_TDR, 0x0C /* Transmit Data Register */
	.set SP_CSR0, 0x30 /* chip select registers */
	.set SP_CSR1, 0x34
	.set SP_CSR2, 0x38
	.set SP_CSR3, 0x3C
	.set NVRAM_CMD_RDSR, 5 /* read status register */
	.set NVRAM_CMD_READ, 3 /* read data */
	.set NVRAM_SR_RDY, 1 /* RDY bit.  This bit is inverted */
	.set SPI_8CLOCKS, 0xFF /* Writing this to the TDR doesn't do anything to the
				  serial output, since SO is normally high.  But it
				  does cause 8 clock cycles and thus 8 bits to be
				  clocked in to the chip.  See Atmel's SPI
C
Carlo Perassi 已提交
4324
				  controller (e.g. AT91M55800) timing and 4K
L
Linus Torvalds 已提交
4325
				  SPI EEPROM manuals */
C
Carlo Perassi 已提交
4326

L
Linus Torvalds 已提交
4327 4328 4329 4330 4331
	.set NVRAM_SCRATCH, 0x02000100  /* arbitrary area for scratchpad memory */
	.set NVRAM_IMAGE, 0x02000200
	.set NVRAM_LENGTH, 0x0200
	.set MAC_ADDRESS_MIB, SRAM_BASE
	.set MAC_ADDRESS_LENGTH, 6
J
John Daiker 已提交
4332
	.set MAC_BOOT_FLAG, 0x10
L
Linus Torvalds 已提交
4333 4334 4335 4336 4337
	.set MR1, 0
	.set MR2, 4
	.set MR3, 8
	.set MR4, 0xC
RESET_VECTOR:
J
John Daiker 已提交
4338
	b RESET_HANDLER
C
Carlo Perassi 已提交
4339
UNDEF_VECTOR:
J
John Daiker 已提交
4340
	b HALT1
C
Carlo Perassi 已提交
4341
SWI_VECTOR:
J
John Daiker 已提交
4342
	b HALT1
C
Carlo Perassi 已提交
4343
IABORT_VECTOR:
J
John Daiker 已提交
4344
	b HALT1
C
Carlo Perassi 已提交
4345 4346
DABORT_VECTOR:
RESERVED_VECTOR:
J
John Daiker 已提交
4347
	b HALT1
C
Carlo Perassi 已提交
4348
IRQ_VECTOR:
J
John Daiker 已提交
4349
	b HALT1
C
Carlo Perassi 已提交
4350
FIQ_VECTOR:
L
Linus Torvalds 已提交
4351 4352 4353 4354 4355
	b HALT1
HALT1:	b HALT1
RESET_HANDLER:
	mov     r0, #CPSR_INITIAL
	msr	CPSR_c, r0	/* This is probably unnecessary */
C
Carlo Perassi 已提交
4356

L
Linus Torvalds 已提交
4357 4358 4359 4360
/* I'm guessing this is initializing clock generator electronics for SPI */
	ldr	r0, =SPI_CGEN_BASE
	mov	r1, #0
	mov	r1, r1, lsl #3
J
John Daiker 已提交
4361
	orr	r1, r1, #0
L
Linus Torvalds 已提交
4362 4363 4364 4365 4366 4367
	str	r1, [r0]
	ldr	r1, [r0, #28]
	bic	r1, r1, #16
	str	r1, [r0, #28]
	mov	r1, #1
	str	r1, [r0, #8]
C
Carlo Perassi 已提交
4368

L
Linus Torvalds 已提交
4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400
	ldr	r0, =MRBASE
	mov	r1, #0
	strh	r1, [r0, #MR1]
	strh	r1, [r0, #MR2]
	strh	r1, [r0, #MR3]
	strh	r1, [r0, #MR4]

	mov	sp, #STACK_BASE
	bl	SP_INIT
	mov	r0, #10
	bl	DELAY9
	bl	GET_MAC_ADDR
	bl	GET_WHOLE_NVRAM
	ldr	r0, =MRBASE
	ldr	r1, =MAC_ADDRESS_MIB
	strh	r1, [r0, #MR2]
	ldr	r1, =NVRAM_IMAGE
	strh	r1, [r0, #MR4]
	mov	r1, #MAC_BOOT_FLAG
	strh	r1, [r0, #MR3]
HALT2:	b HALT2
.func Get_Whole_NVRAM, GET_WHOLE_NVRAM
GET_WHOLE_NVRAM:
	stmdb	sp!, {lr}
	mov	r2, #0 /* 0th bytes of NVRAM */
	mov	r3, #NVRAM_LENGTH
	mov	r1, #0		/* not used in routine */
	ldr	r0, =NVRAM_IMAGE
	bl	NVRAM_XFER
	ldmia	sp!, {lr}
	bx	lr
.endfunc
C
Carlo Perassi 已提交
4401

L
Linus Torvalds 已提交
4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416
.func Get_MAC_Addr, GET_MAC_ADDR
GET_MAC_ADDR:
	stmdb	sp!, {lr}
	mov	r2, #0x120	/* address of MAC Address within NVRAM */
	mov	r3, #MAC_ADDRESS_LENGTH
	mov	r1, #0		/* not used in routine */
	ldr	r0, =MAC_ADDRESS_MIB
	bl	NVRAM_XFER
	ldmia	sp!, {lr}
	bx	lr
.endfunc
.ltorg
.func Delay9, DELAY9
DELAY9:
	adds	r0, r0, r0, LSL #3   /* r0 = r0 * 9 */
C
Carlo Perassi 已提交
4417
DELAYLOOP:
L
Linus Torvalds 已提交
4418 4419 4420
	beq	DELAY9_done
	subs	r0, r0, #1
	b	DELAYLOOP
C
Carlo Perassi 已提交
4421
DELAY9_done:
L
Linus Torvalds 已提交
4422
	bx	lr
C
Carlo Perassi 已提交
4423
.endfunc
L
Linus Torvalds 已提交
4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451

.func SP_Init, SP_INIT
SP_INIT:
	mov	r1, #SP_SWRST
	ldr	r0, =SP_BASE
	str	r1, [r0, #SP_CR] /* reset the SPI */
	mov	r1, #0
	str	r1, [r0, #SP_CR] /* release SPI from reset state */
	mov	r1, #SP_SPIEN
	str	r1, [r0, #SP_MR] /* set the SPI to MASTER mode*/
	str	r1, [r0, #SP_CR] /* enable the SPI */

/*  My guess would be this turns on the SPI clock */
	ldr	r3, =SPI_CGEN_BASE
	ldr	r1, [r3, #28]
	orr	r1, r1, #0x2000
	str	r1, [r3, #28]

	ldr	r1, =0x2000c01
	str	r1, [r0, #SP_CSR0]
	ldr	r1, =0x2000201
	str	r1, [r0, #SP_CSR1]
	str	r1, [r0, #SP_CSR2]
	str	r1, [r0, #SP_CSR3]
	ldr	r1, [r0, #SP_SR]
	ldr	r0, [r0, #SP_RDR]
	bx	lr
.endfunc
C
Carlo Perassi 已提交
4452
.func NVRAM_Init, NVRAM_INIT
L
Linus Torvalds 已提交
4453 4454 4455 4456 4457
NVRAM_INIT:
	ldr	r1, =SP_BASE
	ldr	r0, [r1, #SP_RDR]
	mov	r0, #NVRAM_CMD_RDSR
	str	r0, [r1, #SP_TDR]
C
Carlo Perassi 已提交
4458
SP_loop1:
L
Linus Torvalds 已提交
4459 4460 4461 4462 4463
	ldr	r0, [r1, #SP_SR]
	tst	r0, #SP_TDRE
	beq	SP_loop1

	mov	r0, #SPI_8CLOCKS
C
Carlo Perassi 已提交
4464 4465
	str	r0, [r1, #SP_TDR]
SP_loop2:
L
Linus Torvalds 已提交
4466 4467 4468 4469 4470
	ldr	r0, [r1, #SP_SR]
	tst	r0, #SP_TDRE
	beq	SP_loop2

	ldr	r0, [r1, #SP_RDR]
C
Carlo Perassi 已提交
4471
SP_loop3:
L
Linus Torvalds 已提交
4472 4473 4474 4475 4476 4477 4478 4479
	ldr	r0, [r1, #SP_SR]
	tst	r0, #SP_RDRF
	beq	SP_loop3

	ldr	r0, [r1, #SP_RDR]
	and	r0, r0, #255
	bx	lr
.endfunc
C
Carlo Perassi 已提交
4480

L
Linus Torvalds 已提交
4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491
.func NVRAM_Xfer, NVRAM_XFER
	/* r0 = dest address */
	/* r1 = not used */
	/* r2 = src address within NVRAM */
	/* r3 = length */
NVRAM_XFER:
	stmdb	sp!, {r4, r5, lr}
	mov	r5, r0		/* save r0 (dest address) */
	mov	r4, r3		/* save r3 (length) */
	mov	r0, r2, LSR #5 /*  SPI memories put A8 in the command field */
	and	r0, r0, #8
C
Carlo Perassi 已提交
4492
	add	r0, r0, #NVRAM_CMD_READ
L
Linus Torvalds 已提交
4493 4494 4495
	ldr	r1, =NVRAM_SCRATCH
	strb	r0, [r1, #0]	/* save command in NVRAM_SCRATCH[0] */
	strb	r2, [r1, #1]    /* save low byte of source address in NVRAM_SCRATCH[1] */
C
Carlo Perassi 已提交
4496
_local1:
L
Linus Torvalds 已提交
4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517
	bl	NVRAM_INIT
	tst	r0, #NVRAM_SR_RDY
	bne	_local1
	mov	r0, #20
	bl	DELAY9
	mov	r2, r4		/* length */
	mov	r1, r5		/* dest address */
	mov	r0, #2		/* bytes to transfer in command */
	bl	NVRAM_XFER2
	ldmia	sp!, {r4, r5, lr}
	bx	lr
.endfunc

.func NVRAM_Xfer2, NVRAM_XFER2
NVRAM_XFER2:
	stmdb	sp!, {r4, r5, r6, lr}
	ldr	r4, =SP_BASE
	mov	r3, #0
	cmp	r0, #0
	bls	_local2
	ldr	r5, =NVRAM_SCRATCH
C
Carlo Perassi 已提交
4518
_local4:
L
Linus Torvalds 已提交
4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531
	ldrb	r6, [r5, r3]
	str	r6, [r4, #SP_TDR]
_local3:
	ldr	r6, [r4, #SP_SR]
	tst	r6, #SP_TDRE
	beq	_local3
	add	r3, r3, #1
	cmp	r3, r0 /* r0 is # of bytes to send out (command+addr) */
	blo	_local4
_local2:
	mov	r3, #SPI_8CLOCKS
	str	r3, [r4, #SP_TDR]
	ldr	r0, [r4, #SP_RDR]
C
Carlo Perassi 已提交
4532
_local5:
L
Linus Torvalds 已提交
4533 4534 4535 4536 4537 4538 4539
	ldr	r0, [r4, #SP_SR]
	tst	r0, #SP_RDRF
	beq	_local5
	ldr	r0, [r4, #SP_RDR] /* what's this byte?  It's the byte read while writing the TDR -- nonsense, because the NVRAM doesn't read and write at the same time */
	mov	r0, #0
	cmp	r2, #0  /* r2 is # of bytes to copy in */
	bls	_local6
C
Carlo Perassi 已提交
4540
_local7:
L
Linus Torvalds 已提交
4541 4542 4543 4544
	ldr	r5, [r4, #SP_SR]
	tst	r5, #SP_TDRE
	beq	_local7
	str	r3, [r4, #SP_TDR]  /* r3 has SPI_8CLOCKS */
C
Carlo Perassi 已提交
4545
_local8:
L
Linus Torvalds 已提交
4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559
	ldr	r5, [r4, #SP_SR]
	tst	r5, #SP_RDRF
	beq	_local8
	ldr	r5, [r4, #SP_RDR] /* but didn't we read this byte above? */
	strb	r5, [r1], #1 /* postindexed */
	add	r0, r0, #1
	cmp	r0, r2
	blo	_local7 /* since we don't send another address, the NVRAM must be capable of sequential reads */
_local6:
	mov	r0, #200
	bl	DELAY9
	ldmia	sp!, {r4, r5, r6, lr}
	bx	lr
#endif