at76c50x-usb.c 69.1 KB
Newer Older
Kalle Valo's avatar
Kalle Valo committed
1 2 3 4 5 6 7 8 9
/*
 * at76c503/at76c505 USB driver
 *
 * Copyright (c) 2002 - 2003 Oliver Kurth
 * Copyright (c) 2004 Joerg Albert <joerg.albert@gmx.de>
 * Copyright (c) 2004 Nick Jones
 * Copyright (c) 2004 Balint Seeber <n0_5p4m_p13453@hotmail.com>
 * Copyright (c) 2007 Guido Guenther <agx@sigxcpu.org>
 * Copyright (c) 2007 Kalle Valo <kalle.valo@iki.fi>
10
 * Copyright (c) 2010 Sebastian Smolorz <sesmo@gmx.net>
Kalle Valo's avatar
Kalle Valo committed
11 12 13 14 15 16 17 18 19 20 21
 *
 * 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 file is part of the Berlios driver for WLAN USB devices based on the
 * Atmel AT76C503A/505/505A.
 *
 * Some iw_handler code was taken from airo.c, (C) 1999 Benjamin Reed
 *
22 23 24 25
 * TODO list is at the wiki:
 *
 * http://wireless.kernel.org/en/users/Drivers/at76c50x-usb#TODO
 *
Kalle Valo's avatar
Kalle Valo committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
 */

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/list.h>
#include <linux/usb.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/wireless.h>
#include <net/iw_handler.h>
#include <net/ieee80211_radiotap.h>
#include <linux/firmware.h>
#include <linux/leds.h>
#include <net/mac80211.h>

#include "at76c50x-usb.h"

/* Version information */
#define DRIVER_NAME "at76c50x-usb"
#define DRIVER_VERSION	"0.17"
#define DRIVER_DESC "Atmel at76x USB Wireless LAN Driver"

/* at76_debug bits */
#define DBG_PROGRESS		0x00000001	/* authentication/accociation */
#define DBG_BSS_TABLE		0x00000002	/* show BSS table after scans */
#define DBG_IOCTL		0x00000004	/* ioctl calls / settings */
#define DBG_MAC_STATE		0x00000008	/* MAC state transitions */
#define DBG_TX_DATA		0x00000010	/* tx header */
#define DBG_TX_DATA_CONTENT	0x00000020	/* tx content */
#define DBG_TX_MGMT		0x00000040	/* tx management */
#define DBG_RX_DATA		0x00000080	/* rx data header */
#define DBG_RX_DATA_CONTENT	0x00000100	/* rx data content */
#define DBG_RX_MGMT		0x00000200	/* rx mgmt frame headers */
#define DBG_RX_BEACON		0x00000400	/* rx beacon */
#define DBG_RX_CTRL		0x00000800	/* rx control */
#define DBG_RX_MGMT_CONTENT	0x00001000	/* rx mgmt content */
#define DBG_RX_FRAGS		0x00002000	/* rx data fragment handling */
#define DBG_DEVSTART		0x00004000	/* fw download, device start */
#define DBG_URB			0x00008000	/* rx urb status, ... */
#define DBG_RX_ATMEL_HDR	0x00010000	/* Atmel-specific Rx headers */
#define DBG_PROC_ENTRY		0x00020000	/* procedure entries/exits */
#define DBG_PM			0x00040000	/* power management settings */
#define DBG_BSS_MATCH		0x00080000	/* BSS match failures */
#define DBG_PARAMS		0x00100000	/* show configured parameters */
#define DBG_WAIT_COMPLETE	0x00200000	/* command completion */
#define DBG_RX_FRAGS_SKB	0x00400000	/* skb header of Rx fragments */
#define DBG_BSS_TABLE_RM	0x00800000	/* purging bss table entries */
#define DBG_MONITOR_MODE	0x01000000	/* monitor mode */
#define DBG_MIB			0x02000000	/* dump all MIBs on startup */
#define DBG_MGMT_TIMER		0x04000000	/* dump mgmt_timer ops */
#define DBG_WE_EVENTS		0x08000000	/* dump wireless events */
#define DBG_FW			0x10000000	/* firmware download */
#define DBG_DFU			0x20000000	/* device firmware upgrade */
#define DBG_CMD			0x40000000
#define DBG_MAC80211		0x80000000

#define DBG_DEFAULTS		0

