at76c50x-usb.c 68.7 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
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
502
503
504

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

static u8 at76_dfu_get_state(struct usb_device *udev, u8 *state)
{
	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)
{
	u8 *block;
	struct dfu_status dfu_stat_buf;
	int ret = 0;
	int need_dfu_state = 1;
	int is_done = 0;
	u8 dfu_state = 0;
	u32 dfu_timeout = 0;
	int bsize = 0;
	int blockno = 0;

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

	if (!size) {
		dev_printk(KERN_ERR, &udev->dev, "FW buffer length invalid!\n");
		return -EINVAL;
	}

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

	do {
		if (need_dfu_state) {
			ret = at76_dfu_get_state(udev, &dfu_state);
			if (ret < 0) {
				dev_printk(KERN_ERR, &udev->dev,
					   "cannot get DFU state: %d\n", ret);
				goto exit;
			}
			need_dfu_state = 0;
		}

		switch (dfu_state) {
		case STATE_DFU_DOWNLOAD_SYNC:
			at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_SYNC");
			ret = at76_dfu_get_status(udev, &dfu_stat_buf);
			if (ret >= 0) {
				dfu_state = dfu_stat_buf.state;
				dfu_timeout = at76_get_timeout(&dfu_stat_buf);
				need_dfu_state = 0;
			} else
				dev_printk(KERN_ERR, &udev->dev,
					   "at76_dfu_get_status returned %d\n",
					   ret);
			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)
				dev_printk(KERN_ERR, &udev->dev,
					   "at76_load_int_fw_block "
					   "returned %d\n", ret);
			need_dfu_state = 1;
			break;

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

			ret = at76_dfu_get_status(udev, &dfu_stat_buf);
			if (ret < 0)
				break;

			dfu_state = dfu_stat_buf.state;
			dfu_timeout = at76_get_timeout(&dfu_stat_buf);
			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:
			at76_dbg(DBG_DFU, "DFU UNKNOWN STATE (%d)", dfu_state);
			ret = -EINVAL;
			break;
		}
	} while (!is_done && (ret >= 0));

exit:
	kfree(block);
	if (ret >= 0)
		ret = 0;

	return ret;
}

#define HEX2STR_BUFFERS 4
#define HEX2STR_MAX_LEN 64

/* Convert binary data into hex string */
505
static char *hex2str(void *buf, size_t len)
Kalle Valo's avatar
Kalle Valo committed
506
507
508
509
510
511
512
513
514
515
{
	static atomic_t a = ATOMIC_INIT(0);
	static char bufs[HEX2STR_BUFFERS][3 * HEX2STR_MAX_LEN + 1];
	char *ret = bufs[atomic_inc_return(&a) & (HEX2STR_BUFFERS - 1)];
	char *obuf = ret;
	u8 *ibuf = buf;

	if (len > HEX2STR_MAX_LEN)
		len = HEX2STR_MAX_LEN;

516
517
	if (len == 0)
		goto exit;
Kalle Valo's avatar
Kalle Valo committed
518
519

	while (len--) {
520
		obuf = hex_byte_pack(obuf, *ibuf++);
Kalle Valo's avatar
Kalle Valo committed
521
522
		*obuf++ = '-';
	}
523
524
525
526
	obuf--;

exit:
	*obuf = '\0';
Kalle Valo's avatar
Kalle Valo committed
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
559
560
561
562
563
564
565
566
567
568
569
570

	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;
571
572
	u8 saved;
	u8 *op_mode;
Kalle Valo's avatar
Kalle Valo committed
573

574
575
576
	op_mode = kmalloc(1, GFP_NOIO);
	if (!op_mode)
		return -ENOMEM;
Kalle Valo's avatar
Kalle Valo committed
577
578
	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
			      USB_TYPE_VENDOR | USB_DIR_IN |
579
			      USB_RECIP_INTERFACE, 0x01, 0, op_mode, 1,
Kalle Valo's avatar
Kalle Valo committed
580
			      USB_CTRL_GET_TIMEOUT);
581
582
583
	saved = *op_mode;
	kfree(op_mode);

Kalle Valo's avatar
Kalle Valo committed
584
585
586
587
588
	if (ret < 0)
		return ret;
	else if (ret < 1)
		return -EIO;
	else
589
		return saved;
Kalle Valo's avatar
Kalle Valo committed
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
644
645
646
647
648
649
650
651
652
653
654
655
}

/* 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)
656
		wiphy_err(priv->hw->wiphy, "cannot get HW Config (error %d)\n",
657
			  ret);
Kalle Valo's avatar
Kalle Valo committed
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
690
691
692
693
694
695
696
697
698
699
700
701

	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)
{
702
	u8 *stat_buf;
Kalle Valo's avatar
Kalle Valo committed
703
704
	int ret;

705
706
707
708
	stat_buf = kmalloc(40, GFP_NOIO);
	if (!stat_buf)
		return -ENOMEM;

Kalle Valo's avatar
Kalle Valo committed
709
	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x22,
710
711
712
713
714
715
			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
716

717
	return ret;
Kalle Valo's avatar
Kalle Valo committed
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
}

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

737
static int at76_set_card_command(struct usb_device *udev, u8 cmd, void *buf,
Kalle Valo's avatar
Kalle Valo committed
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
780
781
782
783
784
785
786
787
788
789
790
791
				 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) {
792
793
794
			wiphy_err(priv->hw->wiphy,
				  "at76_get_cmd_status failed: %d\n",
				  status);
Kalle Valo's avatar
Kalle Valo committed
795
796
797
798
799
800
801
802
803
804
805
806
807
808
			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)) {
809
810
			wiphy_err(priv->hw->wiphy,
				  "completion timeout for command %d\n", cmd);
Kalle Valo's avatar
Kalle Valo committed
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
			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) {
831
832
833
		wiphy_info(priv->hw->wiphy,
			   "set_mib: at76_wait_completion failed with %d\n",
			   ret);
Kalle Valo's avatar
Kalle Valo committed
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
		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)
853
854
		wiphy_err(priv->hw->wiphy,
			  "at76_set_card_command(%d) failed: %d\n", cmd, ret);
Kalle Valo's avatar
Kalle Valo committed
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
	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)
874
875
		wiphy_err(priv->hw->wiphy, "set_mib (pm_mode) failed: %d\n",
			  ret);
Kalle Valo's avatar
Kalle Valo committed
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890

	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)
891
892
		wiphy_err(priv->hw->wiphy, "set_mib (preamble) failed: %d\n",
			  ret);
Kalle Valo's avatar
Kalle Valo committed
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907

	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)
908
909
		wiphy_err(priv->hw->wiphy,
			  "set_mib (frag threshold) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924

	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)
925
		wiphy_err(priv->hw->wiphy, "set_mib (rts) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940

	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)
941
942
		wiphy_err(priv->hw->wiphy,
			  "set_mib (autorate fallback) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959

	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) {
960
		wiphy_err(priv->hw->wiphy,
961
			  "at76_get_mib (MAC_ADDR) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
962
963
964
		goto exit;
	}

965
	at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: mac_addr %pM res 0x%x 0x%x",
Kalle Valo's avatar
Kalle Valo committed
966
		 wiphy_name(priv->hw->wiphy),
967
		 m->mac_addr, m->res[0], m->res[1]);
Kalle Valo's avatar
Kalle Valo committed
968
	for (i = 0; i < ARRAY_SIZE(m->group_addr); i++)
969
		at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: group addr %d: %pM, "
Kalle Valo's avatar
Kalle Valo committed
970
			 "status %d", wiphy_name(priv->hw->wiphy), i,
971
			 m->group_addr[i], m->group_addr_status[i]);
Kalle Valo's avatar
Kalle Valo committed
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
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) {
989
		wiphy_err(priv->hw->wiphy,
990
			  "at76_get_mib (MAC_WEP) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
		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++)
		at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: key %d: %s",
			 wiphy_name(priv->hw->wiphy), i,
			 hex2str(m->wep_default_keyvalue[i], key_len));
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) {
1026
		wiphy_err(priv->hw->wiphy,
1027
			  "at76_get_mib (MAC_MGMT) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1028
1029
1030
1031
1032
1033
		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 "
1034
		 "current_bssid %pM current_essid %s current_bss_type %d "
Kalle Valo's avatar
Kalle Valo committed
1035
1036
1037
1038
1039
1040
1041
1042
		 "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,
1043
		 m->CFP_period, m->current_bssid,
Kalle Valo's avatar
Kalle Valo committed
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
		 hex2str(m->current_essid, IW_ESSID_MAX_SIZE),
		 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) {
1062
		wiphy_err(priv->hw->wiphy,
1063
			  "at76_get_mib (MAC) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1064
1065
1066
1067
1068
1069
1070
1071
		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 "
1072
		 "desired_ssid %s desired_bssid %pM desired_bsstype %d",
Kalle Valo's avatar
Kalle Valo committed
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
		 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),
		 hex2str(m->desired_ssid, IW_ESSID_MAX_SIZE),
1084
		 m->desired_bssid, m->desired_bsstype);
Kalle Valo's avatar
Kalle Valo committed
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
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) {
1099
		wiphy_err(priv->hw->wiphy,
1100
			  "at76_get_mib (PHY) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
		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;
	struct mib_local *m = kmalloc(sizeof(struct mib_phy), GFP_KERNEL);

	if (!m)
		return;

	ret = at76_get_mib(priv->udev, MIB_LOCAL, m, sizeof(struct mib_local));
	if (ret < 0) {
1132
		wiphy_err(priv->hw->wiphy,
1133
			  "at76_get_mib (LOCAL) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
		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) {
1158
		wiphy_err(priv->hw->wiphy,
1159
			  "at76_get_mib (MDOMAIN) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
		goto exit;
	}

	at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: channel_list %s",
		 wiphy_name(priv->hw->wiphy),
		 hex2str(m->channel_list, sizeof(m->channel_list)));

	at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: tx_powerlevel %s",
		 wiphy_name(priv->hw->wiphy),
		 hex2str(m->tx_powerlevel, sizeof(m->tx_powerlevel)));
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;
1186
1187
1188
	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
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229

	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) {
1230
		wiphy_err(priv->hw->wiphy, "%s: priv->rx_urb is NULL\n",
1231
			  __func__);
Kalle Valo's avatar
Kalle Valo committed
1232
1233
1234
1235
1236
1237
		return -EFAULT;
	}

	if (!skb) {
		skb = dev_alloc_skb(sizeof(struct at76_rx_buffer));
		if (!skb) {
1238
1239
			wiphy_err(priv->hw->wiphy,
				  "cannot allocate rx skbuff\n");
Kalle Valo's avatar
Kalle Valo committed
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
			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
1258
1259
			wiphy_err(priv->hw->wiphy,
				  "rx, usb_submit_urb failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1260
1261
1262
1263
	}

exit:
	if (ret < 0 && ret != -ENODEV)
1264
1265
		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
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433

	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) {
		dev_printk(KERN_ERR, &udev->dev, "unexpected opmode %d\n",
			   op_mode);
		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) {
			dev_printk(KERN_ERR, &udev->dev,
				   "loading %dth firmware block failed: %d\n",
				   blockno, ret);
			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)
		dev_printk(KERN_ERR, &udev->dev,
			   "downloading external firmware failed: %d\n", ret);
	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) {
		dev_printk(KERN_ERR, &udev->dev,
			   "downloading internal fw failed with %d\n", ret);
		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) {
			dev_printk(KERN_ERR, &udev->dev,
				   "sending REMAP failed with %d\n", ret);
			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,
		 "%s param: ssid %.*s (%s) mode %s ch %d wep %s key %d "
		 "keylen %d", wiphy_name(priv->hw->wiphy), priv->essid_size,
		 priv->essid, hex2str(priv->essid, IW_ESSID_MAX_SIZE),
		 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) {
1434
1435
		wiphy_err(priv->hw->wiphy, "at76_set_card_command failed: %d\n",
			  ret);
Kalle Valo's avatar
Kalle Valo committed
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
		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;

1488
1489
1490
	if (priv->device_unplugged)
		return;

Kalle Valo's avatar
Kalle Valo committed
1491
1492
1493
1494
1495
1496
1497
1498
1499
	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)
1500
1501
		wiphy_err(priv->hw->wiphy,
			  "set_mib (promiscuous_mode) failed: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525

	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");
1526
		at76_dbg(DBG_DEVSTART, "urb status %d", urb->status);
Kalle Valo's avatar
Kalle Valo committed
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
		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);
1560
1561
	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
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644

	/* 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) {
		dev_printk(KERN_ERR, &udev->dev, "firmware %s not found!\n",
			   fwe->fwname);
		dev_printk(KERN_ERR, &udev->dev,
			   "you may need to download the firmware from "
			   "http://developer.berlios.de/projects/at76c503a/\n");
		goto exit;
	}

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

	if (fwe->fw->size <= sizeof(*fwh)) {
		dev_printk(KERN_ERR, &udev->dev,
			   "firmware is too short (0x%zx)\n", fwe->fw->size);
		goto exit;
	}

	/* CRC currently not checked */
	fwe->board_type = le32_to_cpu(fwh->board_type);
	if (fwe->board_type != board_type) {
		dev_printk(KERN_ERR, &udev->dev,
			   "board type mismatch, requested %u, got %u\n",
			   board_type, fwe->board_type);
		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;
}