/* Use our own dbg macro */
92 93 94 95 96
#define at76_dbg(bits, format, arg...)					\
do {									\
	if (at76_debug & (bits))					\
		printk(KERN_DEBUG DRIVER_NAME ": " format "\n", ##arg);	\
} while (0)
Kalle Valo's avatar
Kalle Valo committed
97

98 99 100 101 102 103 104
#define at76_dbg_dump(bits, buf, len, format, arg...)			\
do {									\
	if (at76_debug & (bits)) {					\
		printk(KERN_DEBUG DRIVER_NAME ": " format "\n", ##arg);	\
		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);	\
	}								\
} while (0)
Kalle Valo's avatar
Kalle Valo committed
105

106
static uint at76_debug = DBG_DEFAULTS;
Kalle Valo's avatar
Kalle Valo committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

/* Protect against concurrent firmware loading and parsing */
static struct mutex fw_mutex;

static struct fwentry firmwares[] = {
	[0] = { "" },
	[BOARD_503_ISL3861] = { "atmel_at76c503-i3861.bin" },
	[BOARD_503_ISL3863] = { "atmel_at76c503-i3863.bin" },
	[BOARD_503] = { "atmel_at76c503-rfmd.bin" },
	[BOARD_503_ACC] = { "atmel_at76c503-rfmd-acc.bin" },
	[BOARD_505] = { "atmel_at76c505-rfmd.bin" },
	[BOARD_505_2958] = { "atmel_at76c505-rfmd2958.bin" },
	[BOARD_505A] = { "atmel_at76c505a-rfmd2958.bin" },
	[BOARD_505AMX] = { "atmel_at76c505amx-rfmd.bin" },
};
122 123 124 125 126 127 128 129
MODULE_FIRMWARE("atmel_at76c503-i3861.bin");
MODULE_FIRMWARE("atmel_at76c503-i3863.bin");
MODULE_FIRMWARE("atmel_at76c503-rfmd.bin");
MODULE_FIRMWARE("atmel_at76c503-rfmd-acc.bin");
MODULE_FIRMWARE("atmel_at76c505-rfmd.bin");
MODULE_FIRMWARE("atmel_at76c505-rfmd2958.bin");
MODULE_FIRMWARE("atmel_at76c505a-rfmd2958.bin");
MODULE_FIRMWARE("atmel_at76c505amx-rfmd.bin");
Kalle Valo's avatar
Kalle Valo committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

#define USB_DEVICE_DATA(__ops)	.driver_info = (kernel_ulong_t)(__ops)

static struct usb_device_id dev_table[] = {
	/*
	 * at76c503-i3861
	 */
	/* Generic AT76C503/3861 device */
	{ USB_DEVICE(0x03eb, 0x7603), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/* Linksys WUSB11 v2.1/v2.6 */
	{ USB_DEVICE(0x066b, 0x2211), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/* Netgear MA101 rev. A */
	{ USB_DEVICE(0x0864, 0x4100), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/* Tekram U300C / Allnet ALL0193 */
	{ USB_DEVICE(0x0b3b, 0x1612), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/* HP HN210W J7801A */
	{ USB_DEVICE(0x03f0, 0x011c), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/* Sitecom/Z-Com/Zyxel M4Y-750 */
	{ USB_DEVICE(0x0cde, 0x0001), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/* Dynalink/Askey WLL013 (intersil) */
	{ USB_DEVICE(0x069a, 0x0320), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/* EZ connect 11Mpbs Wireless USB Adapter SMC2662W v1 */
	{ USB_DEVICE(0x0d5c, 0xa001), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/* BenQ AWL300 */
	{ USB_DEVICE(0x04a5, 0x9000), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/* Addtron AWU-120, Compex WLU11 */
	{ USB_DEVICE(0x05dd, 0xff31), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/* Intel AP310 AnyPoint II USB */
	{ USB_DEVICE(0x8086, 0x0200), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/* Dynalink L11U */
	{ USB_DEVICE(0x0d8e, 0x7100), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/* Arescom WL-210, FCC id 07J-GL2411USB */
	{ USB_DEVICE(0x0d8e, 0x7110), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/* I-O DATA WN-B11/USB */
	{ USB_DEVICE(0x04bb, 0x0919), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/* BT Voyager 1010 */
	{ USB_DEVICE(0x069a, 0x0821), USB_DEVICE_DATA(BOARD_503_ISL3861) },
	/*
	 * at76c503-i3863
	 */
	/* Generic AT76C503/3863 device */
	{ USB_DEVICE(0x03eb, 0x7604), USB_DEVICE_DATA(BOARD_503_ISL3863) },
	/* Samsung SWL-2100U */
	{ USB_DEVICE(0x055d, 0xa000), USB_DEVICE_DATA(BOARD_503_ISL3863) },
	/*
	 * at76c503-rfmd
	 */
	/* Generic AT76C503/RFMD device */
	{ USB_DEVICE(0x03eb, 0x7605), USB_DEVICE_DATA(BOARD_503) },
	/* Dynalink/Askey WLL013 (rfmd) */
	{ USB_DEVICE(0x069a, 0x0321), USB_DEVICE_DATA(BOARD_503) },
	/* Linksys WUSB11 v2.6 */
	{ USB_DEVICE(0x077b, 0x2219), USB_DEVICE_DATA(BOARD_503) },
	/* Network Everywhere NWU11B */
	{ USB_DEVICE(0x077b, 0x2227), USB_DEVICE_DATA(BOARD_503) },
	/* Netgear MA101 rev. B */
	{ USB_DEVICE(0x0864, 0x4102), USB_DEVICE_DATA(BOARD_503) },
	/* D-Link DWL-120 rev. E */
	{ USB_DEVICE(0x2001, 0x3200), USB_DEVICE_DATA(BOARD_503) },
	/* Actiontec 802UAT1, HWU01150-01UK */
	{ USB_DEVICE(0x1668, 0x7605), USB_DEVICE_DATA(BOARD_503) },
	/* AirVast W-Buddie WN210 */
	{ USB_DEVICE(0x03eb, 0x4102), USB_DEVICE_DATA(BOARD_503) },
	/* Dick Smith Electronics XH1153 802.11b USB adapter */
	{ USB_DEVICE(0x1371, 0x5743), USB_DEVICE_DATA(BOARD_503) },
	/* CNet CNUSB611 */
	{ USB_DEVICE(0x1371, 0x0001), USB_DEVICE_DATA(BOARD_503) },
	/* FiberLine FL-WL200U */
	{ USB_DEVICE(0x1371, 0x0002), USB_DEVICE_DATA(BOARD_503) },
	/* BenQ AWL400 USB stick */
	{ USB_DEVICE(0x04a5, 0x9001), USB_DEVICE_DATA(BOARD_503) },
	/* 3Com 3CRSHEW696 */
	{ USB_DEVICE(0x0506, 0x0a01), USB_DEVICE_DATA(BOARD_503) },
	/* Siemens Santis ADSL WLAN USB adapter WLL 013 */
	{ USB_DEVICE(0x0681, 0x001b), USB_DEVICE_DATA(BOARD_503) },
	/* Belkin F5D6050, version 2 */
	{ USB_DEVICE(0x050d, 0x0050), USB_DEVICE_DATA(BOARD_503) },
	/* iBlitzz, BWU613 (not *B or *SB) */
	{ USB_DEVICE(0x07b8, 0xb000), USB_DEVICE_DATA(BOARD_503) },
	/* Gigabyte GN-WLBM101 */
	{ USB_DEVICE(0x1044, 0x8003), USB_DEVICE_DATA(BOARD_503) },
	/* Planex GW-US11S */
	{ USB_DEVICE(0x2019, 0x3220), USB_DEVICE_DATA(BOARD_503) },
	/* Internal WLAN adapter in h5[4,5]xx series iPAQs */
	{ USB_DEVICE(0x049f, 0x0032), USB_DEVICE_DATA(BOARD_503) },
	/* Corega Wireless LAN USB-11 mini */
	{ USB_DEVICE(0x07aa, 0x0011), USB_DEVICE_DATA(BOARD_503) },
	/* Corega Wireless LAN USB-11 mini2 */
	{ USB_DEVICE(0x07aa, 0x0018), USB_DEVICE_DATA(BOARD_503) },
	/* Uniden PCW100 */
	{ USB_DEVICE(0x05dd, 0xff35), USB_DEVICE_DATA(BOARD_503) },
	/*
	 * at76c503-rfmd-acc
	 */
	/* SMC2664W */
	{ USB_DEVICE(0x083a, 0x3501), USB_DEVICE_DATA(BOARD_503_ACC) },
	/* Belkin F5D6050, SMC2662W v2, SMC2662W-AR */
	{ USB_DEVICE(0x0d5c, 0xa002), USB_DEVICE_DATA(BOARD_503_ACC) },
	/*
	 * at76c505-rfmd
	 */
	/* Generic AT76C505/RFMD */
	{ USB_DEVICE(0x03eb, 0x7606), USB_DEVICE_DATA(BOARD_505) },
	/*
	 * at76c505-rfmd2958
	 */
	/* Generic AT76C505/RFMD, OvisLink WL-1130USB */
	{ USB_DEVICE(0x03eb, 0x7613), USB_DEVICE_DATA(BOARD_505_2958) },
	/* Fiberline FL-WL240U */
	{ USB_DEVICE(0x1371, 0x0014), USB_DEVICE_DATA(BOARD_505_2958) },
	/* CNet CNUSB-611G */
	{ USB_DEVICE(0x1371, 0x0013), USB_DEVICE_DATA(BOARD_505_2958) },
	/* Linksys WUSB11 v2.8 */
	{ USB_DEVICE(0x1915, 0x2233), USB_DEVICE_DATA(BOARD_505_2958) },
	/* Xterasys XN-2122B, IBlitzz BWU613B/BWU613SB */
	{ USB_DEVICE(0x12fd, 0x1001), USB_DEVICE_DATA(BOARD_505_2958) },
	/* Corega WLAN USB Stick 11 */
	{ USB_DEVICE(0x07aa, 0x7613), USB_DEVICE_DATA(BOARD_505_2958) },
	/* Microstar MSI Box MS6978 */
	{ USB_DEVICE(0x0db0, 0x1020), USB_DEVICE_DATA(BOARD_505_2958) },
	/*
	 * at76c505a-rfmd2958
	 */
	/* Generic AT76C505A device */
	{ USB_DEVICE(0x03eb, 0x7614), USB_DEVICE_DATA(BOARD_505A) },
	/* Generic AT76C505AS device */
	{ USB_DEVICE(0x03eb, 0x7617), USB_DEVICE_DATA(BOARD_505A) },
	/* Siemens Gigaset USB WLAN Adapter 11 */
	{ USB_DEVICE(0x1690, 0x0701), USB_DEVICE_DATA(BOARD_505A) },
259 260
	/* OQO Model 01+ Internal Wi-Fi */
	{ USB_DEVICE(0x1557, 0x0002), USB_DEVICE_DATA(BOARD_505A) },
Kalle Valo's avatar
Kalle Valo committed
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
	/*
	 * at76c505amx-rfmd
	 */
	/* Generic AT76C505AMX device */
	{ USB_DEVICE(0x03eb, 0x7615), USB_DEVICE_DATA(BOARD_505AMX) },
	{ }
};

MODULE_DEVICE_TABLE(usb, dev_table);

/* Supported rates of this hardware, bit 7 marks basic rates */
static const u8 hw_rates[] = { 0x82, 0x84, 0x0b, 0x16 };

static const char *const preambles[] = { "long", "short", "auto" };

/* Firmware download */
/* DFU states */
#define STATE_IDLE			0x00
#define STATE_DETACH			0x01
#define STATE_DFU_IDLE			0x02
#define STATE_DFU_DOWNLOAD_SYNC		0x03
#define STATE_DFU_DOWNLOAD_BUSY		0x04
#define STATE_DFU_DOWNLOAD_IDLE		0x05
#define STATE_DFU_MANIFEST_SYNC		0x06
#define STATE_DFU_MANIFEST		0x07
#define STATE_DFU_MANIFEST_WAIT_RESET	0x08
#define STATE_DFU_UPLOAD_IDLE		0x09
#define STATE_DFU_ERROR			0x0a

/* DFU commands */
#define DFU_DETACH			0
#define DFU_DNLOAD			1
#define DFU_UPLOAD			2
#define DFU_GETSTATUS			3
#define DFU_CLRSTATUS			4
#define DFU_GETSTATE			5
#define DFU_ABORT			6

#define FW_BLOCK_SIZE 1024

struct dfu_status {
	unsigned char status;
	unsigned char poll_timeout[3];
	unsigned char state;
	unsigned char string;
306
} __packed;
Kalle Valo's avatar
Kalle Valo committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

static inline int at76_is_intersil(enum board_type board)
{
	return (board == BOARD_503_ISL3861 || board == BOARD_503_ISL3863);
}

static inline int at76_is_503rfmd(enum board_type board)
{
	return (board == BOARD_503 || board == BOARD_503_ACC);
}

static inline int at76_is_505a(enum board_type board)
{
	return (board == BOARD_505A || board == BOARD_505AMX);
}

/* Load a block of the first (internal) part of the firmware */
static int at76_load_int_fw_block(struct usb_device *udev, int blockno,
				  void *block, int size)
{
	return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), DFU_DNLOAD,
			       USB_TYPE_CLASS | USB_DIR_OUT |
			       USB_RECIP_INTERFACE, blockno, 0, block, size,
			       USB_CTRL_GET_TIMEOUT);
}

static int at76_dfu_get_status(struct usb_device *udev,
			       struct dfu_status *status)
{
	int ret;

	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), DFU_GETSTATUS,
			      USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
			      0, 0, status, sizeof(struct dfu_status),
			      USB_CTRL_GET_TIMEOUT);
	return ret;
}

345
static int at76_dfu_get_state(struct usb_device *udev, u8 *state)
Kalle Valo's avatar
Kalle Valo committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
{
	int ret;

	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), DFU_GETSTATE,
			      USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
			      0, 0, state, 1, USB_CTRL_GET_TIMEOUT);
	return ret;
}

/* Convert timeout from the DFU status to jiffies */
static inline unsigned long at76_get_timeout(struct dfu_status *s)
{
	return msecs_to_jiffies((s->poll_timeout[2] << 16)
				| (s->poll_timeout[1] << 8)
				| (s->poll_timeout[0]));
}

/* Load internal firmware from the buffer.  If manifest_sync_timeout > 0, use
 * its value in jiffies in the MANIFEST_SYNC state.  */
static int at76_usbdfu_download(struct usb_device *udev, u8 *buf, u32 size,
				int manifest_sync_timeout)
{
	int ret = 0;
	int need_dfu_state = 1;
	int is_done = 0;
	u32 dfu_timeout = 0;
	int bsize = 0;
	int blockno = 0;
374 375 376
	struct dfu_status *dfu_stat_buf = NULL;
	u8 *dfu_state = NULL;
	u8 *block = NULL;
Kalle Valo's avatar
Kalle Valo committed
377 378 379 380 381

	at76_dbg(DBG_DFU, "%s( %p, %u, %d)", __func__, buf, size,
		 manifest_sync_timeout);

	if (!size) {
382
		dev_err(&udev->dev, "FW buffer length invalid!\n");
Kalle Valo's avatar
Kalle Valo committed
383 384 385
		return -EINVAL;
	}

386 387 388 389 390 391
	dfu_stat_buf = kmalloc(sizeof(struct dfu_status), GFP_KERNEL);
	if (!dfu_stat_buf) {
		ret = -ENOMEM;
		goto exit;
	}

Kalle Valo's avatar
Kalle Valo committed
392
	block = kmalloc(FW_BLOCK_SIZE, GFP_KERNEL);
393 394 395 396 397 398 399 400 401 402 403
	if (!block) {
		ret = -ENOMEM;
		goto exit;
	}

	dfu_state = kmalloc(sizeof(u8), GFP_KERNEL);
	if (!dfu_state) {
		ret = -ENOMEM;
		goto exit;
	}
	*dfu_state = 0;
Kalle Valo's avatar
Kalle Valo committed
404 405 406

	do {
		if (need_dfu_state) {
407
			ret = at76_dfu_get_state(udev, dfu_state);
Kalle Valo's avatar
Kalle Valo committed
408
			if (ret < 0) {
409 410
				dev_err(&udev->dev,
					"cannot get DFU state: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
411 412 413 414 415
				goto exit;
			}
			need_dfu_state = 0;
		}

416
		switch (*dfu_state) {
Kalle Valo's avatar
Kalle Valo committed
417 418
		case STATE_DFU_DOWNLOAD_SYNC:
			at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_SYNC");
419
			ret = at76_dfu_get_status(udev, dfu_stat_buf);
Kalle Valo's avatar
Kalle Valo committed
420
			if (ret >= 0) {
421 422
				*dfu_state = dfu_stat_buf->state;
				dfu_timeout = at76_get_timeout(dfu_stat_buf);
Kalle Valo's avatar
Kalle Valo committed
423 424
				need_dfu_state = 0;
			} else
425 426 427
				dev_err(&udev->dev,
					"at76_dfu_get_status returned %d\n",
					ret);
Kalle Valo's avatar
Kalle Valo committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
			break;

		case STATE_DFU_DOWNLOAD_BUSY:
			at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_BUSY");
			need_dfu_state = 1;

			at76_dbg(DBG_DFU, "DFU: Resetting device");
			schedule_timeout_interruptible(dfu_timeout);
			break;

		case STATE_DFU_DOWNLOAD_IDLE:
			at76_dbg(DBG_DFU, "DOWNLOAD...");
			/* fall through */
		case STATE_DFU_IDLE:
			at76_dbg(DBG_DFU, "DFU IDLE");

			bsize = min_t(int, size, FW_BLOCK_SIZE);
			memcpy(block, buf, bsize);
			at76_dbg(DBG_DFU, "int fw, size left = %5d, "
				 "bsize = %4d, blockno = %2d", size, bsize,
				 blockno);
			ret =
			    at76_load_int_fw_block(udev, blockno, block, bsize);
			buf += bsize;
			size -= bsize;
			blockno++;

			if (ret != bsize)
456 457 458
				dev_err(&udev->dev,
					"at76_load_int_fw_block returned %d\n",
					ret);
Kalle Valo's avatar
Kalle Valo committed
459 460 461 462 463 464
			need_dfu_state = 1;
			break;

		case STATE_DFU_MANIFEST_SYNC:
			at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST_SYNC");

465
			ret = at76_dfu_get_status(udev, dfu_stat_buf);
Kalle Valo's avatar
Kalle Valo committed
466 467 468
			if (ret < 0)
				break;

469 470
			*dfu_state = dfu_stat_buf->state;
			dfu_timeout = at76_get_timeout(dfu_stat_buf);
Kalle Valo's avatar
Kalle Valo committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
			need_dfu_state = 0;

			/* override the timeout from the status response,
			   needed for AT76C505A */
			if (manifest_sync_timeout > 0)
				dfu_timeout = manifest_sync_timeout;

			at76_dbg(DBG_DFU, "DFU: Waiting for manifest phase");
			schedule_timeout_interruptible(dfu_timeout);
			break;

		case STATE_DFU_MANIFEST:
			at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST");
			is_done = 1;
			break;

		case STATE_DFU_MANIFEST_WAIT_RESET:
			at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST_WAIT_RESET");
			is_done = 1;
			break;

		case STATE_DFU_UPLOAD_IDLE:
			at76_dbg(DBG_DFU, "STATE_DFU_UPLOAD_IDLE");
			break;

		case STATE_DFU_ERROR:
			at76_dbg(DBG_DFU, "STATE_DFU_ERROR");
			ret = -EPIPE;
			break;

		default:
502
			at76_dbg(DBG_DFU, "DFU UNKNOWN STATE (%d)", *dfu_state);
Kalle Valo's avatar
Kalle Valo committed
503 504 505 506 507 508
			ret = -EINVAL;
			break;
		}
	} while (!is_done && (ret >= 0));

exit:
509
	kfree(dfu_state);
Kalle Valo's avatar
Kalle Valo committed
510
	kfree(block);
511 512
	kfree(dfu_stat_buf);

Kalle Valo's avatar
Kalle Valo committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
	if (ret >= 0)
		ret = 0;

	return ret;
}

/* LED trigger */
static int tx_activity;
static void at76_ledtrig_tx_timerfunc(unsigned long data);
static DEFINE_TIMER(ledtrig_tx_timer, at76_ledtrig_tx_timerfunc, 0, 0);
DEFINE_LED_TRIGGER(ledtrig_tx);

static void at76_ledtrig_tx_timerfunc(unsigned long data)
{
	static int tx_lastactivity;

	if (tx_lastactivity != tx_activity) {
		tx_lastactivity = tx_activity;
		led_trigger_event(ledtrig_tx, LED_FULL);
		mod_timer(&ledtrig_tx_timer, jiffies + HZ / 4);
	} else
		led_trigger_event(ledtrig_tx, LED_OFF);
}

static void at76_ledtrig_tx_activity(void)
{
	tx_activity++;
	if (!timer_pending(&ledtrig_tx_timer))
		mod_timer(&ledtrig_tx_timer, jiffies + HZ / 4);
}

static int at76_remap(struct usb_device *udev)
{
	int ret;
	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0a,
			      USB_TYPE_VENDOR | USB_DIR_OUT |
			      USB_RECIP_INTERFACE, 0, 0, NULL, 0,
			      USB_CTRL_GET_TIMEOUT);
	if (ret < 0)
		return ret;
	return 0;
}

static int at76_get_op_mode(struct usb_device *udev)
{
	int ret;
559 560
	u8 saved;
	u8 *op_mode;
Kalle Valo's avatar
Kalle Valo committed
561

562 563 564
	op_mode = kmalloc(1, GFP_NOIO);
	if (!op_mode)
		return -ENOMEM;
Kalle Valo's avatar
Kalle Valo committed
565 566
	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
			      USB_TYPE_VENDOR | USB_DIR_IN |
567
			      USB_RECIP_INTERFACE, 0x01, 0, op_mode, 1,
Kalle Valo's avatar
Kalle Valo committed
568
			      USB_CTRL_GET_TIMEOUT);
569 570 571
	saved = *op_mode;
	kfree(op_mode);

Kalle Valo's avatar
Kalle Valo committed
572 573 574 575 576
	if (ret < 0)
		return ret;
	else if (ret < 1)
		return -EIO;
	else
577
		return saved;
Kalle Valo's avatar
Kalle Valo committed
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
}

/* Load a block of the second ("external") part of the firmware */
static inline int at76_load_ext_fw_block(struct usb_device *udev, int blockno,
					 void *block, int size)
{
	return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0e,
			       USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
			       0x0802, blockno, block, size,
			       USB_CTRL_GET_TIMEOUT);
}

static inline int at76_get_hw_cfg(struct usb_device *udev,
				  union at76_hwcfg *buf, int buf_size)
{
	return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
			       USB_TYPE_VENDOR | USB_DIR_IN |
			       USB_RECIP_INTERFACE, 0x0a02, 0,
			       buf, buf_size, USB_CTRL_GET_TIMEOUT);
}

/* Intersil boards use a different "value" for GetHWConfig requests */
static inline int at76_get_hw_cfg_intersil(struct usb_device *udev,
					   union at76_hwcfg *buf, int buf_size)
{
	return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
			       USB_TYPE_VENDOR | USB_DIR_IN |
			       USB_RECIP_INTERFACE, 0x0902, 0,
			       buf, buf_size, USB_CTRL_GET_TIMEOUT);
}

/* Get the hardware configuration for the adapter and put it to the appropriate
 * fields of 'priv' (the GetHWConfig request and interpretation of the result
 * depends on the board type) */
static int at76_get_hw_config(struct at76_priv *priv)
{
	int ret;
	union at76_hwcfg *hwcfg = kmalloc(sizeof(*hwcfg), GFP_KERNEL);

	if (!hwcfg)
		return -ENOMEM;

	if (at76_is_intersil(priv->board_type)) {
		ret = at76_get_hw_cfg_intersil(priv->udev, hwcfg,
					       sizeof(hwcfg->i));
		if (ret < 0)
			goto exit;
		memcpy(priv->mac_addr, hwcfg->i.mac_addr, ETH_ALEN);
		priv->regulatory_domain = hwcfg->i.regulatory_domain;
	} else if (at76_is_503rfmd(priv->board_type)) {
		ret = at76_get_hw_cfg(priv->udev, hwcfg, sizeof(hwcfg->r3));
		if (ret < 0)
			goto exit;
		memcpy(priv->mac_addr, hwcfg->r3.mac_addr, ETH_ALEN);
		priv->regulatory_domain = hwcfg->r3.regulatory_domain;
	} else {
		ret = at76_get_hw_cfg(priv->udev, hwcfg, sizeof(hwcfg->r5));
		if (ret < 0)
			goto exit;
		memcpy(priv->mac_addr, hwcfg->r5.mac_addr, ETH_ALEN);
		priv->regulatory_domain = hwcfg->r5.regulatory_domain;
	}

exit:
	kfree(hwcfg);
	if (ret < 0)
644
		wiphy_err(priv->hw->wiphy, "cannot get HW Config (error %d)\n",
645
			  ret);
Kalle Valo's avatar
Kalle Valo committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689

	return ret;
}

static struct reg_domain const *at76_get_reg_domain(u16 code)
{
	int i;
	static struct reg_domain const fd_tab[] = {
		{ 0x10, "FCC (USA)", 0x7ff },	/* ch 1-11 */
		{ 0x20, "IC (Canada)", 0x7ff },	/* ch 1-11 */
		{ 0x30, "ETSI (most of Europe)", 0x1fff },	/* ch 1-13 */
		{ 0x31, "Spain", 0x600 },	/* ch 10-11 */
		{ 0x32, "France", 0x1e00 },	/* ch 10-13 */
		{ 0x40, "MKK (Japan)", 0x2000 },	/* ch 14 */
		{ 0x41, "MKK1 (Japan)", 0x3fff },	/* ch 1-14 */
		{ 0x50, "Israel", 0x3fc },	/* ch 3-9 */
		{ 0x00, "<unknown>", 0xffffffff }	/* ch 1-32 */
	};

	/* Last entry is fallback for unknown domain code */
	for (i = 0; i < ARRAY_SIZE(fd_tab) - 1; i++)
		if (code == fd_tab[i].code)
			break;

	return &fd_tab[i];
}

static inline int at76_get_mib(struct usb_device *udev, u16 mib, void *buf,
			       int buf_size)
{
	int ret;

	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
			      USB_TYPE_VENDOR | USB_DIR_IN |
			      USB_RECIP_INTERFACE, mib << 8, 0, buf, buf_size,
			      USB_CTRL_GET_TIMEOUT);
	if (ret >= 0 && ret != buf_size)
		return -EIO;
	return ret;
}

/* Return positive number for status, negative for an error */
static inline int at76_get_cmd_status(struct usb_device *udev, u8 cmd)
{
690
	u8 *stat_buf;
Kalle Valo's avatar
Kalle Valo committed
691 692
	int ret;

693 694 695 696
	stat_buf = kmalloc(40, GFP_NOIO);
	if (!stat_buf)
		return -ENOMEM;

Kalle Valo's avatar
Kalle Valo committed
697
	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x22,
698 699 700 701 702 703
			USB_TYPE_VENDOR | USB_DIR_IN |
			USB_RECIP_INTERFACE, cmd, 0, stat_buf,
			40, USB_CTRL_GET_TIMEOUT);
	if (ret >= 0)
		ret = stat_buf[5];
	kfree(stat_buf);
Kalle Valo's avatar
Kalle Valo committed
704

705
	return ret;
Kalle Valo's avatar
Kalle Valo committed
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
}

#define MAKE_CMD_CASE(c) case (c): return #c
static const char *at76_get_cmd_string(u8 cmd_status)
{
	switch (cmd_status) {
		MAKE_CMD_CASE(CMD_SET_MIB);
		MAKE_CMD_CASE(CMD_GET_MIB);
		MAKE_CMD_CASE(CMD_SCAN);
		MAKE_CMD_CASE(CMD_JOIN);
		MAKE_CMD_CASE(CMD_START_IBSS);
		MAKE_CMD_CASE(CMD_RADIO_ON);
		MAKE_CMD_CASE(CMD_RADIO_OFF);
		MAKE_CMD_CASE(CMD_STARTUP);
	}

	return "UNKNOWN";
}

725
static int at76_set_card_command(struct usb_device *udev, u8 cmd, void *buf,
Kalle Valo's avatar
Kalle Valo committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
				 int buf_size)
{
	int ret;
	struct at76_command *cmd_buf = kmalloc(sizeof(struct at76_command) +
					       buf_size, GFP_KERNEL);

	if (!cmd_buf)
		return -ENOMEM;

	cmd_buf->cmd = cmd;
	cmd_buf->reserved = 0;
	cmd_buf->size = cpu_to_le16(buf_size);
	memcpy(cmd_buf->data, buf, buf_size);

	at76_dbg_dump(DBG_CMD, cmd_buf, sizeof(struct at76_command) + buf_size,
		      "issuing command %s (0x%02x)",
		      at76_get_cmd_string(cmd), cmd);

	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0e,
			      USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
			      0, 0, cmd_buf,
			      sizeof(struct at76_command) + buf_size,
			      USB_CTRL_GET_TIMEOUT);
	kfree(cmd_buf);
	return ret;
}

#define MAKE_CMD_STATUS_CASE(c)	case (c): return #c
static const char *at76_get_cmd_status_string(u8 cmd_status)
{
	switch (cmd_status) {
		MAKE_CMD_STATUS_CASE(CMD_STATUS_IDLE);
		MAKE_CMD_STATUS_CASE(CMD_STATUS_COMPLETE);
		MAKE_CMD_STATUS_CASE(CMD_STATUS_UNKNOWN);
		MAKE_CMD_STATUS_CASE(CMD_STATUS_INVALID_PARAMETER);
		MAKE_CMD_STATUS_CASE(CMD_STATUS_FUNCTION_NOT_SUPPORTED);
		MAKE_CMD_STATUS_CASE(CMD_STATUS_TIME_OUT);
		MAKE_CMD_STATUS_CASE(CMD_STATUS_IN_PROGRESS);
		MAKE_CMD_STATUS_CASE(CMD_STATUS_HOST_FAILURE);
		MAKE_CMD_STATUS_CASE(CMD_STATUS_SCAN_FAILED);
	}

	return "UNKNOWN";
}

/* Wait until the command is completed */
static int at76_wait_completion(struct at76_priv *priv, int cmd)
{
	int status = 0;
	unsigned long timeout = jiffies + CMD_COMPLETION_TIMEOUT;

	do {
		status = at76_get_cmd_status(priv->udev, cmd);
		if (status < 0) {
780 781 782
			wiphy_err(priv->hw->wiphy,
				  "at76_get_cmd_status failed: %d\n",
				  status);
Kalle Valo's avatar
Kalle Valo committed
783 784 785 786 787 788 789 790 791 792 793 794 795 796
			break;
		}

		at76_dbg(DBG_WAIT_COMPLETE,
			 "%s: Waiting on cmd %d, status = %d (%s)",
			 wiphy_name(priv->hw->wiphy), cmd, status,
			 at76_get_cmd_status_string(status));

		if (status != CMD_STATUS_IN_PROGRESS
		    && status != CMD_STATUS_IDLE)
			break;

		schedule_timeout_interruptible(HZ / 10);	/* 100 ms */
		if (time_after(jiffies, timeout)) {
797 798
			wiphy_err(priv->hw->wiphy,
				  "completion timeout for command %d\n", cmd);
Kalle Valo's avatar
Kalle Valo committed
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
			status = -ETIMEDOUT;
			break;
		}
	} while (1);

	return status;
}

static int at76_set_mib(struct at76_priv *priv, struct set_mib_buffer *buf)
{
	int ret;

	ret = at76_set_card_command(priv->udev, CMD_SET_MIB, buf,
				    offsetof(struct set_mib_buffer,
					     data) + buf->size);
	if (ret < 0)
		return ret;

	ret = at76_wait_completion(priv, CMD_SET_MIB);
	if (ret != CMD_STATUS_COMPLETE) {
819 820 821
		wiphy_info(priv->hw->wiphy,
			   "set_mib: at76_wait_completion failed with %d\n",
			   ret);
Kalle Valo's avatar
Kalle Valo committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
		ret = -EIO;
	}

	return ret;
}

/* Return < 0 on error, == 0 if no command sent, == 1 if cmd sent */
static int at76_set_radio(struct at76_priv *priv, int enable)
{
	int ret;
	int cmd;

	if (priv->radio_on == enable)
		return 0;

	cmd = enable ? CMD_RADIO_ON : CMD_RADIO_OFF;

	ret = at76_set_card_command(priv->udev, cmd, NULL, 0);
	if (ret < 0)
841 842
		wiphy_err(priv->hw->wiphy,
			  "at76_set_card_command(%d) failed: %d\n", cmd, ret);
Kalle Valo's avatar
Kalle Valo committed
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
	else
		ret = 1;

	priv->radio_on = enable;
	return ret;
}

/* Set current power save mode (AT76_PM_OFF/AT76_PM_ON/AT76_PM_SMART) */
static int at76_set_pm_mode(struct at76_priv *priv)
{
	int ret = 0;

	priv->mib_buf.type = MIB_MAC_MGMT;
	priv->mib_buf.size = 1;
	priv->mib_buf.index = offsetof(struct mib_mac_mgmt, power_mgmt_mode);
	priv->mib_buf.data.byte = priv->pm_mode;

	ret = at76_set_mib(priv, &priv->mib_buf);
	if (ret < 0)
862 863
		wiphy_err(priv->hw->wiphy, "set_mib (pm_mode) failed: %d\n",
			  ret);
Kalle Valo's avatar
Kalle Valo committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878

	return ret;
}

static int at76_set_preamble(struct at76_priv *priv, u8 type)
{
	int ret = 0;

	priv->mib_buf.type = MIB_LOCAL;
	priv->mib_buf.size = 1;
	priv->mib_buf.index = offsetof(struct mib_local, preamble_type);
	priv->mib_buf.data.byte = type;

	ret = at76_set_mib(priv, &priv->mib_buf);
	if (ret < 0)
879 880
		wiphy_err(priv->hw->wiphy, "set_mib (preamble) failed: %d\n",
			  ret);
Kalle Valo's avatar
Kalle Valo committed
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895

	return ret;
}

static int at76_set_frag(struct at76_priv *priv, u16 size)
{
	int ret = 0;

	priv->mib_buf.type = MIB_MAC;
	priv->mib_buf.size = 2;
	priv->mib_buf.index = offsetof(struct mib_mac, frag_threshold);
	priv->mib_buf.data.word = cpu_to_le16(size);

	ret = at76_set_mib(priv, &priv->mib_buf);
	if (ret < 0)
896 897
		wiphy_err(priv->hw->wiphy,
			  "set_mib (frag threshold) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912

	return ret;
}

static int at76_set_rts(struct at76_priv *priv, u16 size)
{
	int ret = 0;

	priv->mib_buf.type = MIB_MAC;
	priv->mib_buf.size = 2;
	priv->mib_buf.index = offsetof(struct mib_mac, rts_threshold);
	priv->mib_buf.data.word = cpu_to_le16(size);

	ret = at76_set_mib(priv, &priv->mib_buf);
	if (ret < 0)
913
		wiphy_err(priv->hw->wiphy, "set_mib (rts) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928

	return ret;
}

static int at76_set_autorate_fallback(struct at76_priv *priv, int onoff)
{
	int ret = 0;

	priv->mib_buf.type = MIB_LOCAL;
	priv->mib_buf.size = 1;
	priv->mib_buf.index = offsetof(struct mib_local, txautorate_fallback);
	priv->mib_buf.data.byte = onoff;

	ret = at76_set_mib(priv, &priv->mib_buf);
	if (ret < 0)
929 930
		wiphy_err(priv->hw->wiphy,
			  "set_mib (autorate fallback) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947

	return ret;
}

static void at76_dump_mib_mac_addr(struct at76_priv *priv)
{
	int i;
	int ret;
	struct mib_mac_addr *m = kmalloc(sizeof(struct mib_mac_addr),
					 GFP_KERNEL);

	if (!m)
		return;

	ret = at76_get_mib(priv->udev, MIB_MAC_ADDR, m,
			   sizeof(struct mib_mac_addr));
	if (ret < 0) {
948
		wiphy_err(priv->hw->wiphy,
949
			  "at76_get_mib (MAC_ADDR) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
950 951 952
		goto exit;
	}

953
	at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: mac_addr %pM res 0x%x 0x%x",
Kalle Valo's avatar
Kalle Valo committed
954
		 wiphy_name(priv->hw->wiphy),
955
		 m->mac_addr, m->res[0], m->res[1]);
Kalle Valo's avatar
Kalle Valo committed
956
	for (i = 0; i < ARRAY_SIZE(m->group_addr); i++)
957
		at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: group addr %d: %pM, "
Kalle Valo's avatar
Kalle Valo committed
958
			 "status %d", wiphy_name(priv->hw->wiphy), i,
959
			 m->group_addr[i], m->group_addr_status[i]);
Kalle Valo's avatar
Kalle Valo committed
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
exit:
	kfree(m);
}

static void at76_dump_mib_mac_wep(struct at76_priv *priv)
{
	int i;
	int ret;
	int key_len;
	struct mib_mac_wep *m = kmalloc(sizeof(struct mib_mac_wep), GFP_KERNEL);

	if (!m)
		return;

	ret = at76_get_mib(priv->udev, MIB_MAC_WEP, m,
			   sizeof(struct mib_mac_wep));
	if (ret < 0) {
977
		wiphy_err(priv->hw->wiphy,
978
			  "at76_get_mib (MAC_WEP) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
		goto exit;
	}

	at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: priv_invoked %u def_key_id %u "
		 "key_len %u excl_unencr %u wep_icv_err %u wep_excluded %u "
		 "encr_level %u key %d", wiphy_name(priv->hw->wiphy),
		 m->privacy_invoked, m->wep_default_key_id,
		 m->wep_key_mapping_len, m->exclude_unencrypted,
		 le32_to_cpu(m->wep_icv_error_count),
		 le32_to_cpu(m->wep_excluded_count), m->encryption_level,
		 m->wep_default_key_id);

	key_len = (m->encryption_level == 1) ?
	    WEP_SMALL_KEY_LEN : WEP_LARGE_KEY_LEN;

	for (i = 0; i < WEP_KEYS; i++)
995
		at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: key %d: %*phD",
Kalle Valo's avatar
Kalle Valo committed
996
			 wiphy_name(priv->hw->wiphy), i,
997
			 key_len, m->wep_default_keyvalue[i]);
Kalle Valo's avatar
Kalle Valo committed
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
exit:
	kfree(m);
}

static void at76_dump_mib_mac_mgmt(struct at76_priv *priv)
{
	int ret;
	struct mib_mac_mgmt *m = kmalloc(sizeof(struct mib_mac_mgmt),
					 GFP_KERNEL);

	if (!m)
		return;

	ret = at76_get_mib(priv->udev, MIB_MAC_MGMT, m,
			   sizeof(struct mib_mac_mgmt));
	if (ret < 0) {
1014
		wiphy_err(priv->hw->wiphy,
1015
			  "at76_get_mib (MAC_MGMT) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1016 1017 1018 1019 1020 1021
		goto exit;
	}

	at76_dbg(DBG_MIB, "%s: MIB MAC_MGMT: beacon_period %d CFP_max_duration "
		 "%d medium_occupancy_limit %d station_id 0x%x ATIM_window %d "
		 "CFP_mode %d privacy_opt_impl %d DTIM_period %d CFP_period %d "
1022
		 "current_bssid %pM current_essid %*phD current_bss_type %d "
Kalle Valo's avatar
Kalle Valo committed
1023 1024 1025 1026 1027 1028 1029 1030
		 "pm_mode %d ibss_change %d res %d "
		 "multi_domain_capability_implemented %d "
		 "international_roaming %d country_string %.3s",
		 wiphy_name(priv->hw->wiphy), le16_to_cpu(m->beacon_period),
		 le16_to_cpu(m->CFP_max_duration),
		 le16_to_cpu(m->medium_occupancy_limit),
		 le16_to_cpu(m->station_id), le16_to_cpu(m->ATIM_window),
		 m->CFP_mode, m->privacy_option_implemented, m->DTIM_period,
1031
		 m->CFP_period, m->current_bssid,
1032
		 IW_ESSID_MAX_SIZE, m->current_essid,
Kalle Valo's avatar
Kalle Valo committed
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
		 m->current_bss_type, m->power_mgmt_mode, m->ibss_change,
		 m->res, m->multi_domain_capability_implemented,
		 m->multi_domain_capability_enabled, m->country_string);
exit:
	kfree(m);
}

static void at76_dump_mib_mac(struct at76_priv *priv)
{
	int ret;
	struct mib_mac *m = kmalloc(sizeof(struct mib_mac), GFP_KERNEL);

	if (!m)
		return;

	ret = at76_get_mib(priv->udev, MIB_MAC, m, sizeof(struct mib_mac));
	if (ret < 0) {
1050
		wiphy_err(priv->hw->wiphy,
1051
			  "at76_get_mib (MAC) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1052 1053 1054 1055 1056 1057 1058 1059
		goto exit;
	}

	at76_dbg(DBG_MIB, "%s: MIB MAC: max_tx_msdu_lifetime %d "
		 "max_rx_lifetime %d frag_threshold %d rts_threshold %d "
		 "cwmin %d cwmax %d short_retry_time %d long_retry_time %d "
		 "scan_type %d scan_channel %d probe_delay %u "
		 "min_channel_time %d max_channel_time %d listen_int %d "
1060
		 "desired_ssid %*phD desired_bssid %pM desired_bsstype %d",
Kalle Valo's avatar
Kalle Valo committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
		 wiphy_name(priv->hw->wiphy),
		 le32_to_cpu(m->max_tx_msdu_lifetime),
		 le32_to_cpu(m->max_rx_lifetime),
		 le16_to_cpu(m->frag_threshold), le16_to_cpu(m->rts_threshold),
		 le16_to_cpu(m->cwmin), le16_to_cpu(m->cwmax),
		 m->short_retry_time, m->long_retry_time, m->scan_type,
		 m->scan_channel, le16_to_cpu(m->probe_delay),
		 le16_to_cpu(m->min_channel_time),
		 le16_to_cpu(m->max_channel_time),
		 le16_to_cpu(m->listen_interval),
1071
		 IW_ESSID_MAX_SIZE, m->desired_ssid,
1072
		 m->desired_bssid, m->desired_bsstype);
Kalle Valo's avatar
Kalle Valo committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
exit:
	kfree(m);
}

static void at76_dump_mib_phy(struct at76_priv *priv)
{
	int ret;
	struct mib_phy *m = kmalloc(sizeof(struct mib_phy), GFP_KERNEL);

	if (!m)
		return;

	ret = at76_get_mib(priv->udev, MIB_PHY, m, sizeof(struct mib_phy));
	if (ret < 0) {
1087
		wiphy_err(priv->hw->wiphy,
1088
			  "at76_get_mib (PHY) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
		goto exit;
	}

	at76_dbg(DBG_MIB, "%s: MIB PHY: ed_threshold %d slot_time %d "
		 "sifs_time %d preamble_length %d plcp_header_length %d "
		 "mpdu_max_length %d cca_mode_supported %d operation_rate_set "
		 "0x%x 0x%x 0x%x 0x%x channel_id %d current_cca_mode %d "
		 "phy_type %d current_reg_domain %d",
		 wiphy_name(priv->hw->wiphy), le32_to_cpu(m->ed_threshold),
		 le16_to_cpu(m->slot_time), le16_to_cpu(m->sifs_time),
		 le16_to_cpu(m->preamble_length),
		 le16_to_cpu(m->plcp_header_length),
		 le16_to_cpu(m->mpdu_max_length),
		 le16_to_cpu(m->cca_mode_supported), m->operation_rate_set[0],
		 m->operation_rate_set[1], m->operation_rate_set[2],
		 m->operation_rate_set[3], m->channel_id, m->current_cca_mode,
		 m->phy_type, m->current_reg_domain);
exit:
	kfree(m);
}

static void at76_dump_mib_local(struct at76_priv *priv)
{
	int ret;
1113
	struct mib_local *m = kmalloc(sizeof(*m), GFP_KERNEL);
Kalle Valo's avatar
Kalle Valo committed
1114 1115 1116 1117

	if (!m)
		return;

1118
	ret = at76_get_mib(priv->udev, MIB_LOCAL, m, sizeof(*m));
Kalle Valo's avatar
Kalle Valo committed
1119
	if (ret < 0) {
1120
		wiphy_err(priv->hw->wiphy,
1121
			  "at76_get_mib (LOCAL) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
		goto exit;
	}

	at76_dbg(DBG_MIB, "%s: MIB LOCAL: beacon_enable %d "
		 "txautorate_fallback %d ssid_size %d promiscuous_mode %d "
		 "preamble_type %d", wiphy_name(priv->hw->wiphy),
		 m->beacon_enable,
		 m->txautorate_fallback, m->ssid_size, m->promiscuous_mode,
		 m->preamble_type);
exit:
	kfree(m);
}

static void at76_dump_mib_mdomain(struct at76_priv *priv)
{
	int ret;
	struct mib_mdomain *m = kmalloc(sizeof(struct mib_mdomain), GFP_KERNEL);

	if (!m)
		return;

	ret = at76_get_mib(priv->udev, MIB_MDOMAIN, m,
			   sizeof(struct mib_mdomain));
	if (ret < 0) {
1146
		wiphy_err(priv->hw->wiphy,
1147
			  "at76_get_mib (MDOMAIN) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1148 1149 1150
		goto exit;
	}

1151
	at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: channel_list %*phD",
Kalle Valo's avatar
Kalle Valo committed
1152
		 wiphy_name(priv->hw->wiphy),
1153
		 (int)sizeof(m->channel_list), m->channel_list);
Kalle Valo's avatar
Kalle Valo committed
1154

1155
	at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: tx_powerlevel %*phD",
Kalle Valo's avatar
Kalle Valo committed
1156
		 wiphy_name(priv->hw->wiphy),
1157
		 (int)sizeof(m->tx_powerlevel), m->tx_powerlevel);
Kalle Valo's avatar
Kalle Valo committed
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
exit:
	kfree(m);
}

/* Enable monitor mode */
static int at76_start_monitor(struct at76_priv *priv)
{
	struct at76_req_scan scan;
	int ret;

	memset(&scan, 0, sizeof(struct at76_req_scan));
	memset(scan.bssid, 0xff, ETH_ALEN);

	scan.channel = priv->channel;
	scan.scan_type = SCAN_TYPE_PASSIVE;
	scan.international_scan = 0;
1174 1175 1176
	scan.min_channel_time = cpu_to_le16(priv->scan_min_time);
	scan.max_channel_time = cpu_to_le16(priv->scan_max_time);
	scan.probe_delay = cpu_to_le16(0);
Kalle Valo's avatar
Kalle Valo committed
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

	ret = at76_set_card_command(priv->udev, CMD_SCAN, &scan, sizeof(scan));
	if (ret >= 0)
		ret = at76_get_cmd_status(priv->udev, CMD_SCAN);

	return ret;
}

/* Calculate padding from txbuf->wlength (which excludes the USB TX header),
   likely to compensate a flaw in the AT76C503A USB part ... */
static inline int at76_calc_padding(int wlen)
{
	/* add the USB TX header */
	wlen += AT76_TX_HDRLEN;

	wlen = wlen % 64;

	if (wlen < 50)
		return 50 - wlen;

	if (wlen >= 61)
		return 64 + 50 - wlen;

	return 0;
}

static void at76_rx_callback(struct urb *urb)
{
	struct at76_priv *priv = urb->context;

	priv->rx_tasklet.data = (unsigned long)urb;
	tasklet_schedule(&priv->rx_tasklet);
}

static int at76_submit_rx_urb(struct at76_priv *priv)
{
	int ret;
	int size;
	struct sk_buff *skb = priv->rx_skb;

	if (!priv->rx_urb) {
1218
		wiphy_err(priv->hw->wiphy, "%s: priv->rx_urb is NULL\n",
1219
			  __func__);
Kalle Valo's avatar
Kalle Valo committed
1220 1221 1222 1223 1224 1225
		return -EFAULT;
	}

	if (!skb) {
		skb = dev_alloc_skb(sizeof(struct at76_rx_buffer));
		if (!skb) {
1226 1227
			wiphy_err(priv->hw->wiphy,
				  "cannot allocate rx skbuff\n");
Kalle Valo's avatar
Kalle Valo committed
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
			ret = -ENOMEM;
			goto exit;
		}
		priv->rx_skb = skb;
	} else {
		skb_push(skb, skb_headroom(skb));
		skb_trim(skb, 0);
	}

	size = skb_tailroom(skb);
	usb_fill_bulk_urb(priv->rx_urb, priv->udev, priv->rx_pipe,
			  skb_put(skb, size), size, at76_rx_callback, priv);
	ret = usb_submit_urb(priv->rx_urb, GFP_ATOMIC);
	if (ret < 0) {
		if (ret == -ENODEV)
			at76_dbg(DBG_DEVSTART,
				 "usb_submit_urb returned -ENODEV");
		else
1246 1247
			wiphy_err(priv->hw->wiphy,
				  "rx, usb_submit_urb failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1248 1249 1250 1251
	}

exit:
	if (ret < 0 && ret != -ENODEV)
1252 1253
		wiphy_err(priv->hw->wiphy,
			  "cannot submit rx urb - please unload the driver and/or power cycle the device\n");
Kalle Valo's avatar
Kalle Valo committed
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275

	return ret;
}

/* Download external firmware */
static int at76_load_external_fw(struct usb_device *udev, struct fwentry *fwe)
{
	int ret;
	int op_mode;
	int blockno = 0;
	int bsize;
	u8 *block;
	u8 *buf = fwe->extfw;
	int size = fwe->extfw_size;

	if (!buf || !size)
		return -ENOENT;

	op_mode = at76_get_op_mode(udev);
	at76_dbg(DBG_DEVSTART, "opmode %d", op_mode);

	if (op_mode != OPMODE_NORMAL_NIC_WITHOUT_FLASH) {
1276
		dev_err(&udev->dev, "unexpected opmode %d\n", op_mode);
Kalle Valo's avatar
Kalle Valo committed
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
		return -EINVAL;
	}

	block = kmalloc(FW_BLOCK_SIZE, GFP_KERNEL);
	if (!block)
		return -ENOMEM;

	at76_dbg(DBG_DEVSTART, "downloading external firmware");

	/* for fw >= 0.100, the device needs an extra empty block */
	do {
		bsize = min_t(int, size, FW_BLOCK_SIZE);
		memcpy(block, buf, bsize);
		at76_dbg(DBG_DEVSTART,
			 "ext fw, size left = %5d, bsize = %4d, blockno = %2d",
			 size, bsize, blockno);
		ret = at76_load_ext_fw_block(udev, blockno, block, bsize);
		if (ret != bsize) {
1295 1296 1297
			dev_err(&udev->dev,
				"loading %dth firmware block failed: %d\n",
				blockno, ret);
1298
			ret = -EIO;
Kalle Valo's avatar
Kalle Valo committed
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
			goto exit;
		}
		buf += bsize;
		size -= bsize;
		blockno++;
	} while (bsize > 0);

	if (at76_is_505a(fwe->board_type)) {
		at76_dbg(DBG_DEVSTART, "200 ms delay for 505a");
		schedule_timeout_interruptible(HZ / 5 + 1);
	}

exit:
	kfree(block);
	if (ret < 0)
1314 1315
		dev_err(&udev->dev,
			"downloading external firmware failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
	return ret;
}

/* Download internal firmware */
static int at76_load_internal_fw(struct usb_device *udev, struct fwentry *fwe)
{
	int ret;
	int need_remap = !at76_is_505a(fwe->board_type);

	ret = at76_usbdfu_download(udev, fwe->intfw, fwe->intfw_size,
				   need_remap ? 0 : 2 * HZ);

	if (ret < 0) {
1329 1330
		dev_err(&udev->dev,
			"downloading internal fw failed with %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1331 1332 1333 1334 1335 1336 1337 1338 1339
		goto exit;
	}

	at76_dbg(DBG_DEVSTART, "sending REMAP");

	/* no REMAP for 505A (see SF driver) */
	if (need_remap) {
		ret = at76_remap(udev);
		if (ret < 0) {
1340 1341
			dev_err(&udev->dev,
				"sending REMAP failed with %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
			goto exit;
		}
	}

	at76_dbg(DBG_DEVSTART, "sleeping for 2 seconds");
	schedule_timeout_interruptible(2 * HZ + 1);
	usb_reset_device(udev);

exit:
	return ret;
}

static int at76_startup_device(struct at76_priv *priv)
{
	struct at76_card_config *ccfg = &priv->card_config;
	int ret;

	at76_dbg(DBG_PARAMS,
1360
		 "%s param: ssid %.*s (%*phD) mode %s ch %d wep %s key %d "
Kalle Valo's avatar
Kalle Valo committed
1361
		 "keylen %d", wiphy_name(priv->hw->wiphy), priv->essid_size,
1362
		 priv->essid, IW_ESSID_MAX_SIZE, priv->essid,
Kalle Valo's avatar
Kalle Valo committed
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
		 priv->iw_mode == IW_MODE_ADHOC ? "adhoc" : "infra",
		 priv->channel, priv->wep_enabled ? "enabled" : "disabled",
		 priv->wep_key_id, priv->wep_keys_len[priv->wep_key_id]);
	at76_dbg(DBG_PARAMS,
		 "%s param: preamble %s rts %d retry %d frag %d "
		 "txrate %s auth_mode %d", wiphy_name(priv->hw->wiphy),
		 preambles[priv->preamble_type], priv->rts_threshold,
		 priv->short_retry_limit, priv->frag_threshold,
		 priv->txrate == TX_RATE_1MBIT ? "1MBit" : priv->txrate ==
		 TX_RATE_2MBIT ? "2MBit" : priv->txrate ==
		 TX_RATE_5_5MBIT ? "5.5MBit" : priv->txrate ==
		 TX_RATE_11MBIT ? "11MBit" : priv->txrate ==
		 TX_RATE_AUTO ? "auto" : "<invalid>", priv->auth_mode);
	at76_dbg(DBG_PARAMS,
		 "%s param: pm_mode %d pm_period %d auth_mode %s "
		 "scan_times %d %d scan_mode %s",
		 wiphy_name(priv->hw->wiphy), priv->pm_mode, priv->pm_period,
		 priv->auth_mode == WLAN_AUTH_OPEN ? "open" : "shared_secret",
		 priv->scan_min_time, priv->scan_max_time,
		 priv->scan_mode == SCAN_TYPE_ACTIVE ? "active" : "passive");

	memset(ccfg, 0, sizeof(struct at76_card_config));
	ccfg->promiscuous_mode = 0;
	ccfg->short_retry_limit = priv->short_retry_limit;

	if (priv->wep_enabled) {
		if (priv->wep_keys_len[priv->wep_key_id] > WEP_SMALL_KEY_LEN)
			ccfg->encryption_type = 2;
		else
			ccfg->encryption_type = 1;

		/* jal: always exclude unencrypted if WEP is active */
		ccfg->exclude_unencrypted = 1;
	} else {
		ccfg->exclude_unencrypted = 0;
		ccfg->encryption_type = 0;
	}

	ccfg->rts_threshold = cpu_to_le16(priv->rts_threshold);
	ccfg->fragmentation_threshold = cpu_to_le16(priv->frag_threshold);

	memcpy(ccfg->basic_rate_set, hw_rates, 4);
	/* jal: really needed, we do a set_mib for autorate later ??? */
	ccfg->auto_rate_fallback = (priv->txrate == TX_RATE_AUTO ? 1 : 0);
	ccfg->channel = priv->channel;
	ccfg->privacy_invoked = priv->wep_enabled;
	memcpy(ccfg->current_ssid, priv->essid, IW_ESSID_MAX_SIZE);
	ccfg->ssid_len = priv->essid_size;

	ccfg->wep_default_key_id = priv->wep_key_id;
	memcpy(ccfg->wep_default_key_value, priv->wep_keys,
	       sizeof(priv->wep_keys));

	ccfg->short_preamble = priv->preamble_type;
	ccfg->beacon_period = cpu_to_le16(priv->beacon_period);

	ret = at76_set_card_command(priv->udev, CMD_STARTUP, &priv->card_config,
				    sizeof(struct at76_card_config));
	if (ret < 0) {
1422 1423
		wiphy_err(priv->hw->wiphy, "at76_set_card_command failed: %d\n",
			  ret);
Kalle Valo's avatar
Kalle Valo committed
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
		return ret;
	}

	at76_wait_completion(priv, CMD_STARTUP);

	/* remove BSSID from previous run */
	memset(priv->bssid, 0, ETH_ALEN);

	if (at76_set_radio(priv, 1) == 1)
		at76_wait_completion(priv, CMD_RADIO_ON);

	ret = at76_set_preamble(priv, priv->preamble_type);
	if (ret < 0)
		return ret;

	ret = at76_set_frag(priv, priv->frag_threshold);
	if (ret < 0)
		return ret;

	ret = at76_set_rts(priv, priv->rts_threshold);
	if (ret < 0)
		return ret;

	ret = at76_set_autorate_fallback(priv,
					 priv->txrate == TX_RATE_AUTO ? 1 : 0);
	if (ret < 0)
		return ret;

	ret = at76_set_pm_mode(priv);
	if (ret < 0)
		return ret;

	if (at76_debug & DBG_MIB) {
		at76_dump_mib_mac(priv);
		at76_dump_mib_mac_addr(priv);
		at76_dump_mib_mac_mgmt(priv);
		at76_dump_mib_mac_wep(priv);
		at76_dump_mib_mdomain(priv);
		at76_dump_mib_phy(priv);
		at76_dump_mib_local(priv);
	}

	return 0;
}

/* Enable or disable promiscuous mode */
static void at76_work_set_promisc(struct work_struct *work)
{
	struct at76_priv *priv = container_of(work, struct at76_priv,
					      work_set_promisc);
	int ret = 0;

1476 1477 1478
	if (priv->device_unplugged)
		return;

Kalle Valo's avatar
Kalle Valo committed
1479 1480 1481 1482 1483 1484 1485 1486 1487
	mutex_lock(&priv->mtx);

	priv->mib_buf.type = MIB_LOCAL;
	priv->mib_buf.size = 1;
	priv->mib_buf.index = offsetof(struct mib_local, promiscuous_mode);
	priv->mib_buf.data.byte = priv->promisc ? 1 : 0;

	ret = at76_set_mib(priv, &priv->mib_buf);
	if (ret < 0)
1488 1489
		wiphy_err(priv->hw->wiphy,
			  "set_mib (promiscuous_mode) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513

	mutex_unlock(&priv->mtx);
}

/* Submit Rx urb back to the device */
static void at76_work_submit_rx(struct work_struct *work)
{
	struct at76_priv *priv = container_of(work, struct at76_priv,
					      work_submit_rx);

	mutex_lock(&priv->mtx);
	at76_submit_rx_urb(priv);
	mutex_unlock(&priv->mtx);
}

static void at76_rx_tasklet(unsigned long param)
{
	struct urb *urb = (struct urb *)param;
	struct at76_priv *priv = urb->context;
	struct at76_rx_buffer *buf;
	struct ieee80211_rx_status rx_status = { 0 };

	if (priv->device_unplugged) {
		at76_dbg(DBG_DEVSTART, "device unplugged");
1514
		at76_dbg(DBG_DEVSTART, "urb status %d", urb->status);
Kalle Valo's avatar
Kalle Valo committed
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
		return;
	}

	if (!priv->rx_skb || !priv->rx_skb->data)
		return;

	buf = (struct at76_rx_buffer *)priv->rx_skb->data;

	if (urb->status != 0) {
		if (urb->status != -ENOENT && urb->status != -ECONNRESET)
			at76_dbg(DBG_URB,
				 "%s %s: - nonzero Rx bulk status received: %d",
				 __func__, wiphy_name(priv->hw->wiphy),
				 urb->status);
		return;
	}

	at76_dbg(DBG_RX_ATMEL_HDR,
		 "%s: rx frame: rate %d rssi %d noise %d link %d",
		 wiphy_name(priv->hw->wiphy), buf->rx_rate, buf->rssi,
		 buf->noise_level, buf->link_quality);

	skb_pull(priv->rx_skb, AT76_RX_HDRLEN);
	skb_trim(priv->rx_skb, le16_to_cpu(buf->wlength));
	at76_dbg_dump(DBG_RX_DATA, priv->rx_skb->data,
		      priv->rx_skb->len, "RX: len=%d", priv->rx_skb->len);

	rx_status.signal = buf->rssi;
	rx_status.flag |= RX_FLAG_DECRYPTED;
	rx_status.flag |= RX_FLAG_IV_STRIPPED;

	at76_dbg(DBG_MAC80211, "calling ieee80211_rx_irqsafe(): %d/%d",
		 priv->rx_skb->len, priv->rx_skb->data_len);
1548 1549
	memcpy(IEEE80211_SKB_RXCB(priv->rx_skb), &rx_status, sizeof(rx_status));
	ieee80211_rx_irqsafe(priv->hw, priv->rx_skb);
Kalle Valo's avatar
Kalle Valo committed
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575

	/* Use a new skb for the next receive */
	priv->rx_skb = NULL;

	at76_submit_rx_urb(priv);
}

/* Load firmware into kernel memory and parse it */
static struct fwentry *at76_load_firmware(struct usb_device *udev,
					  enum board_type board_type)
{
	int ret;
	char *str;
	struct at76_fw_header *fwh;
	struct fwentry *fwe = &firmwares[board_type];

	mutex_lock(&fw_mutex);

	if (fwe->loaded) {
		at76_dbg(DBG_FW, "re-using previously loaded fw");
		goto exit;
	}

	at76_dbg(DBG_FW, "downloading firmware %s", fwe->fwname);
	ret = request_firmware(&fwe->fw, fwe->fwname, &udev->dev);
	if (ret < 0) {
1576 1577 1578 1579
		dev_err(&udev->dev, "firmware %s not found!\n",
			fwe->fwname);
		dev_err(&udev->dev,
			"you may need to download the firmware from http://developer.berlios.de/projects/at76c503a/\n");
Kalle Valo's avatar
Kalle Valo committed
1580 1581 1582 1583 1584 1585 1586
		goto exit;
	}

	at76_dbg(DBG_FW, "got it.");
	fwh = (struct at76_fw_header *)(fwe->fw->data);

	if (fwe->fw->size <= sizeof(*fwh)) {
1587 1588
		dev_err(&udev->dev,
			"firmware is too short (0x%zx)\n", fwe->fw->size);
Kalle Valo's avatar
Kalle Valo committed
1589 1590 1591 1592 1593 1594
		goto exit;
	}

	/* CRC currently not checked */
	fwe->board_type = le32_to_cpu(fwh->board_type);
	if (fwe->board_type != board_type) {
1595 1596 1597
		dev_err(&udev->dev,
			"board type mismatch, requested %u, got %u\n",
			board_type, fwe->board_type);
Kalle Valo's avatar
Kalle Valo committed
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
		goto exit;
	}

	fwe->fw_version.major = fwh->major;
	fwe->fw_version.minor = fwh->minor;
	fwe->fw_version.patch = fwh->patch;
	fwe->fw_version.build = fwh->build;

	str = (char *)fwh + le32_to_cpu(fwh->str_offset);
	fwe->intfw = (u8 *)fwh + le32_to_cpu(fwh->int_fw_offset);
	fwe->intfw_size = le32_to_cpu(fwh->int_fw_len);
	fwe->extfw = (u8 *)fwh + le32_to_cpu(fwh->ext_fw_offset);
	fwe->extfw_size = le32_to_cpu(fwh->ext_fw_len);

	fwe->loaded = 1;

	dev_printk(KERN_DEBUG, &udev->dev,
		   "using firmware %s (version %d.%d.%d-%d)\n",
		   fwe->fwname, fwh->major, fwh->minor, fwh->patch, fwh->build);

	at76_dbg(DBG_DEVSTART, "board %u, int %d:%d, ext %d:%d", board_type,
		 le32_to_cpu(fwh->int_fw_offset), le32_to_cpu(fwh->int_fw_len),
		 le32_to_cpu(fwh->ext_fw_offset), le32_to_cpu(fwh->ext_fw_len));
	at76_dbg(DBG_DEVSTART, "firmware id %s", str);

exit:
	mutex_unlock(&fw_mutex);

	if (fwe->loaded)
		return fwe;
	else
		return NULL;
}

1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
static int at76_join(struct at76_priv *priv)
{
	struct at76_req_join join;
	int ret;

	memset(&join, 0, sizeof(struct at76_req_join));
	memcpy(join.essid, priv->essid, priv->essid_size);
	join.essid_size = priv->essid_size;
	memcpy(join.bssid, priv->bssid, ETH_ALEN);
	join.bss_type = INFRASTRUCTURE_MODE;
	join.channel = priv->channel;
	join.timeout = cpu_to_le16(2000);

	at76_dbg(DBG_MAC80211, "%s: sending CMD_JOIN", __func__);
	ret = at76_set_card_command(priv->udev, CMD_JOIN, &join,
				    sizeof(struct at76_req_join));

	if (ret < 0) {
1650 1651
		wiphy_err(priv->hw->wiphy, "at76_set_card_command failed: %d\n",
			  ret);
1652 1653 1654 1655 1656 1657
		return 0;
	}

	ret = at76_wait_completion(priv, CMD_JOIN);
	at76_dbg(DBG_MAC80211, "%s: CMD_JOIN returned: 0x%02x", __func__, ret);
	if (ret != CMD_STATUS_COMPLETE) {
1658 1659
		wiphy_err(priv->hw->wiphy, "at76_wait_completion failed: %d\n",
			  ret);
1660 1661 1662 1663 1664 1665 1666 1667
		return 0;
	}

	at76_set_pm_mode(priv);

	return 0;
}

1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
static void at76_work_join_bssid(struct work_struct *work)
{
	struct at76_priv *priv = container_of(work, struct at76_priv,
					      work_join_bssid);

	if (priv->device_unplugged)
		return;

	mutex_lock(&priv->mtx);

	if (is_valid_ether_addr(priv->bssid))
		at76_join(priv);

	mutex_unlock(&priv->mtx);
}

Kalle Valo's avatar
Kalle Valo committed
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
static void at76_mac80211_tx_callback(struct urb *urb)
{
	struct at76_priv *priv = urb->context;
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(priv->tx_skb);

	at76_dbg(DBG_MAC80211, "%s()", __func__);

	switch (urb->status) {
	case 0:
		/* success */
		info->flags |= IEEE80211_TX_STAT_ACK;
		break;
	case -ENOENT:
	case -ECONNRESET:
		/* fail, urb has been unlinked */
		/* FIXME: add error message */
		break;
	default:
		at76_dbg(DBG_URB, "%s - nonzero tx status received: %d",
			 __func__, urb->status);
		break;
	}

	memset(&info->status, 0, sizeof(info->status));

	ieee80211_tx_status_irqsafe(priv->hw, priv->tx_skb);

	priv->tx_skb = NULL;

	ieee80211_wake_queues(priv->hw);
}

1716 1717 1718
static void at76_mac80211_tx(struct ieee80211_hw *hw,
			     struct ieee80211_tx_control *control,
			     struct sk_buff *skb)
Kalle Valo's avatar
Kalle Valo committed
1719 1720 1721 1722
{
	struct at76_priv *priv = hw->priv;
	struct at76_tx_buffer *tx_buffer = priv->bulk_out_buffer;
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1723
	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
Kalle Valo's avatar
Kalle Valo committed
1724 1725 1726 1727 1728
	int padding, submit_len, ret;

	at76_dbg(DBG_MAC80211, "%s()", __func__);

	if (priv->tx_urb->status == -EINPROGRESS) {
1729 1730
		wiphy_err(priv->hw->wiphy,
			  "%s called while tx urb is pending\n", __func__);
1731 1732
		dev_kfree_skb_any(skb);
		return;
Kalle Valo's avatar
Kalle Valo committed
1733 1734
	}

1735 1736 1737 1738 1739 1740 1741 1742
	/* The following code lines are important when the device is going to
	 * authenticate with a new bssid. The driver must send CMD_JOIN before
	 * an authentication frame is transmitted. For this to succeed, the
	 * correct bssid of the AP must be known. As mac80211 does not inform
	 * drivers about the bssid prior to the authentication process the
	 * following workaround is necessary. If the TX frame is an
	 * authentication frame extract the bssid and send the CMD_JOIN. */
	if (mgmt->frame_control & cpu_to_le16(IEEE80211_STYPE_AUTH)) {
1743
		if (!ether_addr_equal_64bits(priv->bssid, mgmt->bssid)) {
1744 1745
			memcpy(priv->bssid, mgmt->bssid, ETH_ALEN);
			ieee80211_queue_work(hw, &priv->work_join_bssid);
1746 1747
			dev_kfree_skb_any(skb);
			return;
1748 1749 1750
		}
	}

Kalle Valo's avatar
Kalle Valo committed
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
	ieee80211_stop_queues(hw);

	at76_ledtrig_tx_activity();	/* tell ledtrigger we send a packet */

	WARN_ON(priv->tx_skb != NULL);

	priv->tx_skb = skb;
	padding = at76_calc_padding(skb->len);
	submit_len = AT76_TX_HDRLEN + skb->len + padding;

	/* setup 'Atmel' header */
	memset(tx_buffer, 0, sizeof(*tx_buffer));
	tx_buffer->padding = padding;
	tx_buffer->wlength = cpu_to_le16(skb->len);
	tx_buffer->tx_rate = ieee80211_get_tx_rate(hw, info)->hw_value;
	memset(tx_buffer->reserved, 0, sizeof(tx_buffer->reserved));
	memcpy(tx_buffer->packet, skb->data, skb->len);

	at76_dbg(DBG_TX_DATA, "%s tx: wlen 0x%x pad 0x%x rate %d hdr",
		 wiphy_name(priv->hw->wiphy), le16_to_cpu(tx_buffer->wlength),
		 tx_buffer->padding, tx_buffer->tx_rate);

	/* send stuff */
	at76_dbg_dump(DBG_TX_DATA_CONTENT, tx_buffer, submit_len,
		      "%s(): tx_buffer %d bytes:", __func__, submit_len);
	usb_fill_bulk_urb(priv->tx_urb, priv->udev, priv->tx_pipe, tx_buffer,
			  submit_len, at76_mac80211_tx_callback, priv);
	ret = usb_submit_urb(priv->tx_urb, GFP_ATOMIC);
	if (ret) {
1780
		wiphy_err(priv->hw->wiphy, "error in tx submit urb: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1781
		if (ret == -EINVAL)
1782
			wiphy_err(priv->hw->wiphy,
1783
				  "-EINVAL: tx urb %p hcpriv %p complete %p\n",
1784 1785
				  priv->tx_urb,
				  priv->tx_urb->hcpriv, priv->tx_urb->complete);
Kalle Valo's avatar
Kalle Valo committed
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
	}
}

static int at76_mac80211_start(struct ieee80211_hw *hw)
{
	struct at76_priv *priv = hw->priv;
	int ret;

	at76_dbg(DBG_MAC80211, "%s()", __func__);

	mutex_lock(&priv->mtx);

	ret = at76_submit_rx_urb(priv);
	if (ret < 0) {
1800 1801
		wiphy_err(priv->hw->wiphy, "open: submit_rx_urb failed: %d\n",
			  ret);
Kalle Valo's avatar
Kalle Valo committed
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
		goto error;
	}

	at76_startup_device(priv);

	at76_start_monitor(priv);

error:
	mutex_unlock(&priv->mtx);

	return 0;
}

static void at76_mac80211_stop(struct ieee80211_hw *hw)
{
	struct at76_priv *priv = hw->priv;

	at76_dbg(DBG_MAC80211, "%s()", __func__);

1821
	cancel_delayed_work(&priv->dwork_hw_scan);
1822
	cancel_work_sync(&priv->work_join_bssid);
1823 1824
	cancel_work_sync(&priv->work_set_promisc);

Kalle Valo's avatar
Kalle Valo committed
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
	mutex_lock(&priv->mtx);

	if (!priv->device_unplugged) {
		/* We are called by "ifconfig ethX down", not because the
		 * device is not available anymore. */
		at76_set_radio(priv, 0);

		/* We unlink rx_urb because at76_open() re-submits it.
		 * If unplugged, at76_delete_device() takes care of it. */
		usb_kill_urb(priv->rx_urb);
	}

	mutex_unlock(&priv->mtx);
}

static int at76_add_interface(struct ieee80211_hw *hw,
1841
			      struct ieee80211_vif *vif)
Kalle Valo's avatar
Kalle Valo committed
1842 1843 1844 1845 1846 1847 1848 1849
{
	struct at76_priv *priv = hw->priv;
	int ret = 0;

	at76_dbg(DBG_MAC80211, "%s()", __func__);

	mutex_lock(&priv->mtx);

1850
	switch (vif->type) {
Kalle Valo's avatar
Kalle Valo committed
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
	case NL80211_IFTYPE_STATION:
		priv->iw_mode = IW_MODE_INFRA;
		break;
	default:
		ret = -EOPNOTSUPP;
		goto exit;
	}

exit:
	mutex_unlock(&priv->mtx);

	return ret;
}

static void at76_remove_interface(struct ieee80211_hw *hw,
1866
				  struct ieee80211_vif *vif)
Kalle Valo's avatar
Kalle Valo committed
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
{
	at76_dbg(DBG_MAC80211, "%s()", __func__);
}

static void at76_dwork_hw_scan(struct work_struct *work)
{
	struct at76_priv *priv = container_of(work, struct at76_priv,
					      dwork_hw_scan.work);
	int ret;

1877 1878 1879
	if (priv->device_unplugged)
		return;

Kalle Valo's avatar
Kalle Valo committed
1880 1881 1882 1883 1884 1885 1886 1887
	mutex_lock(&priv->mtx);

	ret = at76_get_cmd_status(priv->udev, CMD_SCAN);
	at76_dbg(DBG_MAC80211, "%s: CMD_SCAN status 0x%02x", __func__, ret);

	/* FIXME: add maximum time for scan to complete */

	if (ret != CMD_STATUS_COMPLETE) {
1888 1889
		ieee80211_queue_delayed_work(priv->hw, &priv->dwork_hw_scan,
					     SCAN_POLL_INTERVAL);
1890 1891
		mutex_unlock(&priv->mtx);
		return;
Kalle Valo's avatar
Kalle Valo committed
1892 1893 1894 1895 1896 1897
	}

	if (is_valid_ether_addr(priv->bssid))
		at76_join(priv);

	mutex_unlock(&priv->mtx);
1898 1899 1900 1901

	ieee80211_scan_completed(priv->hw, false);

	ieee80211_wake_queues(priv->hw);
Kalle Valo's avatar
Kalle Valo committed
1902 1903
}

1904
static int at76_hw_scan(struct ieee80211_hw *hw,
1905
			struct ieee80211_vif *vif,
1906
			struct cfg80211_scan_request *req)
Kalle Valo's avatar
Kalle Valo committed
1907 1908 1909
{
	struct at76_priv *priv = hw->priv;
	struct at76_req_scan scan;
1910 1911
	u8 *ssid = NULL;
	int ret, len = 0;
Kalle Valo's avatar
Kalle Valo committed
1912 1913 1914

	at76_dbg(DBG_MAC80211, "%s():", __func__);

1915 1916 1917
	if (priv->device_unplugged)
		return 0;

Kalle Valo's avatar
Kalle Valo committed
1918 1919 1920 1921 1922 1923
	mutex_lock(&priv->mtx);

	ieee80211_stop_queues(hw);

	memset(&scan, 0, sizeof(struct at76_req_scan));
	memset(scan.bssid, 0xFF, ETH_ALEN);
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933

	if (req->n_ssids) {
		scan.scan_type = SCAN_TYPE_ACTIVE;
		ssid = req->ssids[0].ssid;
		len = req->ssids[0].ssid_len;
	} else {
		scan.scan_type = SCAN_TYPE_PASSIVE;
	}

	if (len) {
Kalle Valo's avatar
Kalle Valo committed
1934 1935 1936
		memcpy(scan.essid, ssid, len);
		scan.essid_size = len;
	}
1937

Kalle Valo's avatar
Kalle Valo committed
1938 1939 1940 1941 1942 1943 1944 1945 1946
	scan.min_channel_time = cpu_to_le16(priv->scan_min_time);
	scan.max_channel_time = cpu_to_le16(priv->scan_max_time);
	scan.probe_delay = cpu_to_le16(priv->scan_min_time * 1000);
	scan.international_scan = 0;

	at76_dbg(DBG_MAC80211, "%s: sending CMD_SCAN", __func__);
	ret = at76_set_card_command(priv->udev, CMD_SCAN, &scan, sizeof(scan));

	if (ret < 0) {
1947
		wiphy_err(priv->hw->wiphy, "CMD_SCAN failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1948 1949 1950
		goto exit;
	}

1951 1952
	ieee80211_queue_delayed_work(priv->hw, &priv->dwork_hw_scan,
				     SCAN_POLL_INTERVAL);
Kalle Valo's avatar
Kalle Valo committed
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963

exit:
	mutex_unlock(&priv->mtx);

	return 0;
}

static int at76_config(struct ieee80211_hw *hw, u32 changed)
{
	struct at76_priv *priv = hw->priv;

1964
	at76_dbg(DBG_MAC80211, "%s(): channel %d",
1965
		 __func__, hw->conf.chandef.chan->hw_value);
Kalle Valo's avatar
Kalle Valo committed
1966 1967 1968 1969
	at76_dbg_dump(DBG_MAC80211, priv->bssid, ETH_ALEN, "bssid:");

	mutex_lock(&priv->mtx);

1970
	priv->channel = hw->conf.chandef.chan->hw_value;
Kalle Valo's avatar
Kalle Valo committed
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981

	if (is_valid_ether_addr(priv->bssid))
		at76_join(priv);
	else
		at76_start_monitor(priv);

	mutex_unlock(&priv->mtx);

	return 0;
}

1982 1983 1984 1985
static void at76_bss_info_changed(struct ieee80211_hw *hw,
				  struct ieee80211_vif *vif,
				  struct ieee80211_bss_conf *conf,
				  u32 changed)
Kalle Valo's avatar
Kalle Valo committed
1986 1987 1988 1989
{
	struct at76_priv *priv = hw->priv;

	at76_dbg(DBG_MAC80211, "%s():", __func__);
1990 1991 1992 1993

	if (!(changed & BSS_CHANGED_BSSID))
		return;

Kalle Valo's avatar
Kalle Valo committed
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
	at76_dbg_dump(DBG_MAC80211, conf->bssid, ETH_ALEN, "bssid:");

	mutex_lock(&priv->mtx);

	memcpy(priv->bssid, conf->bssid, ETH_ALEN);

	if (is_valid_ether_addr(priv->bssid))
		/* mac80211 is joining a bss */
		at76_join(priv);

	mutex_unlock(&priv->mtx);
}

/* must be atomic */
static void at76_configure_filter(struct ieee80211_hw *hw,
				  unsigned int changed_flags,
2010
				  unsigned int *total_flags, u64 multicast)
Kalle Valo's avatar
Kalle Valo committed
2011 2012 2013 2014 2015
{
	struct at76_priv *priv = hw->priv;
	int flags;

	at76_dbg(DBG_MAC80211, "%s(): changed_flags=0x%08x "
2016 2017
		 "total_flags=0x%08x",
		 __func__, changed_flags, *total_flags);
Kalle Valo's avatar
Kalle Valo committed
2018 2019 2020 2021

	flags = changed_flags & AT76_SUPPORTED_FILTERS;
	*total_flags = AT76_SUPPORTED_FILTERS;

2022 2023 2024 2025
	/* Bail out after updating flags to prevent a WARN_ON in mac80211. */
	if (priv->device_unplugged)
		return;

Kalle Valo's avatar
Kalle Valo committed
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
	/* FIXME: access to priv->promisc should be protected with
	 * priv->mtx, but it's impossible because this function needs to be
	 * atomic */

	if (flags && !priv->promisc) {
		/* mac80211 wants us to enable promiscuous mode */
		priv->promisc = 1;
	} else if (!flags && priv->promisc) {
		/* we need to disable promiscuous mode */
		priv->promisc = 0;
	} else
		return;

2039
	ieee80211_queue_work(hw, &priv->work_set_promisc);
Kalle Valo's avatar
Kalle Valo committed
2040 2041
}

2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
static int at76_set_wep(struct at76_priv *priv)
{
	int ret = 0;
	struct mib_mac_wep *mib_data = &priv->mib_buf.data.wep_mib;

	priv->mib_buf.type = MIB_MAC_WEP;
	priv->mib_buf.size = sizeof(struct mib_mac_wep);
	priv->mib_buf.index = 0;

	memset(mib_data, 0, sizeof(*mib_data));

	if (priv->wep_enabled) {
		if (priv->wep_keys_len[priv->wep_key_id] > WEP_SMALL_KEY_LEN)
			mib_data->encryption_level = 2;
		else
			mib_data->encryption_level = 1;

		/* always exclude unencrypted if WEP is active */
		mib_data->exclude_unencrypted = 1;
	} else {
		mib_data->exclude_unencrypted = 0;
		mib_data->encryption_level = 0;
	}

	mib_data->privacy_invoked = priv->wep_enabled;
	mib_data->wep_default_key_id = priv->wep_key_id;
	memcpy(mib_data->wep_default_keyvalue, priv->wep_keys,
	       sizeof(priv->wep_keys));

	ret = at76_set_mib(priv, &priv->mib_buf);

	if (ret < 0)
		wiphy_err(priv->hw->wiphy,
			  "set_mib (wep) failed: %d\n", ret);

	return ret;
}

Kalle Valo's avatar
Kalle Valo committed
2080 2081 2082 2083 2084 2085 2086 2087
static int at76_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
			struct ieee80211_vif *vif, struct ieee80211_sta *sta,
			struct ieee80211_key_conf *key)
{
	struct at76_priv *priv = hw->priv;

	int i;

2088
	at76_dbg(DBG_MAC80211, "%s(): cmd %d key->cipher %d key->keyidx %d "
Kalle Valo's avatar
Kalle Valo committed
2089
		 "key->keylen %d",
2090
		 __func__, cmd, key->cipher, key->keyidx, key->keylen);
Kalle Valo's avatar
Kalle Valo committed
2091

2092 2093
	if ((key->cipher != WLAN_CIPHER_SUITE_WEP40) &&
	    (key->cipher != WLAN_CIPHER_SUITE_WEP104))
Kalle Valo's avatar
Kalle Valo committed
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
		return -EOPNOTSUPP;

	key->hw_key_idx = key->keyidx;

	mutex_lock(&priv->mtx);

	switch (cmd) {
	case SET_KEY:
		memcpy(priv->wep_keys[key->keyidx], key->key, key->keylen);
		priv->wep_keys_len[key->keyidx] = key->keylen;

		/* FIXME: find out how to do this properly */
		priv->wep_key_id = key->keyidx;

		break;
	case DISABLE_KEY:
	default:
		priv->wep_keys_len[key->keyidx] = 0;
		break;
	}

	priv->wep_enabled = 0;

	for (i = 0; i < WEP_KEYS; i++) {
		if (priv->wep_keys_len[i] != 0)
			priv->wep_enabled = 1;
	}

2122
	at76_set_wep(priv);
Kalle Valo's avatar
Kalle Valo committed
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133

	mutex_unlock(&priv->mtx);

	return 0;
}

static const struct ieee80211_ops at76_ops = {
	.tx = at76_mac80211_tx,
	.add_interface = at76_add_interface,
	.remove_interface = at76_remove_interface,
	.config = at76_config,
2134
	.bss_info_changed = at76_bss_info_changed,
Kalle Valo's avatar
Kalle Valo committed
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
	.configure_filter = at76_configure_filter,
	.start = at76_mac80211_start,
	.stop = at76_mac80211_stop,
	.hw_scan = at76_hw_scan,
	.set_key = at76_set_key,
};

/* Allocate network device and initialize private data */
static struct at76_priv *at76_alloc_new_device(struct usb_device *udev)
{
	struct ieee80211_hw *hw;
	struct at76_priv *priv;

	hw = ieee80211_alloc_hw(sizeof(struct at76_priv), &at76_ops);
	if (!hw) {
		printk(KERN_ERR DRIVER_NAME ": could not register"
		       " ieee80211_hw\n");
		return NULL;
	}

	priv = hw->priv;
	priv->hw = hw;

	priv->udev = udev;

	mutex_init(&priv->mtx);
	INIT_WORK(&priv->work_set_promisc, at76_work_set_promisc);
	INIT_WORK(&priv->work_submit_rx, at76_work_submit_rx);
2163
	INIT_WORK(&priv->work_join_bssid, at76_work_join_bssid);
Kalle Valo's avatar
Kalle Valo committed
2164 2165
	INIT_DELAYED_WORK(&priv->dwork_hw_scan, at76_dwork_hw_scan);

2166
	tasklet_init(&priv->rx_tasklet, at76_rx_tasklet, 0);
Kalle Valo's avatar
Kalle Valo committed
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206

	priv->pm_mode = AT76_PM_OFF;
	priv->pm_period = 0;

	/* unit us */

	return priv;
}

static int at76_alloc_urbs(struct at76_priv *priv,
			   struct usb_interface *interface)
{
	struct usb_endpoint_descriptor *endpoint, *ep_in, *ep_out;
	int i;
	int buffer_size;
	struct usb_host_interface *iface_desc;

	at76_dbg(DBG_PROC_ENTRY, "%s: ENTER", __func__);

	at76_dbg(DBG_URB, "%s: NumEndpoints %d ", __func__,
		 interface->altsetting[0].desc.bNumEndpoints);

	ep_in = NULL;
	ep_out = NULL;
	iface_desc = interface->cur_altsetting;
	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
		endpoint = &iface_desc->endpoint[i].desc;

		at76_dbg(DBG_URB, "%s: %d. endpoint: addr 0x%x attr 0x%x",
			 __func__, i, endpoint->bEndpointAddress,
			 endpoint->bmAttributes);

		if (!ep_in && usb_endpoint_is_bulk_in(endpoint))
			ep_in = endpoint;

		if (!ep_out && usb_endpoint_is_bulk_out(endpoint))
			ep_out = endpoint;
	}

	if (!ep_in || !ep_out) {
2207
		dev_err(&interface->dev, "bulk endpoints missing\n");
Kalle Valo's avatar
Kalle Valo committed
2208 2209 2210 2211 2212 2213 2214 2215 2216
		return -ENXIO;
	}

	priv->rx_pipe = usb_rcvbulkpipe(priv->udev, ep_in->bEndpointAddress);
	priv->tx_pipe = usb_sndbulkpipe(priv->udev, ep_out->bEndpointAddress);

	priv->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
	priv->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!priv->rx_urb || !priv->tx_urb) {
2217
		dev_err(&interface->dev, "cannot allocate URB\n");
Kalle Valo's avatar
Kalle Valo committed
2218 2219 2220 2221 2222
		return -ENOMEM;
	}

	buffer_size = sizeof(struct at76_tx_buffer) + MAX_PADDING_SIZE;
	priv->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
2223
	if (!priv->bulk_out_buffer)
Kalle Valo's avatar
Kalle Valo committed
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265
		return -ENOMEM;

	at76_dbg(DBG_PROC_ENTRY, "%s: EXIT", __func__);

	return 0;
}

static struct ieee80211_rate at76_rates[] = {
	{ .bitrate = 10, .hw_value = TX_RATE_1MBIT, },
	{ .bitrate = 20, .hw_value = TX_RATE_2MBIT, },
	{ .bitrate = 55, .hw_value = TX_RATE_5_5MBIT, },
	{ .bitrate = 110, .hw_value = TX_RATE_11MBIT, },
};

static struct ieee80211_channel at76_channels[] = {
	{ .center_freq = 2412, .hw_value = 1 },
	{ .center_freq = 2417, .hw_value = 2 },
	{ .center_freq = 2422, .hw_value = 3 },
	{ .center_freq = 2427, .hw_value = 4 },
	{ .center_freq = 2432, .hw_value = 5 },
	{ .center_freq = 2437, .hw_value = 6 },
	{ .center_freq = 2442, .hw_value = 7 },
	{ .center_freq = 2447, .hw_value = 8 },
	{ .center_freq = 2452, .hw_value = 9 },
	{ .center_freq = 2457, .hw_value = 10 },
	{ .center_freq = 2462, .hw_value = 11 },
	{ .center_freq = 2467, .hw_value = 12 },
	{ .center_freq = 2472, .hw_value = 13 },
	{ .center_freq = 2484, .hw_value = 14 }
};

static struct ieee80211_supported_band at76_supported_band = {
	.channels = at76_channels,
	.n_channels = ARRAY_SIZE(at76_channels),
	.bitrates = at76_rates,
	.n_bitrates = ARRAY_SIZE(at76_rates),
};

/* Register network device and initialize the hardware */
static int at76_init_new_device(struct at76_priv *priv,
				struct usb_interface *interface)
{
2266 2267
	struct wiphy *wiphy;
	size_t len;
Kalle Valo's avatar
Kalle Valo committed
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
	int ret;

	/* set up the endpoint information */
	/* check out the endpoints */

	at76_dbg(DBG_DEVSTART, "USB interface: %d endpoints",
		 interface->cur_altsetting->desc.bNumEndpoints);

	ret = at76_alloc_urbs(priv, interface);
	if (ret < 0)
		goto exit;

	/* MAC address */
	ret = at76_get_hw_config(priv);
	if (ret < 0) {
2283
		dev_err(&interface->dev, "cannot get MAC address\n");
Kalle Valo's avatar
Kalle Valo committed
2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
		goto exit;
	}

	priv->domain = at76_get_reg_domain(priv->regulatory_domain);

	priv->channel = DEF_CHANNEL;
	priv->iw_mode = IW_MODE_INFRA;
	priv->rts_threshold = DEF_RTS_THRESHOLD;
	priv->frag_threshold = DEF_FRAG_THRESHOLD;
	priv->short_retry_limit = DEF_SHORT_RETRY_LIMIT;
	priv->txrate = TX_RATE_AUTO;
	priv->preamble_type = PREAMBLE_TYPE_LONG;
	priv->beacon_period = 100;
	priv->auth_mode = WLAN_AUTH_OPEN;
	priv->scan_min_time = DEF_SCAN_MIN_TIME;
	priv->scan_max_time = DEF_SCAN_MAX_TIME;
	priv->scan_mode = SCAN_TYPE_ACTIVE;
2301
	priv->device_unplugged = 0;
Kalle Valo's avatar
Kalle Valo committed
2302 2303

	/* mac80211 initialisation */
2304
	wiphy = priv->hw->wiphy;
2305
	priv->hw->wiphy->max_scan_ssids = 1;
2306
	priv->hw->wiphy->max_scan_ie_len = 0;
Kalle Valo's avatar
Kalle Valo committed
2307 2308 2309 2310
	priv->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
	priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &at76_supported_band;
	priv->hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
			  IEEE80211_HW_SIGNAL_UNSPEC;
2311
	priv->hw->max_signal = 100;
Kalle Valo's avatar
Kalle Valo committed
2312 2313 2314 2315

	SET_IEEE80211_DEV(priv->hw, &interface->dev);
	SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);

2316 2317 2318 2319 2320 2321 2322
	len = sizeof(wiphy->fw_version);
	snprintf(wiphy->fw_version, len, "%d.%d.%d-%d",
		 priv->fw_version.major, priv->fw_version.minor,
		 priv->fw_version.patch, priv->fw_version.build);

	wiphy->hw_version = priv->board_type;

Kalle Valo's avatar
Kalle Valo committed
2323 2324 2325 2326 2327 2328 2329 2330 2331
	ret = ieee80211_register_hw(priv->hw);
	if (ret) {
		printk(KERN_ERR "cannot register mac80211 hw (status %d)!\n",
		       ret);
		goto exit;
	}

	priv->mac80211_registered = 1;

2332
	wiphy_info(priv->hw->wiphy, "USB %s, MAC %pM, firmware %d.%d.%d-%d\n",
2333 2334 2335 2336 2337
		   dev_name(&interface->dev), priv->mac_addr,
		   priv->fw_version.major, priv->fw_version.minor,
		   priv->fw_version.patch, priv->fw_version.build);
	wiphy_info(priv->hw->wiphy, "regulatory domain 0x%02x: %s\n",
		   priv->regulatory_domain, priv->domain->name);
Kalle Valo's avatar
Kalle Valo committed
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349

exit:
	return ret;
}

static void at76_delete_device(struct at76_priv *priv)
{
	at76_dbg(DBG_PROC_ENTRY, "%s: ENTER", __func__);

	/* The device is gone, don't bother turning it off */
	priv->device_unplugged = 1;

2350
	tasklet_kill(&priv->rx_tasklet);
Kalle Valo's avatar
Kalle Valo committed
2351

2352
	if (priv->mac80211_registered)
2353
		ieee80211_unregister_hw(priv->hw);
Kalle Valo's avatar
Kalle Valo committed
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365

	if (priv->tx_urb) {
		usb_kill_urb(priv->tx_urb);
		usb_free_urb(priv->tx_urb);
	}
	if (priv->rx_urb) {
		usb_kill_urb(priv->rx_urb);
		usb_free_urb(priv->rx_urb);
	}

	at76_dbg(DBG_PROC_ENTRY, "%s: unlinked urbs", __func__);

2366 2367
	kfree(priv->bulk_out_buffer);

2368 2369
	del_timer_sync(&ledtrig_tx_timer);

2370
	kfree_skb(priv->rx_skb);
Kalle Valo's avatar
Kalle Valo committed
2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389

	usb_put_dev(priv->udev);

	at76_dbg(DBG_PROC_ENTRY, "%s: before freeing priv/ieee80211_hw",
		 __func__);
	ieee80211_free_hw(priv->hw);

	at76_dbg(DBG_PROC_ENTRY, "%s: EXIT", __func__);
}

static int at76_probe(struct usb_interface *interface,
		      const struct usb_device_id *id)
{
	int ret;
	struct at76_priv *priv;
	struct fwentry *fwe;
	struct usb_device *udev;
	int op_mode;
	int need_ext_fw = 0;
2390
	struct mib_fw_version *fwv = NULL;
Kalle Valo's avatar
Kalle Valo committed
2391 2392 2393 2394
	int board_type = (int)id->driver_info;

	udev = usb_get_dev(interface_to_usbdev(interface));

2395 2396 2397 2398 2399 2400
	fwv = kmalloc(sizeof(*fwv), GFP_KERNEL);
	if (!fwv) {
		ret = -ENOMEM;
		goto exit;
	}

Kalle Valo's avatar
Kalle Valo committed
2401 2402 2403 2404
	/* Load firmware into kernel memory */
	fwe = at76_load_firmware(udev, board_type);
	if (!fwe) {
		ret = -ENOENT;
2405
		goto exit;
Kalle Valo's avatar
Kalle Valo committed
2406 2407 2408 2409 2410 2411 2412 2413 2414 2415
	}

	op_mode = at76_get_op_mode(udev);

	at76_dbg(DBG_DEVSTART, "opmode %d", op_mode);

	/* we get OPMODE_NONE with 2.4.23, SMC2662W-AR ???
	   we get 204 with 2.4.23, Fiberline FL-WL240u (505A+RFMD2958) ??? */

	if (op_mode == OPMODE_HW_CONFIG_MODE) {
2416 2417
		dev_err(&interface->dev,
			"cannot handle a device in HW_CONFIG_MODE\n");
Kalle Valo's avatar
Kalle Valo committed
2418
		ret = -EBUSY;
2419
		goto exit;
Kalle Valo's avatar
Kalle Valo committed
2420 2421 2422 2423 2424 2425 2426 2427 2428
	}

	if (op_mode != OPMODE_NORMAL_NIC_WITH_FLASH
	    && op_mode != OPMODE_NORMAL_NIC_WITHOUT_FLASH) {
		/* download internal firmware part */
		dev_printk(KERN_DEBUG, &interface->dev,
			   "downloading internal firmware\n");
		ret = at76_load_internal_fw(udev, fwe);
		if (ret < 0) {
2429 2430 2431
			dev_err(&interface->dev,
				"error %d downloading internal firmware\n",
				ret);
2432
			goto exit;
Kalle Valo's avatar
Kalle Valo committed
2433 2434
		}
		usb_put_dev(udev);
2435
		goto exit;
Kalle Valo's avatar
Kalle Valo committed
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
	}

	/* Internal firmware already inside the device.  Get firmware
	 * version to test if external firmware is loaded.
	 * This works only for newer firmware, e.g. the Intersil 0.90.x
	 * says "control timeout on ep0in" and subsequent
	 * at76_get_op_mode() fail too :-( */

	/* if version >= 0.100.x.y or device with built-in flash we can
	 * query the device for the fw version */
	if ((fwe->fw_version.major > 0 || fwe->fw_version.minor >= 100)
	    || (op_mode == OPMODE_NORMAL_NIC_WITH_FLASH)) {
2448 2449
		ret = at76_get_mib(udev, MIB_FW_VERSION, fwv, sizeof(*fwv));
		if (ret < 0 || (fwv->major | fwv->minor) == 0)
Kalle Valo's avatar
Kalle Valo committed
2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
			need_ext_fw = 1;
	} else
		/* No way to check firmware version, reload to be sure */
		need_ext_fw = 1;

	if (need_ext_fw) {
		dev_printk(KERN_DEBUG, &interface->dev,
			   "downloading external firmware\n");

		ret = at76_load_external_fw(udev, fwe);
2460 2461
		if (ret < 0)
			goto exit;
Kalle Valo's avatar
Kalle Valo committed
2462 2463

		/* Re-check firmware version */
2464
		ret = at76_get_mib(udev, MIB_FW_VERSION, fwv, sizeof(*fwv));
Kalle Valo's avatar
Kalle Valo committed
2465
		if (ret < 0) {
2466 2467
			dev_err(&interface->dev,
				"error %d getting firmware version\n", ret);
2468
			goto exit;
Kalle Valo's avatar
Kalle Valo committed
2469 2470 2471 2472 2473 2474
		}
	}

	priv = at76_alloc_new_device(udev);
	if (!priv) {
		ret = -ENOMEM;
2475
		goto exit;
Kalle Valo's avatar
Kalle Valo committed
2476 2477 2478 2479
	}

	usb_set_intfdata(interface, priv);

2480
	memcpy(&priv->fw_version, fwv, sizeof(struct mib_fw_version));
Kalle Valo's avatar
Kalle Valo committed
2481 2482 2483 2484 2485 2486
	priv->board_type = board_type;

	ret = at76_init_new_device(priv, interface);
	if (ret < 0)
		at76_delete_device(priv);

2487 2488 2489 2490
exit:
	kfree(fwv);
	if (ret < 0)
		usb_put_dev(udev);
Kalle Valo's avatar
Kalle Valo committed
2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504
	return ret;
}

static void at76_disconnect(struct usb_interface *interface)
{
	struct at76_priv *priv;

	priv = usb_get_intfdata(interface);
	usb_set_intfdata(interface, NULL);

	/* Disconnect after loading internal firmware */
	if (!priv)
		return;

2505
	wiphy_info(priv->hw->wiphy, "disconnecting\n");
Kalle Valo's avatar
Kalle Valo committed
2506
	at76_delete_device(priv);
2507
	dev_info(&interface->dev, "disconnected\n");
Kalle Valo's avatar
Kalle Valo committed
2508 2509 2510 2511 2512 2513 2514 2515
}

/* Structure for registering this driver with the USB subsystem */
static struct usb_driver at76_driver = {
	.name = DRIVER_NAME,
	.probe = at76_probe,
	.disconnect = at76_disconnect,
	.id_table = dev_table,
2516
	.disable_hub_initiated_lpm = 1,
Kalle Valo's avatar
Kalle Valo committed
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542
};

static int __init at76_mod_init(void)
{
	int result;

	printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION " loading\n");

	mutex_init(&fw_mutex);

	/* register this driver with the USB subsystem */
	result = usb_register(&at76_driver);
	if (result < 0)
		printk(KERN_ERR DRIVER_NAME
		       ": usb_register failed (status %d)\n", result);

	led_trigger_register_simple("at76_usb-tx", &ledtrig_tx);
	return result;
}

static void __exit at76_mod_exit(void)
{
	int i;

	printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION " unloading\n");
	usb_deregister(&at76_driver);
2543 2544
	for (i = 0; i < ARRAY_SIZE(firmwares); i++)
		release_firmware(firmwares[i].fw);
Kalle Valo's avatar
Kalle Valo committed
2545 2546 2547
	led_trigger_unregister_simple(ledtrig_tx);
}

2548
module_param_named(debug, at76_debug, uint, 0600);
Kalle Valo's avatar
Kalle Valo committed
2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561
MODULE_PARM_DESC(debug, "Debugging level");

module_init(at76_mod_init);
module_exit(at76_mod_exit);

MODULE_AUTHOR("Oliver Kurth <oku@masqmail.cx>");
MODULE_AUTHOR("Joerg Albert <joerg.albert@gmx.de>");
MODULE_AUTHOR("Alex <alex@foogod.com>");
MODULE_AUTHOR("Nick Jones");
MODULE_AUTHOR("Balint Seeber <n0_5p4m_p13453@hotmail.com>");
MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>");
MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
MODULE_AUTHOR("Kalle Valo <kalle.valo@iki.fi>");
2562
MODULE_AUTHOR("Sebastian Smolorz <sesmo@gmx.net>");
Kalle Valo's avatar
Kalle Valo committed
2563 2564
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");