1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
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) {
1663
1664
		wiphy_err(priv->hw->wiphy, "at76_set_card_command failed: %d\n",
			  ret);
1665
1666
1667
1668
1669
1670
		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) {
1671
1672
		wiphy_err(priv->hw->wiphy, "at76_wait_completion failed: %d\n",
			  ret);
1673
1674
1675
1676
1677
1678
1679
1680
		return 0;
	}

	at76_set_pm_mode(priv);

	return 0;
}

1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
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
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
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);
}

1729
static void at76_mac80211_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo's avatar
Kalle Valo committed
1730
1731
1732
1733
{
	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);
1734
	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
Kalle Valo's avatar
Kalle Valo committed
1735
1736
1737
1738
1739
	int padding, submit_len, ret;

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

	if (priv->tx_urb->status == -EINPROGRESS) {
1740
1741
		wiphy_err(priv->hw->wiphy,
			  "%s called while tx urb is pending\n", __func__);
1742
1743
		dev_kfree_skb_any(skb);
		return;
Kalle Valo's avatar
Kalle Valo committed
1744
1745
	}

1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
	/* 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)) {
		if (compare_ether_addr(priv->bssid, mgmt->bssid)) {
			memcpy(priv->bssid, mgmt->bssid, ETH_ALEN);
			ieee80211_queue_work(hw, &priv->work_join_bssid);
1757
1758
			dev_kfree_skb_any(skb);
			return;
1759
1760
1761
		}
	}

Kalle Valo's avatar
Kalle Valo committed
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
	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) {
1791
		wiphy_err(priv->hw->wiphy, "error in tx submit urb: %d\n", ret);
Kalle Valo's avatar
Kalle Valo committed
1792
		if (ret == -EINVAL)
1793
			wiphy_err(priv->hw->wiphy,
1794
				  "-EINVAL: tx urb %p hcpriv %p complete %p\n",
1795
1796
				  priv->tx_urb,
				  priv->tx_urb->hcpriv, priv->tx_urb->complete);
Kalle Valo's avatar
Kalle Valo committed
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
	}
}

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) {
1811
1812
		wiphy_err(priv->hw->wiphy, "open: submit_rx_urb failed: %d\n",
			  ret);
Kalle Valo's avatar
Kalle Valo committed
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
		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__);

1832
	cancel_delayed_work(&priv->dwork_hw_scan);
1833
	cancel_work_sync(&priv->work_join_bssid);
1834
1835
	cancel_work_sync(&priv->work_set_promisc);

Kalle Valo's avatar
Kalle Valo committed
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
	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,
1852
			      struct ieee80211_vif *vif)
Kalle Valo's avatar
Kalle Valo committed
1853
1854
1855
1856
1857
1858
1859
1860
{
	struct at76_priv *priv = hw->priv;
	int ret = 0;

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

	mutex_lock(&priv->mtx);

1861
	switch (vif->type) {
Kalle Valo's avatar
Kalle Valo committed
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
	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,
1877
				  struct ieee80211_vif *vif)
Kalle Valo's avatar
Kalle Valo committed
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
{
	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;

1888
1889
1890
	if (priv->device_unplugged)
		return;

Kalle Valo's avatar
Kalle Valo committed
1891
1892
1893
1894
1895
1896
1897
1898
	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) {
1899
1900
		ieee80211_queue_delayed_work(priv->hw, &priv->dwork_hw_scan,
					     SCAN_POLL_INTERVAL);
1901
1902
		mutex_unlock(&priv->mtx);
		return;
Kalle Valo's avatar
Kalle Valo committed
1903
1904
1905
1906
1907
1908
	}

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

	mutex_unlock(&priv->mtx);
1909
1910
1911
1912

	ieee80211_scan_completed(priv->hw, false);

	ieee80211_wake_queues(priv->hw);
Kalle Valo's avatar
Kalle Valo committed
1913
1914
}

1915
static int at76_hw_scan(struct ieee80211_hw *hw,
1916
			struct ieee80211_vif *vif,
1917
			struct cfg80211_scan_request *req)
Kalle Valo's avatar
Kalle Valo committed
1918
1919
1920
{
	struct at76_priv *priv = hw->priv;
	struct at76_req_scan scan;
1921
1922
	u8 *ssid = NULL;
	int ret, len = 0;
Kalle Valo's avatar
Kalle Valo committed
1923
1924
1925

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

1926
1927
1928
	if (priv->device_unplugged)
		return 0;

Kalle Valo's avatar
Kalle Valo committed
1929
1930
1931
1932
1933
1934
	mutex_lock(&priv->mtx);

	ieee80211_stop_queues(hw);

	memset(&scan, 0, sizeof(struct at76_req_scan));
	memset(scan.bssid, 0xFF, ETH_ALEN);
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944

	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
1945
1946
1947
		memcpy(scan.essid, ssid, len);
		scan.essid_size = len;
	}
1948

Kalle Valo's avatar
Kalle Valo committed
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
	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) {
		err("CMD_SCAN failed: %d", ret);
		goto exit;
	}

1962
1963
	ieee80211_queue_delayed_work(priv->hw, &priv->dwork_hw_scan,
				     SCAN_POLL_INTERVAL);
Kalle Valo's avatar
Kalle Valo committed
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974

exit:
	mutex_unlock(&priv->mtx);

	return 0;
}

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

1975
1976
	at76_dbg(DBG_MAC80211, "%s(): channel %d",
		 __func__, hw->conf.channel->hw_value);
Kalle Valo's avatar
Kalle Valo committed
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
	at76_dbg_dump(DBG_MAC80211, priv->bssid, ETH_ALEN, "bssid:");

	mutex_lock(&priv->mtx);

	priv->channel = hw->conf.channel->hw_value;

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

	mutex_unlock(&priv->mtx);

	return 0;
}

1993
1994
1995
1996
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
1997
1998
1999
2000
{
	struct at76_priv *priv = hw->priv;

	at76_dbg(DBG_MAC80211, "%s():", __func__);
2001
2002
2003
2004

	if (!(changed & BSS_CHANGED_BSSID))
		return;

Kalle Valo's avatar
Kalle Valo committed
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
	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,
2021
				  unsigned int *total_flags, u64 multicast)
Kalle Valo's avatar
Kalle Valo committed
2022
2023
2024
2025
2026
{
	struct at76_priv *priv = hw->priv;
	int flags;

	at76_dbg(DBG_MAC80211, "%s(): changed_flags=0x%08x "
2027
2028
		 "total_flags=0x%08x",
		 __func__, changed_flags, *total_flags);
Kalle Valo's avatar
Kalle Valo committed
2029
2030
2031
2032

	flags = changed_flags & AT76_SUPPORTED_FILTERS;
	*total_flags = AT76_SUPPORTED_FILTERS;

2033
2034
2035
2036
	/* Bail out after updating flags to prevent a WARN_ON in mac80211. */
	if (priv->device_unplugged)
		return;

Kalle Valo's avatar
Kalle Valo committed
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
	/* 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;

2050
	ieee80211_queue_work(hw, &priv->work_set_promisc);
Kalle Valo's avatar
Kalle Valo committed
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
}

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;

2061
	at76_dbg(DBG_MAC80211, "%s(): cmd %d key->cipher %d key->keyidx %d "
Kalle Valo's avatar
Kalle Valo committed
2062
		 "key->keylen %d",
2063
		 __func__, cmd, key->cipher, key->keyidx, key->keylen);