blob: 9b0f055420ee0283e418a24a6487af693e96ebd5 [file] [log] [blame]
Kalle Valo1264b952009-02-07 09:27:10 +02001/*
2 * at76c503/at76c505 USB driver
3 *
4 * Copyright (c) 2002 - 2003 Oliver Kurth
5 * Copyright (c) 2004 Joerg Albert <joerg.albert@gmx.de>
6 * Copyright (c) 2004 Nick Jones
7 * Copyright (c) 2004 Balint Seeber <n0_5p4m_p13453@hotmail.com>
8 * Copyright (c) 2007 Guido Guenther <agx@sigxcpu.org>
9 * Copyright (c) 2007 Kalle Valo <kalle.valo@iki.fi>
Sebastian Smolorza1850452010-06-22 16:55:17 +020010 * Copyright (c) 2010 Sebastian Smolorz <sesmo@gmx.net>
Kalle Valo1264b952009-02-07 09:27:10 +020011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This file is part of the Berlios driver for WLAN USB devices based on the
18 * Atmel AT76C503A/505/505A.
19 *
20 * Some iw_handler code was taken from airo.c, (C) 1999 Benjamin Reed
21 *
Kalle Valoba3907e2009-02-21 09:53:37 +020022 * TODO list is at the wiki:
23 *
24 * http://wireless.kernel.org/en/users/Drivers/at76c50x-usb#TODO
25 *
Kalle Valo1264b952009-02-07 09:27:10 +020026 */
27
28#include <linux/init.h>
29#include <linux/kernel.h>
30#include <linux/sched.h>
31#include <linux/errno.h>
32#include <linux/slab.h>
33#include <linux/module.h>
34#include <linux/spinlock.h>
35#include <linux/list.h>
36#include <linux/usb.h>
37#include <linux/netdevice.h>
38#include <linux/if_arp.h>
39#include <linux/etherdevice.h>
40#include <linux/ethtool.h>
41#include <linux/wireless.h>
42#include <net/iw_handler.h>
43#include <net/ieee80211_radiotap.h>
44#include <linux/firmware.h>
45#include <linux/leds.h>
46#include <net/mac80211.h>
47
48#include "at76c50x-usb.h"
49
50/* Version information */
51#define DRIVER_NAME "at76c50x-usb"
52#define DRIVER_VERSION "0.17"
53#define DRIVER_DESC "Atmel at76x USB Wireless LAN Driver"
54
55/* at76_debug bits */
56#define DBG_PROGRESS 0x00000001 /* authentication/accociation */
57#define DBG_BSS_TABLE 0x00000002 /* show BSS table after scans */
58#define DBG_IOCTL 0x00000004 /* ioctl calls / settings */
59#define DBG_MAC_STATE 0x00000008 /* MAC state transitions */
60#define DBG_TX_DATA 0x00000010 /* tx header */
61#define DBG_TX_DATA_CONTENT 0x00000020 /* tx content */
62#define DBG_TX_MGMT 0x00000040 /* tx management */
63#define DBG_RX_DATA 0x00000080 /* rx data header */
64#define DBG_RX_DATA_CONTENT 0x00000100 /* rx data content */
65#define DBG_RX_MGMT 0x00000200 /* rx mgmt frame headers */
66#define DBG_RX_BEACON 0x00000400 /* rx beacon */
67#define DBG_RX_CTRL 0x00000800 /* rx control */
68#define DBG_RX_MGMT_CONTENT 0x00001000 /* rx mgmt content */
69#define DBG_RX_FRAGS 0x00002000 /* rx data fragment handling */
70#define DBG_DEVSTART 0x00004000 /* fw download, device start */
71#define DBG_URB 0x00008000 /* rx urb status, ... */
72#define DBG_RX_ATMEL_HDR 0x00010000 /* Atmel-specific Rx headers */
73#define DBG_PROC_ENTRY 0x00020000 /* procedure entries/exits */
74#define DBG_PM 0x00040000 /* power management settings */
75#define DBG_BSS_MATCH 0x00080000 /* BSS match failures */
76#define DBG_PARAMS 0x00100000 /* show configured parameters */
77#define DBG_WAIT_COMPLETE 0x00200000 /* command completion */
78#define DBG_RX_FRAGS_SKB 0x00400000 /* skb header of Rx fragments */
79#define DBG_BSS_TABLE_RM 0x00800000 /* purging bss table entries */
80#define DBG_MONITOR_MODE 0x01000000 /* monitor mode */
81#define DBG_MIB 0x02000000 /* dump all MIBs on startup */
82#define DBG_MGMT_TIMER 0x04000000 /* dump mgmt_timer ops */
83#define DBG_WE_EVENTS 0x08000000 /* dump wireless events */
84#define DBG_FW 0x10000000 /* firmware download */
85#define DBG_DFU 0x20000000 /* device firmware upgrade */
86#define DBG_CMD 0x40000000
87#define DBG_MAC80211 0x80000000
88
89#define DBG_DEFAULTS 0
90
91/* Use our own dbg macro */
92#define at76_dbg(bits, format, arg...) \
93 do { \
94 if (at76_debug & (bits)) \
95 printk(KERN_DEBUG DRIVER_NAME ": " format "\n" , \
96 ## arg); \
97 } while (0)
98
99#define at76_dbg_dump(bits, buf, len, format, arg...) \
100 do { \
101 if (at76_debug & (bits)) { \
102 printk(KERN_DEBUG DRIVER_NAME ": " format "\n" , \
103 ## arg); \
104 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, \
105 buf, len); \
106 } \
107 } while (0)
108
Jason Andryuk2ce4f9d2009-02-18 22:41:04 +0200109static uint at76_debug = DBG_DEFAULTS;
Kalle Valo1264b952009-02-07 09:27:10 +0200110
111/* Protect against concurrent firmware loading and parsing */
112static struct mutex fw_mutex;
113
114static struct fwentry firmwares[] = {
115 [0] = { "" },
116 [BOARD_503_ISL3861] = { "atmel_at76c503-i3861.bin" },
117 [BOARD_503_ISL3863] = { "atmel_at76c503-i3863.bin" },
118 [BOARD_503] = { "atmel_at76c503-rfmd.bin" },
119 [BOARD_503_ACC] = { "atmel_at76c503-rfmd-acc.bin" },
120 [BOARD_505] = { "atmel_at76c505-rfmd.bin" },
121 [BOARD_505_2958] = { "atmel_at76c505-rfmd2958.bin" },
122 [BOARD_505A] = { "atmel_at76c505a-rfmd2958.bin" },
123 [BOARD_505AMX] = { "atmel_at76c505amx-rfmd.bin" },
124};
Ben Hutchings202982d2009-11-07 21:56:08 +0000125MODULE_FIRMWARE("atmel_at76c503-i3861.bin");
126MODULE_FIRMWARE("atmel_at76c503-i3863.bin");
127MODULE_FIRMWARE("atmel_at76c503-rfmd.bin");
128MODULE_FIRMWARE("atmel_at76c503-rfmd-acc.bin");
129MODULE_FIRMWARE("atmel_at76c505-rfmd.bin");
130MODULE_FIRMWARE("atmel_at76c505-rfmd2958.bin");
131MODULE_FIRMWARE("atmel_at76c505a-rfmd2958.bin");
132MODULE_FIRMWARE("atmel_at76c505amx-rfmd.bin");
Kalle Valo1264b952009-02-07 09:27:10 +0200133
134#define USB_DEVICE_DATA(__ops) .driver_info = (kernel_ulong_t)(__ops)
135
136static struct usb_device_id dev_table[] = {
137 /*
138 * at76c503-i3861
139 */
140 /* Generic AT76C503/3861 device */
141 { USB_DEVICE(0x03eb, 0x7603), USB_DEVICE_DATA(BOARD_503_ISL3861) },
142 /* Linksys WUSB11 v2.1/v2.6 */
143 { USB_DEVICE(0x066b, 0x2211), USB_DEVICE_DATA(BOARD_503_ISL3861) },
144 /* Netgear MA101 rev. A */
145 { USB_DEVICE(0x0864, 0x4100), USB_DEVICE_DATA(BOARD_503_ISL3861) },
146 /* Tekram U300C / Allnet ALL0193 */
147 { USB_DEVICE(0x0b3b, 0x1612), USB_DEVICE_DATA(BOARD_503_ISL3861) },
148 /* HP HN210W J7801A */
149 { USB_DEVICE(0x03f0, 0x011c), USB_DEVICE_DATA(BOARD_503_ISL3861) },
150 /* Sitecom/Z-Com/Zyxel M4Y-750 */
151 { USB_DEVICE(0x0cde, 0x0001), USB_DEVICE_DATA(BOARD_503_ISL3861) },
152 /* Dynalink/Askey WLL013 (intersil) */
153 { USB_DEVICE(0x069a, 0x0320), USB_DEVICE_DATA(BOARD_503_ISL3861) },
154 /* EZ connect 11Mpbs Wireless USB Adapter SMC2662W v1 */
155 { USB_DEVICE(0x0d5c, 0xa001), USB_DEVICE_DATA(BOARD_503_ISL3861) },
156 /* BenQ AWL300 */
157 { USB_DEVICE(0x04a5, 0x9000), USB_DEVICE_DATA(BOARD_503_ISL3861) },
158 /* Addtron AWU-120, Compex WLU11 */
159 { USB_DEVICE(0x05dd, 0xff31), USB_DEVICE_DATA(BOARD_503_ISL3861) },
160 /* Intel AP310 AnyPoint II USB */
161 { USB_DEVICE(0x8086, 0x0200), USB_DEVICE_DATA(BOARD_503_ISL3861) },
162 /* Dynalink L11U */
163 { USB_DEVICE(0x0d8e, 0x7100), USB_DEVICE_DATA(BOARD_503_ISL3861) },
164 /* Arescom WL-210, FCC id 07J-GL2411USB */
165 { USB_DEVICE(0x0d8e, 0x7110), USB_DEVICE_DATA(BOARD_503_ISL3861) },
166 /* I-O DATA WN-B11/USB */
167 { USB_DEVICE(0x04bb, 0x0919), USB_DEVICE_DATA(BOARD_503_ISL3861) },
168 /* BT Voyager 1010 */
169 { USB_DEVICE(0x069a, 0x0821), USB_DEVICE_DATA(BOARD_503_ISL3861) },
170 /*
171 * at76c503-i3863
172 */
173 /* Generic AT76C503/3863 device */
174 { USB_DEVICE(0x03eb, 0x7604), USB_DEVICE_DATA(BOARD_503_ISL3863) },
175 /* Samsung SWL-2100U */
176 { USB_DEVICE(0x055d, 0xa000), USB_DEVICE_DATA(BOARD_503_ISL3863) },
177 /*
178 * at76c503-rfmd
179 */
180 /* Generic AT76C503/RFMD device */
181 { USB_DEVICE(0x03eb, 0x7605), USB_DEVICE_DATA(BOARD_503) },
182 /* Dynalink/Askey WLL013 (rfmd) */
183 { USB_DEVICE(0x069a, 0x0321), USB_DEVICE_DATA(BOARD_503) },
184 /* Linksys WUSB11 v2.6 */
185 { USB_DEVICE(0x077b, 0x2219), USB_DEVICE_DATA(BOARD_503) },
186 /* Network Everywhere NWU11B */
187 { USB_DEVICE(0x077b, 0x2227), USB_DEVICE_DATA(BOARD_503) },
188 /* Netgear MA101 rev. B */
189 { USB_DEVICE(0x0864, 0x4102), USB_DEVICE_DATA(BOARD_503) },
190 /* D-Link DWL-120 rev. E */
191 { USB_DEVICE(0x2001, 0x3200), USB_DEVICE_DATA(BOARD_503) },
192 /* Actiontec 802UAT1, HWU01150-01UK */
193 { USB_DEVICE(0x1668, 0x7605), USB_DEVICE_DATA(BOARD_503) },
194 /* AirVast W-Buddie WN210 */
195 { USB_DEVICE(0x03eb, 0x4102), USB_DEVICE_DATA(BOARD_503) },
196 /* Dick Smith Electronics XH1153 802.11b USB adapter */
197 { USB_DEVICE(0x1371, 0x5743), USB_DEVICE_DATA(BOARD_503) },
198 /* CNet CNUSB611 */
199 { USB_DEVICE(0x1371, 0x0001), USB_DEVICE_DATA(BOARD_503) },
200 /* FiberLine FL-WL200U */
201 { USB_DEVICE(0x1371, 0x0002), USB_DEVICE_DATA(BOARD_503) },
202 /* BenQ AWL400 USB stick */
203 { USB_DEVICE(0x04a5, 0x9001), USB_DEVICE_DATA(BOARD_503) },
204 /* 3Com 3CRSHEW696 */
205 { USB_DEVICE(0x0506, 0x0a01), USB_DEVICE_DATA(BOARD_503) },
206 /* Siemens Santis ADSL WLAN USB adapter WLL 013 */
207 { USB_DEVICE(0x0681, 0x001b), USB_DEVICE_DATA(BOARD_503) },
208 /* Belkin F5D6050, version 2 */
209 { USB_DEVICE(0x050d, 0x0050), USB_DEVICE_DATA(BOARD_503) },
210 /* iBlitzz, BWU613 (not *B or *SB) */
211 { USB_DEVICE(0x07b8, 0xb000), USB_DEVICE_DATA(BOARD_503) },
212 /* Gigabyte GN-WLBM101 */
213 { USB_DEVICE(0x1044, 0x8003), USB_DEVICE_DATA(BOARD_503) },
214 /* Planex GW-US11S */
215 { USB_DEVICE(0x2019, 0x3220), USB_DEVICE_DATA(BOARD_503) },
216 /* Internal WLAN adapter in h5[4,5]xx series iPAQs */
217 { USB_DEVICE(0x049f, 0x0032), USB_DEVICE_DATA(BOARD_503) },
218 /* Corega Wireless LAN USB-11 mini */
219 { USB_DEVICE(0x07aa, 0x0011), USB_DEVICE_DATA(BOARD_503) },
220 /* Corega Wireless LAN USB-11 mini2 */
221 { USB_DEVICE(0x07aa, 0x0018), USB_DEVICE_DATA(BOARD_503) },
222 /* Uniden PCW100 */
223 { USB_DEVICE(0x05dd, 0xff35), USB_DEVICE_DATA(BOARD_503) },
224 /*
225 * at76c503-rfmd-acc
226 */
227 /* SMC2664W */
228 { USB_DEVICE(0x083a, 0x3501), USB_DEVICE_DATA(BOARD_503_ACC) },
229 /* Belkin F5D6050, SMC2662W v2, SMC2662W-AR */
230 { USB_DEVICE(0x0d5c, 0xa002), USB_DEVICE_DATA(BOARD_503_ACC) },
231 /*
232 * at76c505-rfmd
233 */
234 /* Generic AT76C505/RFMD */
235 { USB_DEVICE(0x03eb, 0x7606), USB_DEVICE_DATA(BOARD_505) },
236 /*
237 * at76c505-rfmd2958
238 */
239 /* Generic AT76C505/RFMD, OvisLink WL-1130USB */
240 { USB_DEVICE(0x03eb, 0x7613), USB_DEVICE_DATA(BOARD_505_2958) },
241 /* Fiberline FL-WL240U */
242 { USB_DEVICE(0x1371, 0x0014), USB_DEVICE_DATA(BOARD_505_2958) },
243 /* CNet CNUSB-611G */
244 { USB_DEVICE(0x1371, 0x0013), USB_DEVICE_DATA(BOARD_505_2958) },
245 /* Linksys WUSB11 v2.8 */
246 { USB_DEVICE(0x1915, 0x2233), USB_DEVICE_DATA(BOARD_505_2958) },
247 /* Xterasys XN-2122B, IBlitzz BWU613B/BWU613SB */
248 { USB_DEVICE(0x12fd, 0x1001), USB_DEVICE_DATA(BOARD_505_2958) },
249 /* Corega WLAN USB Stick 11 */
250 { USB_DEVICE(0x07aa, 0x7613), USB_DEVICE_DATA(BOARD_505_2958) },
251 /* Microstar MSI Box MS6978 */
252 { USB_DEVICE(0x0db0, 0x1020), USB_DEVICE_DATA(BOARD_505_2958) },
253 /*
254 * at76c505a-rfmd2958
255 */
256 /* Generic AT76C505A device */
257 { USB_DEVICE(0x03eb, 0x7614), USB_DEVICE_DATA(BOARD_505A) },
258 /* Generic AT76C505AS device */
259 { USB_DEVICE(0x03eb, 0x7617), USB_DEVICE_DATA(BOARD_505A) },
260 /* Siemens Gigaset USB WLAN Adapter 11 */
261 { USB_DEVICE(0x1690, 0x0701), USB_DEVICE_DATA(BOARD_505A) },
Jamie Lentin02a9a392009-04-07 22:59:56 +0100262 /* OQO Model 01+ Internal Wi-Fi */
263 { USB_DEVICE(0x1557, 0x0002), USB_DEVICE_DATA(BOARD_505A) },
Kalle Valo1264b952009-02-07 09:27:10 +0200264 /*
265 * at76c505amx-rfmd
266 */
267 /* Generic AT76C505AMX device */
268 { USB_DEVICE(0x03eb, 0x7615), USB_DEVICE_DATA(BOARD_505AMX) },
269 { }
270};
271
272MODULE_DEVICE_TABLE(usb, dev_table);
273
274/* Supported rates of this hardware, bit 7 marks basic rates */
275static const u8 hw_rates[] = { 0x82, 0x84, 0x0b, 0x16 };
276
277static const char *const preambles[] = { "long", "short", "auto" };
278
279/* Firmware download */
280/* DFU states */
281#define STATE_IDLE 0x00
282#define STATE_DETACH 0x01
283#define STATE_DFU_IDLE 0x02
284#define STATE_DFU_DOWNLOAD_SYNC 0x03
285#define STATE_DFU_DOWNLOAD_BUSY 0x04
286#define STATE_DFU_DOWNLOAD_IDLE 0x05
287#define STATE_DFU_MANIFEST_SYNC 0x06
288#define STATE_DFU_MANIFEST 0x07
289#define STATE_DFU_MANIFEST_WAIT_RESET 0x08
290#define STATE_DFU_UPLOAD_IDLE 0x09
291#define STATE_DFU_ERROR 0x0a
292
293/* DFU commands */
294#define DFU_DETACH 0
295#define DFU_DNLOAD 1
296#define DFU_UPLOAD 2
297#define DFU_GETSTATUS 3
298#define DFU_CLRSTATUS 4
299#define DFU_GETSTATE 5
300#define DFU_ABORT 6
301
302#define FW_BLOCK_SIZE 1024
303
304struct dfu_status {
305 unsigned char status;
306 unsigned char poll_timeout[3];
307 unsigned char state;
308 unsigned char string;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000309} __packed;
Kalle Valo1264b952009-02-07 09:27:10 +0200310
311static inline int at76_is_intersil(enum board_type board)
312{
313 return (board == BOARD_503_ISL3861 || board == BOARD_503_ISL3863);
314}
315
316static inline int at76_is_503rfmd(enum board_type board)
317{
318 return (board == BOARD_503 || board == BOARD_503_ACC);
319}
320
321static inline int at76_is_505a(enum board_type board)
322{
323 return (board == BOARD_505A || board == BOARD_505AMX);
324}
325
326/* Load a block of the first (internal) part of the firmware */
327static int at76_load_int_fw_block(struct usb_device *udev, int blockno,
328 void *block, int size)
329{
330 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), DFU_DNLOAD,
331 USB_TYPE_CLASS | USB_DIR_OUT |
332 USB_RECIP_INTERFACE, blockno, 0, block, size,
333 USB_CTRL_GET_TIMEOUT);
334}
335
336static int at76_dfu_get_status(struct usb_device *udev,
337 struct dfu_status *status)
338{
339 int ret;
340
341 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), DFU_GETSTATUS,
342 USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
343 0, 0, status, sizeof(struct dfu_status),
344 USB_CTRL_GET_TIMEOUT);
345 return ret;
346}
347
348static u8 at76_dfu_get_state(struct usb_device *udev, u8 *state)
349{
350 int ret;
351
352 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), DFU_GETSTATE,
353 USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
354 0, 0, state, 1, USB_CTRL_GET_TIMEOUT);
355 return ret;
356}
357
358/* Convert timeout from the DFU status to jiffies */
359static inline unsigned long at76_get_timeout(struct dfu_status *s)
360{
361 return msecs_to_jiffies((s->poll_timeout[2] << 16)
362 | (s->poll_timeout[1] << 8)
363 | (s->poll_timeout[0]));
364}
365
366/* Load internal firmware from the buffer. If manifest_sync_timeout > 0, use
367 * its value in jiffies in the MANIFEST_SYNC state. */
368static int at76_usbdfu_download(struct usb_device *udev, u8 *buf, u32 size,
369 int manifest_sync_timeout)
370{
371 u8 *block;
372 struct dfu_status dfu_stat_buf;
373 int ret = 0;
374 int need_dfu_state = 1;
375 int is_done = 0;
376 u8 dfu_state = 0;
377 u32 dfu_timeout = 0;
378 int bsize = 0;
379 int blockno = 0;
380
381 at76_dbg(DBG_DFU, "%s( %p, %u, %d)", __func__, buf, size,
382 manifest_sync_timeout);
383
384 if (!size) {
385 dev_printk(KERN_ERR, &udev->dev, "FW buffer length invalid!\n");
386 return -EINVAL;
387 }
388
389 block = kmalloc(FW_BLOCK_SIZE, GFP_KERNEL);
390 if (!block)
391 return -ENOMEM;
392
393 do {
394 if (need_dfu_state) {
395 ret = at76_dfu_get_state(udev, &dfu_state);
396 if (ret < 0) {
397 dev_printk(KERN_ERR, &udev->dev,
398 "cannot get DFU state: %d\n", ret);
399 goto exit;
400 }
401 need_dfu_state = 0;
402 }
403
404 switch (dfu_state) {
405 case STATE_DFU_DOWNLOAD_SYNC:
406 at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_SYNC");
407 ret = at76_dfu_get_status(udev, &dfu_stat_buf);
408 if (ret >= 0) {
409 dfu_state = dfu_stat_buf.state;
410 dfu_timeout = at76_get_timeout(&dfu_stat_buf);
411 need_dfu_state = 0;
412 } else
413 dev_printk(KERN_ERR, &udev->dev,
414 "at76_dfu_get_status returned %d\n",
415 ret);
416 break;
417
418 case STATE_DFU_DOWNLOAD_BUSY:
419 at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_BUSY");
420 need_dfu_state = 1;
421
422 at76_dbg(DBG_DFU, "DFU: Resetting device");
423 schedule_timeout_interruptible(dfu_timeout);
424 break;
425
426 case STATE_DFU_DOWNLOAD_IDLE:
427 at76_dbg(DBG_DFU, "DOWNLOAD...");
428 /* fall through */
429 case STATE_DFU_IDLE:
430 at76_dbg(DBG_DFU, "DFU IDLE");
431
432 bsize = min_t(int, size, FW_BLOCK_SIZE);
433 memcpy(block, buf, bsize);
434 at76_dbg(DBG_DFU, "int fw, size left = %5d, "
435 "bsize = %4d, blockno = %2d", size, bsize,
436 blockno);
437 ret =
438 at76_load_int_fw_block(udev, blockno, block, bsize);
439 buf += bsize;
440 size -= bsize;
441 blockno++;
442
443 if (ret != bsize)
444 dev_printk(KERN_ERR, &udev->dev,
445 "at76_load_int_fw_block "
446 "returned %d\n", ret);
447 need_dfu_state = 1;
448 break;
449
450 case STATE_DFU_MANIFEST_SYNC:
451 at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST_SYNC");
452
453 ret = at76_dfu_get_status(udev, &dfu_stat_buf);
454 if (ret < 0)
455 break;
456
457 dfu_state = dfu_stat_buf.state;
458 dfu_timeout = at76_get_timeout(&dfu_stat_buf);
459 need_dfu_state = 0;
460
461 /* override the timeout from the status response,
462 needed for AT76C505A */
463 if (manifest_sync_timeout > 0)
464 dfu_timeout = manifest_sync_timeout;
465
466 at76_dbg(DBG_DFU, "DFU: Waiting for manifest phase");
467 schedule_timeout_interruptible(dfu_timeout);
468 break;
469
470 case STATE_DFU_MANIFEST:
471 at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST");
472 is_done = 1;
473 break;
474
475 case STATE_DFU_MANIFEST_WAIT_RESET:
476 at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST_WAIT_RESET");
477 is_done = 1;
478 break;
479
480 case STATE_DFU_UPLOAD_IDLE:
481 at76_dbg(DBG_DFU, "STATE_DFU_UPLOAD_IDLE");
482 break;
483
484 case STATE_DFU_ERROR:
485 at76_dbg(DBG_DFU, "STATE_DFU_ERROR");
486 ret = -EPIPE;
487 break;
488
489 default:
490 at76_dbg(DBG_DFU, "DFU UNKNOWN STATE (%d)", dfu_state);
491 ret = -EINVAL;
492 break;
493 }
494 } while (!is_done && (ret >= 0));
495
496exit:
497 kfree(block);
498 if (ret >= 0)
499 ret = 0;
500
501 return ret;
502}
503
504#define HEX2STR_BUFFERS 4
505#define HEX2STR_MAX_LEN 64
506#define BIN2HEX(x) ((x) < 10 ? '0' + (x) : (x) + 'A' - 10)
507
508/* Convert binary data into hex string */
509static char *hex2str(void *buf, int len)
510{
511 static atomic_t a = ATOMIC_INIT(0);
512 static char bufs[HEX2STR_BUFFERS][3 * HEX2STR_MAX_LEN + 1];
513 char *ret = bufs[atomic_inc_return(&a) & (HEX2STR_BUFFERS - 1)];
514 char *obuf = ret;
515 u8 *ibuf = buf;
516
517 if (len > HEX2STR_MAX_LEN)
518 len = HEX2STR_MAX_LEN;
519
520 if (len <= 0) {
521 ret[0] = '\0';
522 return ret;
523 }
524
525 while (len--) {
526 *obuf++ = BIN2HEX(*ibuf >> 4);
527 *obuf++ = BIN2HEX(*ibuf & 0xf);
528 *obuf++ = '-';
529 ibuf++;
530 }
531 *(--obuf) = '\0';
532
533 return ret;
534}
535
Kalle Valo1264b952009-02-07 09:27:10 +0200536/* LED trigger */
537static int tx_activity;
538static void at76_ledtrig_tx_timerfunc(unsigned long data);
539static DEFINE_TIMER(ledtrig_tx_timer, at76_ledtrig_tx_timerfunc, 0, 0);
540DEFINE_LED_TRIGGER(ledtrig_tx);
541
542static void at76_ledtrig_tx_timerfunc(unsigned long data)
543{
544 static int tx_lastactivity;
545
546 if (tx_lastactivity != tx_activity) {
547 tx_lastactivity = tx_activity;
548 led_trigger_event(ledtrig_tx, LED_FULL);
549 mod_timer(&ledtrig_tx_timer, jiffies + HZ / 4);
550 } else
551 led_trigger_event(ledtrig_tx, LED_OFF);
552}
553
554static void at76_ledtrig_tx_activity(void)
555{
556 tx_activity++;
557 if (!timer_pending(&ledtrig_tx_timer))
558 mod_timer(&ledtrig_tx_timer, jiffies + HZ / 4);
559}
560
561static int at76_remap(struct usb_device *udev)
562{
563 int ret;
564 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0a,
565 USB_TYPE_VENDOR | USB_DIR_OUT |
566 USB_RECIP_INTERFACE, 0, 0, NULL, 0,
567 USB_CTRL_GET_TIMEOUT);
568 if (ret < 0)
569 return ret;
570 return 0;
571}
572
573static int at76_get_op_mode(struct usb_device *udev)
574{
575 int ret;
Jason Andryuk5a2137d2009-02-21 09:53:22 +0200576 u8 saved;
577 u8 *op_mode;
Kalle Valo1264b952009-02-07 09:27:10 +0200578
Jason Andryuk5a2137d2009-02-21 09:53:22 +0200579 op_mode = kmalloc(1, GFP_NOIO);
580 if (!op_mode)
581 return -ENOMEM;
Kalle Valo1264b952009-02-07 09:27:10 +0200582 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
583 USB_TYPE_VENDOR | USB_DIR_IN |
Jason Andryuk5a2137d2009-02-21 09:53:22 +0200584 USB_RECIP_INTERFACE, 0x01, 0, op_mode, 1,
Kalle Valo1264b952009-02-07 09:27:10 +0200585 USB_CTRL_GET_TIMEOUT);
Jason Andryuk5a2137d2009-02-21 09:53:22 +0200586 saved = *op_mode;
587 kfree(op_mode);
588
Kalle Valo1264b952009-02-07 09:27:10 +0200589 if (ret < 0)
590 return ret;
591 else if (ret < 1)
592 return -EIO;
593 else
Jason Andryuk5a2137d2009-02-21 09:53:22 +0200594 return saved;
Kalle Valo1264b952009-02-07 09:27:10 +0200595}
596
597/* Load a block of the second ("external") part of the firmware */
598static inline int at76_load_ext_fw_block(struct usb_device *udev, int blockno,
599 void *block, int size)
600{
601 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0e,
602 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
603 0x0802, blockno, block, size,
604 USB_CTRL_GET_TIMEOUT);
605}
606
607static inline int at76_get_hw_cfg(struct usb_device *udev,
608 union at76_hwcfg *buf, int buf_size)
609{
610 return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
611 USB_TYPE_VENDOR | USB_DIR_IN |
612 USB_RECIP_INTERFACE, 0x0a02, 0,
613 buf, buf_size, USB_CTRL_GET_TIMEOUT);
614}
615
616/* Intersil boards use a different "value" for GetHWConfig requests */
617static inline int at76_get_hw_cfg_intersil(struct usb_device *udev,
618 union at76_hwcfg *buf, int buf_size)
619{
620 return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
621 USB_TYPE_VENDOR | USB_DIR_IN |
622 USB_RECIP_INTERFACE, 0x0902, 0,
623 buf, buf_size, USB_CTRL_GET_TIMEOUT);
624}
625
626/* Get the hardware configuration for the adapter and put it to the appropriate
627 * fields of 'priv' (the GetHWConfig request and interpretation of the result
628 * depends on the board type) */
629static int at76_get_hw_config(struct at76_priv *priv)
630{
631 int ret;
632 union at76_hwcfg *hwcfg = kmalloc(sizeof(*hwcfg), GFP_KERNEL);
633
634 if (!hwcfg)
635 return -ENOMEM;
636
637 if (at76_is_intersil(priv->board_type)) {
638 ret = at76_get_hw_cfg_intersil(priv->udev, hwcfg,
639 sizeof(hwcfg->i));
640 if (ret < 0)
641 goto exit;
642 memcpy(priv->mac_addr, hwcfg->i.mac_addr, ETH_ALEN);
643 priv->regulatory_domain = hwcfg->i.regulatory_domain;
644 } else if (at76_is_503rfmd(priv->board_type)) {
645 ret = at76_get_hw_cfg(priv->udev, hwcfg, sizeof(hwcfg->r3));
646 if (ret < 0)
647 goto exit;
648 memcpy(priv->mac_addr, hwcfg->r3.mac_addr, ETH_ALEN);
649 priv->regulatory_domain = hwcfg->r3.regulatory_domain;
650 } else {
651 ret = at76_get_hw_cfg(priv->udev, hwcfg, sizeof(hwcfg->r5));
652 if (ret < 0)
653 goto exit;
654 memcpy(priv->mac_addr, hwcfg->r5.mac_addr, ETH_ALEN);
655 priv->regulatory_domain = hwcfg->r5.regulatory_domain;
656 }
657
658exit:
659 kfree(hwcfg);
660 if (ret < 0)
Joe Perchesc96c31e2010-07-26 14:39:58 -0700661 wiphy_err(priv->hw->wiphy, "cannot get hw config (error %d)\n",
662 ret);
Kalle Valo1264b952009-02-07 09:27:10 +0200663
664 return ret;
665}
666
667static struct reg_domain const *at76_get_reg_domain(u16 code)
668{
669 int i;
670 static struct reg_domain const fd_tab[] = {
671 { 0x10, "FCC (USA)", 0x7ff }, /* ch 1-11 */
672 { 0x20, "IC (Canada)", 0x7ff }, /* ch 1-11 */
673 { 0x30, "ETSI (most of Europe)", 0x1fff }, /* ch 1-13 */
674 { 0x31, "Spain", 0x600 }, /* ch 10-11 */
675 { 0x32, "France", 0x1e00 }, /* ch 10-13 */
676 { 0x40, "MKK (Japan)", 0x2000 }, /* ch 14 */
677 { 0x41, "MKK1 (Japan)", 0x3fff }, /* ch 1-14 */
678 { 0x50, "Israel", 0x3fc }, /* ch 3-9 */
679 { 0x00, "<unknown>", 0xffffffff } /* ch 1-32 */
680 };
681
682 /* Last entry is fallback for unknown domain code */
683 for (i = 0; i < ARRAY_SIZE(fd_tab) - 1; i++)
684 if (code == fd_tab[i].code)
685 break;
686
687 return &fd_tab[i];
688}
689
690static inline int at76_get_mib(struct usb_device *udev, u16 mib, void *buf,
691 int buf_size)
692{
693 int ret;
694
695 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
696 USB_TYPE_VENDOR | USB_DIR_IN |
697 USB_RECIP_INTERFACE, mib << 8, 0, buf, buf_size,
698 USB_CTRL_GET_TIMEOUT);
699 if (ret >= 0 && ret != buf_size)
700 return -EIO;
701 return ret;
702}
703
704/* Return positive number for status, negative for an error */
705static inline int at76_get_cmd_status(struct usb_device *udev, u8 cmd)
706{
Jason Andryuk5a2137d2009-02-21 09:53:22 +0200707 u8 *stat_buf;
Kalle Valo1264b952009-02-07 09:27:10 +0200708 int ret;
709
Jason Andryuk5a2137d2009-02-21 09:53:22 +0200710 stat_buf = kmalloc(40, GFP_NOIO);
711 if (!stat_buf)
712 return -ENOMEM;
Kalle Valo1264b952009-02-07 09:27:10 +0200713
Jason Andryuk5a2137d2009-02-21 09:53:22 +0200714 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x22,
715 USB_TYPE_VENDOR | USB_DIR_IN |
716 USB_RECIP_INTERFACE, cmd, 0, stat_buf,
717 40, USB_CTRL_GET_TIMEOUT);
718 if (ret >= 0)
719 ret = stat_buf[5];
720 kfree(stat_buf);
721
722 return ret;
Kalle Valo1264b952009-02-07 09:27:10 +0200723}
724
725#define MAKE_CMD_CASE(c) case (c): return #c
Kalle Valo1264b952009-02-07 09:27:10 +0200726static const char *at76_get_cmd_string(u8 cmd_status)
727{
728 switch (cmd_status) {
729 MAKE_CMD_CASE(CMD_SET_MIB);
730 MAKE_CMD_CASE(CMD_GET_MIB);
731 MAKE_CMD_CASE(CMD_SCAN);
732 MAKE_CMD_CASE(CMD_JOIN);
733 MAKE_CMD_CASE(CMD_START_IBSS);
734 MAKE_CMD_CASE(CMD_RADIO_ON);
735 MAKE_CMD_CASE(CMD_RADIO_OFF);
736 MAKE_CMD_CASE(CMD_STARTUP);
737 }
738
739 return "UNKNOWN";
740}
741
Jason Andryuk5a2137d2009-02-21 09:53:22 +0200742static int at76_set_card_command(struct usb_device *udev, u8 cmd, void *buf,
Kalle Valo1264b952009-02-07 09:27:10 +0200743 int buf_size)
744{
745 int ret;
746 struct at76_command *cmd_buf = kmalloc(sizeof(struct at76_command) +
747 buf_size, GFP_KERNEL);
748
749 if (!cmd_buf)
750 return -ENOMEM;
751
752 cmd_buf->cmd = cmd;
753 cmd_buf->reserved = 0;
754 cmd_buf->size = cpu_to_le16(buf_size);
755 memcpy(cmd_buf->data, buf, buf_size);
756
757 at76_dbg_dump(DBG_CMD, cmd_buf, sizeof(struct at76_command) + buf_size,
758 "issuing command %s (0x%02x)",
759 at76_get_cmd_string(cmd), cmd);
760
761 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0e,
762 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
763 0, 0, cmd_buf,
764 sizeof(struct at76_command) + buf_size,
765 USB_CTRL_GET_TIMEOUT);
766 kfree(cmd_buf);
767 return ret;
768}
769
770#define MAKE_CMD_STATUS_CASE(c) case (c): return #c
771static const char *at76_get_cmd_status_string(u8 cmd_status)
772{
773 switch (cmd_status) {
774 MAKE_CMD_STATUS_CASE(CMD_STATUS_IDLE);
775 MAKE_CMD_STATUS_CASE(CMD_STATUS_COMPLETE);
776 MAKE_CMD_STATUS_CASE(CMD_STATUS_UNKNOWN);
777 MAKE_CMD_STATUS_CASE(CMD_STATUS_INVALID_PARAMETER);
778 MAKE_CMD_STATUS_CASE(CMD_STATUS_FUNCTION_NOT_SUPPORTED);
779 MAKE_CMD_STATUS_CASE(CMD_STATUS_TIME_OUT);
780 MAKE_CMD_STATUS_CASE(CMD_STATUS_IN_PROGRESS);
781 MAKE_CMD_STATUS_CASE(CMD_STATUS_HOST_FAILURE);
782 MAKE_CMD_STATUS_CASE(CMD_STATUS_SCAN_FAILED);
783 }
784
785 return "UNKNOWN";
786}
787
788/* Wait until the command is completed */
789static int at76_wait_completion(struct at76_priv *priv, int cmd)
790{
791 int status = 0;
792 unsigned long timeout = jiffies + CMD_COMPLETION_TIMEOUT;
793
794 do {
795 status = at76_get_cmd_status(priv->udev, cmd);
796 if (status < 0) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700797 wiphy_err(priv->hw->wiphy,
798 "at76_get_cmd_status failed: %d\n",
799 status);
Kalle Valo1264b952009-02-07 09:27:10 +0200800 break;
801 }
802
803 at76_dbg(DBG_WAIT_COMPLETE,
804 "%s: Waiting on cmd %d, status = %d (%s)",
805 wiphy_name(priv->hw->wiphy), cmd, status,
806 at76_get_cmd_status_string(status));
807
808 if (status != CMD_STATUS_IN_PROGRESS
809 && status != CMD_STATUS_IDLE)
810 break;
811
812 schedule_timeout_interruptible(HZ / 10); /* 100 ms */
813 if (time_after(jiffies, timeout)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700814 wiphy_err(priv->hw->wiphy,
815 "completion timeout for command %d\n", cmd);
Kalle Valo1264b952009-02-07 09:27:10 +0200816 status = -ETIMEDOUT;
817 break;
818 }
819 } while (1);
820
821 return status;
822}
823
824static int at76_set_mib(struct at76_priv *priv, struct set_mib_buffer *buf)
825{
826 int ret;
827
828 ret = at76_set_card_command(priv->udev, CMD_SET_MIB, buf,
829 offsetof(struct set_mib_buffer,
830 data) + buf->size);
831 if (ret < 0)
832 return ret;
833
834 ret = at76_wait_completion(priv, CMD_SET_MIB);
835 if (ret != CMD_STATUS_COMPLETE) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700836 wiphy_info(priv->hw->wiphy,
837 "set_mib: at76_wait_completion failed with %d\n",
838 ret);
Kalle Valo1264b952009-02-07 09:27:10 +0200839 ret = -EIO;
840 }
841
842 return ret;
843}
844
845/* Return < 0 on error, == 0 if no command sent, == 1 if cmd sent */
846static int at76_set_radio(struct at76_priv *priv, int enable)
847{
848 int ret;
849 int cmd;
850
851 if (priv->radio_on == enable)
852 return 0;
853
854 cmd = enable ? CMD_RADIO_ON : CMD_RADIO_OFF;
855
856 ret = at76_set_card_command(priv->udev, cmd, NULL, 0);
857 if (ret < 0)
Joe Perchesc96c31e2010-07-26 14:39:58 -0700858 wiphy_err(priv->hw->wiphy,
859 "at76_set_card_command(%d) failed: %d\n", cmd, ret);
Kalle Valo1264b952009-02-07 09:27:10 +0200860 else
861 ret = 1;
862
863 priv->radio_on = enable;
864 return ret;
865}
866
867/* Set current power save mode (AT76_PM_OFF/AT76_PM_ON/AT76_PM_SMART) */
868static int at76_set_pm_mode(struct at76_priv *priv)
869{
870 int ret = 0;
871
872 priv->mib_buf.type = MIB_MAC_MGMT;
873 priv->mib_buf.size = 1;
874 priv->mib_buf.index = offsetof(struct mib_mac_mgmt, power_mgmt_mode);
875 priv->mib_buf.data.byte = priv->pm_mode;
876
877 ret = at76_set_mib(priv, &priv->mib_buf);
878 if (ret < 0)
Joe Perchesc96c31e2010-07-26 14:39:58 -0700879 wiphy_err(priv->hw->wiphy, "set_mib (pm_mode) failed: %d\n",
880 ret);
Kalle Valo1264b952009-02-07 09:27:10 +0200881
882 return ret;
883}
884
885static int at76_set_preamble(struct at76_priv *priv, u8 type)
886{
887 int ret = 0;
888
889 priv->mib_buf.type = MIB_LOCAL;
890 priv->mib_buf.size = 1;
891 priv->mib_buf.index = offsetof(struct mib_local, preamble_type);
892 priv->mib_buf.data.byte = type;
893
894 ret = at76_set_mib(priv, &priv->mib_buf);
895 if (ret < 0)
Joe Perchesc96c31e2010-07-26 14:39:58 -0700896 wiphy_err(priv->hw->wiphy, "set_mib (preamble) failed: %d\n",
897 ret);
Kalle Valo1264b952009-02-07 09:27:10 +0200898
899 return ret;
900}
901
902static int at76_set_frag(struct at76_priv *priv, u16 size)
903{
904 int ret = 0;
905
906 priv->mib_buf.type = MIB_MAC;
907 priv->mib_buf.size = 2;
908 priv->mib_buf.index = offsetof(struct mib_mac, frag_threshold);
909 priv->mib_buf.data.word = cpu_to_le16(size);
910
911 ret = at76_set_mib(priv, &priv->mib_buf);
912 if (ret < 0)
Joe Perchesc96c31e2010-07-26 14:39:58 -0700913 wiphy_err(priv->hw->wiphy,
914 "set_mib (frag threshold) failed: %d\n", ret);
Kalle Valo1264b952009-02-07 09:27:10 +0200915
916 return ret;
917}
918
919static int at76_set_rts(struct at76_priv *priv, u16 size)
920{
921 int ret = 0;
922
923 priv->mib_buf.type = MIB_MAC;
924 priv->mib_buf.size = 2;
925 priv->mib_buf.index = offsetof(struct mib_mac, rts_threshold);
926 priv->mib_buf.data.word = cpu_to_le16(size);
927
928 ret = at76_set_mib(priv, &priv->mib_buf);
929 if (ret < 0)
Joe Perchesc96c31e2010-07-26 14:39:58 -0700930 wiphy_err(priv->hw->wiphy, "set_mib (rts) failed: %d\n", ret);
Kalle Valo1264b952009-02-07 09:27:10 +0200931
932 return ret;
933}
934
935static int at76_set_autorate_fallback(struct at76_priv *priv, int onoff)
936{
937 int ret = 0;
938
939 priv->mib_buf.type = MIB_LOCAL;
940 priv->mib_buf.size = 1;
941 priv->mib_buf.index = offsetof(struct mib_local, txautorate_fallback);
942 priv->mib_buf.data.byte = onoff;
943
944 ret = at76_set_mib(priv, &priv->mib_buf);
945 if (ret < 0)
Joe Perchesc96c31e2010-07-26 14:39:58 -0700946 wiphy_err(priv->hw->wiphy,
947 "set_mib (autorate fallback) failed: %d\n", ret);
Kalle Valo1264b952009-02-07 09:27:10 +0200948
949 return ret;
950}
951
952static void at76_dump_mib_mac_addr(struct at76_priv *priv)
953{
954 int i;
955 int ret;
956 struct mib_mac_addr *m = kmalloc(sizeof(struct mib_mac_addr),
957 GFP_KERNEL);
958
959 if (!m)
960 return;
961
962 ret = at76_get_mib(priv->udev, MIB_MAC_ADDR, m,
963 sizeof(struct mib_mac_addr));
964 if (ret < 0) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700965 wiphy_err(priv->hw->wiphy,
966 "at76_get_mib (mac_addr) failed: %d\n", ret);
Kalle Valo1264b952009-02-07 09:27:10 +0200967 goto exit;
968 }
969
Jason Andryuka6ef92a2009-11-08 18:58:01 -0500970 at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: mac_addr %pM res 0x%x 0x%x",
Kalle Valo1264b952009-02-07 09:27:10 +0200971 wiphy_name(priv->hw->wiphy),
Jason Andryuka6ef92a2009-11-08 18:58:01 -0500972 m->mac_addr, m->res[0], m->res[1]);
Kalle Valo1264b952009-02-07 09:27:10 +0200973 for (i = 0; i < ARRAY_SIZE(m->group_addr); i++)
Jason Andryuka6ef92a2009-11-08 18:58:01 -0500974 at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: group addr %d: %pM, "
Kalle Valo1264b952009-02-07 09:27:10 +0200975 "status %d", wiphy_name(priv->hw->wiphy), i,
Jason Andryuka6ef92a2009-11-08 18:58:01 -0500976 m->group_addr[i], m->group_addr_status[i]);
Kalle Valo1264b952009-02-07 09:27:10 +0200977exit:
978 kfree(m);
979}
980
981static void at76_dump_mib_mac_wep(struct at76_priv *priv)
982{
983 int i;
984 int ret;
985 int key_len;
986 struct mib_mac_wep *m = kmalloc(sizeof(struct mib_mac_wep), GFP_KERNEL);
987
988 if (!m)
989 return;
990
991 ret = at76_get_mib(priv->udev, MIB_MAC_WEP, m,
992 sizeof(struct mib_mac_wep));
993 if (ret < 0) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700994 wiphy_err(priv->hw->wiphy,
995 "at76_get_mib (mac_wep) failed: %d\n", ret);
Kalle Valo1264b952009-02-07 09:27:10 +0200996 goto exit;
997 }
998
999 at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: priv_invoked %u def_key_id %u "
1000 "key_len %u excl_unencr %u wep_icv_err %u wep_excluded %u "
1001 "encr_level %u key %d", wiphy_name(priv->hw->wiphy),
1002 m->privacy_invoked, m->wep_default_key_id,
1003 m->wep_key_mapping_len, m->exclude_unencrypted,
1004 le32_to_cpu(m->wep_icv_error_count),
1005 le32_to_cpu(m->wep_excluded_count), m->encryption_level,
1006 m->wep_default_key_id);
1007
1008 key_len = (m->encryption_level == 1) ?
1009 WEP_SMALL_KEY_LEN : WEP_LARGE_KEY_LEN;
1010
1011 for (i = 0; i < WEP_KEYS; i++)
1012 at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: key %d: %s",
1013 wiphy_name(priv->hw->wiphy), i,
1014 hex2str(m->wep_default_keyvalue[i], key_len));
1015exit:
1016 kfree(m);
1017}
1018
1019static void at76_dump_mib_mac_mgmt(struct at76_priv *priv)
1020{
1021 int ret;
1022 struct mib_mac_mgmt *m = kmalloc(sizeof(struct mib_mac_mgmt),
1023 GFP_KERNEL);
1024
1025 if (!m)
1026 return;
1027
1028 ret = at76_get_mib(priv->udev, MIB_MAC_MGMT, m,
1029 sizeof(struct mib_mac_mgmt));
1030 if (ret < 0) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001031 wiphy_err(priv->hw->wiphy,
1032 "at76_get_mib (mac_mgmt) failed: %d\n", ret);
Kalle Valo1264b952009-02-07 09:27:10 +02001033 goto exit;
1034 }
1035
1036 at76_dbg(DBG_MIB, "%s: MIB MAC_MGMT: beacon_period %d CFP_max_duration "
1037 "%d medium_occupancy_limit %d station_id 0x%x ATIM_window %d "
1038 "CFP_mode %d privacy_opt_impl %d DTIM_period %d CFP_period %d "
Jason Andryuka6ef92a2009-11-08 18:58:01 -05001039 "current_bssid %pM current_essid %s current_bss_type %d "
Kalle Valo1264b952009-02-07 09:27:10 +02001040 "pm_mode %d ibss_change %d res %d "
1041 "multi_domain_capability_implemented %d "
1042 "international_roaming %d country_string %.3s",
1043 wiphy_name(priv->hw->wiphy), le16_to_cpu(m->beacon_period),
1044 le16_to_cpu(m->CFP_max_duration),
1045 le16_to_cpu(m->medium_occupancy_limit),
1046 le16_to_cpu(m->station_id), le16_to_cpu(m->ATIM_window),
1047 m->CFP_mode, m->privacy_option_implemented, m->DTIM_period,
Jason Andryuka6ef92a2009-11-08 18:58:01 -05001048 m->CFP_period, m->current_bssid,
Kalle Valo1264b952009-02-07 09:27:10 +02001049 hex2str(m->current_essid, IW_ESSID_MAX_SIZE),
1050 m->current_bss_type, m->power_mgmt_mode, m->ibss_change,
1051 m->res, m->multi_domain_capability_implemented,
1052 m->multi_domain_capability_enabled, m->country_string);
1053exit:
1054 kfree(m);
1055}
1056
1057static void at76_dump_mib_mac(struct at76_priv *priv)
1058{
1059 int ret;
1060 struct mib_mac *m = kmalloc(sizeof(struct mib_mac), GFP_KERNEL);
1061
1062 if (!m)
1063 return;
1064
1065 ret = at76_get_mib(priv->udev, MIB_MAC, m, sizeof(struct mib_mac));
1066 if (ret < 0) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001067 wiphy_err(priv->hw->wiphy,
1068 "at76_get_mib (mac) failed: %d\n", ret);
Kalle Valo1264b952009-02-07 09:27:10 +02001069 goto exit;
1070 }
1071
1072 at76_dbg(DBG_MIB, "%s: MIB MAC: max_tx_msdu_lifetime %d "
1073 "max_rx_lifetime %d frag_threshold %d rts_threshold %d "
1074 "cwmin %d cwmax %d short_retry_time %d long_retry_time %d "
1075 "scan_type %d scan_channel %d probe_delay %u "
1076 "min_channel_time %d max_channel_time %d listen_int %d "
Jason Andryuka6ef92a2009-11-08 18:58:01 -05001077 "desired_ssid %s desired_bssid %pM desired_bsstype %d",
Kalle Valo1264b952009-02-07 09:27:10 +02001078 wiphy_name(priv->hw->wiphy),
1079 le32_to_cpu(m->max_tx_msdu_lifetime),
1080 le32_to_cpu(m->max_rx_lifetime),
1081 le16_to_cpu(m->frag_threshold), le16_to_cpu(m->rts_threshold),
1082 le16_to_cpu(m->cwmin), le16_to_cpu(m->cwmax),
1083 m->short_retry_time, m->long_retry_time, m->scan_type,
1084 m->scan_channel, le16_to_cpu(m->probe_delay),
1085 le16_to_cpu(m->min_channel_time),
1086 le16_to_cpu(m->max_channel_time),
1087 le16_to_cpu(m->listen_interval),
1088 hex2str(m->desired_ssid, IW_ESSID_MAX_SIZE),
Jason Andryuka6ef92a2009-11-08 18:58:01 -05001089 m->desired_bssid, m->desired_bsstype);
Kalle Valo1264b952009-02-07 09:27:10 +02001090exit:
1091 kfree(m);
1092}
1093
1094static void at76_dump_mib_phy(struct at76_priv *priv)
1095{
1096 int ret;
1097 struct mib_phy *m = kmalloc(sizeof(struct mib_phy), GFP_KERNEL);
1098
1099 if (!m)
1100 return;
1101
1102 ret = at76_get_mib(priv->udev, MIB_PHY, m, sizeof(struct mib_phy));
1103 if (ret < 0) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001104 wiphy_err(priv->hw->wiphy,
1105 "at76_get_mib (phy) failed: %d\n", ret);
Kalle Valo1264b952009-02-07 09:27:10 +02001106 goto exit;
1107 }
1108
1109 at76_dbg(DBG_MIB, "%s: MIB PHY: ed_threshold %d slot_time %d "
1110 "sifs_time %d preamble_length %d plcp_header_length %d "
1111 "mpdu_max_length %d cca_mode_supported %d operation_rate_set "
1112 "0x%x 0x%x 0x%x 0x%x channel_id %d current_cca_mode %d "
1113 "phy_type %d current_reg_domain %d",
1114 wiphy_name(priv->hw->wiphy), le32_to_cpu(m->ed_threshold),
1115 le16_to_cpu(m->slot_time), le16_to_cpu(m->sifs_time),
1116 le16_to_cpu(m->preamble_length),
1117 le16_to_cpu(m->plcp_header_length),
1118 le16_to_cpu(m->mpdu_max_length),
1119 le16_to_cpu(m->cca_mode_supported), m->operation_rate_set[0],
1120 m->operation_rate_set[1], m->operation_rate_set[2],
1121 m->operation_rate_set[3], m->channel_id, m->current_cca_mode,
1122 m->phy_type, m->current_reg_domain);
1123exit:
1124 kfree(m);
1125}
1126
1127static void at76_dump_mib_local(struct at76_priv *priv)
1128{
1129 int ret;
1130 struct mib_local *m = kmalloc(sizeof(struct mib_phy), GFP_KERNEL);
1131
1132 if (!m)
1133 return;
1134
1135 ret = at76_get_mib(priv->udev, MIB_LOCAL, m, sizeof(struct mib_local));
1136 if (ret < 0) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001137 wiphy_err(priv->hw->wiphy,
1138 "at76_get_mib (local) failed: %d\n", ret);
Kalle Valo1264b952009-02-07 09:27:10 +02001139 goto exit;
1140 }
1141
1142 at76_dbg(DBG_MIB, "%s: MIB LOCAL: beacon_enable %d "
1143 "txautorate_fallback %d ssid_size %d promiscuous_mode %d "
1144 "preamble_type %d", wiphy_name(priv->hw->wiphy),
1145 m->beacon_enable,
1146 m->txautorate_fallback, m->ssid_size, m->promiscuous_mode,
1147 m->preamble_type);
1148exit:
1149 kfree(m);
1150}
1151
1152static void at76_dump_mib_mdomain(struct at76_priv *priv)
1153{
1154 int ret;
1155 struct mib_mdomain *m = kmalloc(sizeof(struct mib_mdomain), GFP_KERNEL);
1156
1157 if (!m)
1158 return;
1159
1160 ret = at76_get_mib(priv->udev, MIB_MDOMAIN, m,
1161 sizeof(struct mib_mdomain));
1162 if (ret < 0) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001163 wiphy_err(priv->hw->wiphy,
1164 "at76_get_mib (mdomain) failed: %d\n", ret);
Kalle Valo1264b952009-02-07 09:27:10 +02001165 goto exit;
1166 }
1167
1168 at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: channel_list %s",
1169 wiphy_name(priv->hw->wiphy),
1170 hex2str(m->channel_list, sizeof(m->channel_list)));
1171
1172 at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: tx_powerlevel %s",
1173 wiphy_name(priv->hw->wiphy),
1174 hex2str(m->tx_powerlevel, sizeof(m->tx_powerlevel)));
1175exit:
1176 kfree(m);
1177}
1178
1179/* Enable monitor mode */
1180static int at76_start_monitor(struct at76_priv *priv)
1181{
1182 struct at76_req_scan scan;
1183 int ret;
1184
1185 memset(&scan, 0, sizeof(struct at76_req_scan));
1186 memset(scan.bssid, 0xff, ETH_ALEN);
1187
1188 scan.channel = priv->channel;
1189 scan.scan_type = SCAN_TYPE_PASSIVE;
1190 scan.international_scan = 0;
Jason Andryuk13b409c2009-11-08 19:02:19 -05001191 scan.min_channel_time = cpu_to_le16(priv->scan_min_time);
1192 scan.max_channel_time = cpu_to_le16(priv->scan_max_time);
1193 scan.probe_delay = cpu_to_le16(0);
Kalle Valo1264b952009-02-07 09:27:10 +02001194
1195 ret = at76_set_card_command(priv->udev, CMD_SCAN, &scan, sizeof(scan));
1196 if (ret >= 0)
1197 ret = at76_get_cmd_status(priv->udev, CMD_SCAN);
1198
1199 return ret;
1200}
1201
1202/* Calculate padding from txbuf->wlength (which excludes the USB TX header),
1203 likely to compensate a flaw in the AT76C503A USB part ... */
1204static inline int at76_calc_padding(int wlen)
1205{
1206 /* add the USB TX header */
1207 wlen += AT76_TX_HDRLEN;
1208
1209 wlen = wlen % 64;
1210
1211 if (wlen < 50)
1212 return 50 - wlen;
1213
1214 if (wlen >= 61)
1215 return 64 + 50 - wlen;
1216
1217 return 0;
1218}
1219
1220static void at76_rx_callback(struct urb *urb)
1221{
1222 struct at76_priv *priv = urb->context;
1223
1224 priv->rx_tasklet.data = (unsigned long)urb;
1225 tasklet_schedule(&priv->rx_tasklet);
Kalle Valo1264b952009-02-07 09:27:10 +02001226}
1227
1228static int at76_submit_rx_urb(struct at76_priv *priv)
1229{
1230 int ret;
1231 int size;
1232 struct sk_buff *skb = priv->rx_skb;
1233
1234 if (!priv->rx_urb) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001235 wiphy_err(priv->hw->wiphy, "%s: priv->rx_urb is null\n",
1236 __func__);
Kalle Valo1264b952009-02-07 09:27:10 +02001237 return -EFAULT;
1238 }
1239
1240 if (!skb) {
1241 skb = dev_alloc_skb(sizeof(struct at76_rx_buffer));
1242 if (!skb) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001243 wiphy_err(priv->hw->wiphy,
1244 "cannot allocate rx skbuff\n");
Kalle Valo1264b952009-02-07 09:27:10 +02001245 ret = -ENOMEM;
1246 goto exit;
1247 }
1248 priv->rx_skb = skb;
1249 } else {
1250 skb_push(skb, skb_headroom(skb));
1251 skb_trim(skb, 0);
1252 }
1253
1254 size = skb_tailroom(skb);
1255 usb_fill_bulk_urb(priv->rx_urb, priv->udev, priv->rx_pipe,
1256 skb_put(skb, size), size, at76_rx_callback, priv);
1257 ret = usb_submit_urb(priv->rx_urb, GFP_ATOMIC);
1258 if (ret < 0) {
1259 if (ret == -ENODEV)
1260 at76_dbg(DBG_DEVSTART,
1261 "usb_submit_urb returned -ENODEV");
1262 else
Joe Perchesc96c31e2010-07-26 14:39:58 -07001263 wiphy_err(priv->hw->wiphy,
1264 "rx, usb_submit_urb failed: %d\n", ret);
Kalle Valo1264b952009-02-07 09:27:10 +02001265 }
1266
1267exit:
1268 if (ret < 0 && ret != -ENODEV)
Joe Perchesc96c31e2010-07-26 14:39:58 -07001269 wiphy_err(priv->hw->wiphy,
1270 "cannot submit rx urb - please unload the driver and/or power cycle the device\n");
Kalle Valo1264b952009-02-07 09:27:10 +02001271
1272 return ret;
1273}
1274
1275/* Download external firmware */
1276static int at76_load_external_fw(struct usb_device *udev, struct fwentry *fwe)
1277{
1278 int ret;
1279 int op_mode;
1280 int blockno = 0;
1281 int bsize;
1282 u8 *block;
1283 u8 *buf = fwe->extfw;
1284 int size = fwe->extfw_size;
1285
1286 if (!buf || !size)
1287 return -ENOENT;
1288
1289 op_mode = at76_get_op_mode(udev);
1290 at76_dbg(DBG_DEVSTART, "opmode %d", op_mode);
1291
1292 if (op_mode != OPMODE_NORMAL_NIC_WITHOUT_FLASH) {
1293 dev_printk(KERN_ERR, &udev->dev, "unexpected opmode %d\n",
1294 op_mode);
1295 return -EINVAL;
1296 }
1297
1298 block = kmalloc(FW_BLOCK_SIZE, GFP_KERNEL);
1299 if (!block)
1300 return -ENOMEM;
1301
1302 at76_dbg(DBG_DEVSTART, "downloading external firmware");
1303
1304 /* for fw >= 0.100, the device needs an extra empty block */
1305 do {
1306 bsize = min_t(int, size, FW_BLOCK_SIZE);
1307 memcpy(block, buf, bsize);
1308 at76_dbg(DBG_DEVSTART,
1309 "ext fw, size left = %5d, bsize = %4d, blockno = %2d",
1310 size, bsize, blockno);
1311 ret = at76_load_ext_fw_block(udev, blockno, block, bsize);
1312 if (ret != bsize) {
1313 dev_printk(KERN_ERR, &udev->dev,
1314 "loading %dth firmware block failed: %d\n",
1315 blockno, ret);
1316 goto exit;
1317 }
1318 buf += bsize;
1319 size -= bsize;
1320 blockno++;
1321 } while (bsize > 0);
1322
1323 if (at76_is_505a(fwe->board_type)) {
1324 at76_dbg(DBG_DEVSTART, "200 ms delay for 505a");
1325 schedule_timeout_interruptible(HZ / 5 + 1);
1326 }
1327
1328exit:
1329 kfree(block);
1330 if (ret < 0)
1331 dev_printk(KERN_ERR, &udev->dev,
1332 "downloading external firmware failed: %d\n", ret);
1333 return ret;
1334}
1335
1336/* Download internal firmware */
1337static int at76_load_internal_fw(struct usb_device *udev, struct fwentry *fwe)
1338{
1339 int ret;
1340 int need_remap = !at76_is_505a(fwe->board_type);
1341
1342 ret = at76_usbdfu_download(udev, fwe->intfw, fwe->intfw_size,
1343 need_remap ? 0 : 2 * HZ);
1344
1345 if (ret < 0) {
1346 dev_printk(KERN_ERR, &udev->dev,
1347 "downloading internal fw failed with %d\n", ret);
1348 goto exit;
1349 }
1350
1351 at76_dbg(DBG_DEVSTART, "sending REMAP");
1352
1353 /* no REMAP for 505A (see SF driver) */
1354 if (need_remap) {
1355 ret = at76_remap(udev);
1356 if (ret < 0) {
1357 dev_printk(KERN_ERR, &udev->dev,
1358 "sending REMAP failed with %d\n", ret);
1359 goto exit;
1360 }
1361 }
1362
1363 at76_dbg(DBG_DEVSTART, "sleeping for 2 seconds");
1364 schedule_timeout_interruptible(2 * HZ + 1);
1365 usb_reset_device(udev);
1366
1367exit:
1368 return ret;
1369}
1370
1371static int at76_startup_device(struct at76_priv *priv)
1372{
1373 struct at76_card_config *ccfg = &priv->card_config;
1374 int ret;
1375
1376 at76_dbg(DBG_PARAMS,
1377 "%s param: ssid %.*s (%s) mode %s ch %d wep %s key %d "
1378 "keylen %d", wiphy_name(priv->hw->wiphy), priv->essid_size,
1379 priv->essid, hex2str(priv->essid, IW_ESSID_MAX_SIZE),
1380 priv->iw_mode == IW_MODE_ADHOC ? "adhoc" : "infra",
1381 priv->channel, priv->wep_enabled ? "enabled" : "disabled",
1382 priv->wep_key_id, priv->wep_keys_len[priv->wep_key_id]);
1383 at76_dbg(DBG_PARAMS,
1384 "%s param: preamble %s rts %d retry %d frag %d "
1385 "txrate %s auth_mode %d", wiphy_name(priv->hw->wiphy),
1386 preambles[priv->preamble_type], priv->rts_threshold,
1387 priv->short_retry_limit, priv->frag_threshold,
1388 priv->txrate == TX_RATE_1MBIT ? "1MBit" : priv->txrate ==
1389 TX_RATE_2MBIT ? "2MBit" : priv->txrate ==
1390 TX_RATE_5_5MBIT ? "5.5MBit" : priv->txrate ==
1391 TX_RATE_11MBIT ? "11MBit" : priv->txrate ==
1392 TX_RATE_AUTO ? "auto" : "<invalid>", priv->auth_mode);
1393 at76_dbg(DBG_PARAMS,
1394 "%s param: pm_mode %d pm_period %d auth_mode %s "
1395 "scan_times %d %d scan_mode %s",
1396 wiphy_name(priv->hw->wiphy), priv->pm_mode, priv->pm_period,
1397 priv->auth_mode == WLAN_AUTH_OPEN ? "open" : "shared_secret",
1398 priv->scan_min_time, priv->scan_max_time,
1399 priv->scan_mode == SCAN_TYPE_ACTIVE ? "active" : "passive");
1400
1401 memset(ccfg, 0, sizeof(struct at76_card_config));
1402 ccfg->promiscuous_mode = 0;
1403 ccfg->short_retry_limit = priv->short_retry_limit;
1404
1405 if (priv->wep_enabled) {
1406 if (priv->wep_keys_len[priv->wep_key_id] > WEP_SMALL_KEY_LEN)
1407 ccfg->encryption_type = 2;
1408 else
1409 ccfg->encryption_type = 1;
1410
1411 /* jal: always exclude unencrypted if WEP is active */
1412 ccfg->exclude_unencrypted = 1;
1413 } else {
1414 ccfg->exclude_unencrypted = 0;
1415 ccfg->encryption_type = 0;
1416 }
1417
1418 ccfg->rts_threshold = cpu_to_le16(priv->rts_threshold);
1419 ccfg->fragmentation_threshold = cpu_to_le16(priv->frag_threshold);
1420
1421 memcpy(ccfg->basic_rate_set, hw_rates, 4);
1422 /* jal: really needed, we do a set_mib for autorate later ??? */
1423 ccfg->auto_rate_fallback = (priv->txrate == TX_RATE_AUTO ? 1 : 0);
1424 ccfg->channel = priv->channel;
1425 ccfg->privacy_invoked = priv->wep_enabled;
1426 memcpy(ccfg->current_ssid, priv->essid, IW_ESSID_MAX_SIZE);
1427 ccfg->ssid_len = priv->essid_size;
1428
1429 ccfg->wep_default_key_id = priv->wep_key_id;
1430 memcpy(ccfg->wep_default_key_value, priv->wep_keys,
1431 sizeof(priv->wep_keys));
1432
1433 ccfg->short_preamble = priv->preamble_type;
1434 ccfg->beacon_period = cpu_to_le16(priv->beacon_period);
1435
1436 ret = at76_set_card_command(priv->udev, CMD_STARTUP, &priv->card_config,
1437 sizeof(struct at76_card_config));
1438 if (ret < 0) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001439 wiphy_err(priv->hw->wiphy, "at76_set_card_command failed: %d\n",
1440 ret);
Kalle Valo1264b952009-02-07 09:27:10 +02001441 return ret;
1442 }
1443
1444 at76_wait_completion(priv, CMD_STARTUP);
1445
1446 /* remove BSSID from previous run */
1447 memset(priv->bssid, 0, ETH_ALEN);
1448
1449 if (at76_set_radio(priv, 1) == 1)
1450 at76_wait_completion(priv, CMD_RADIO_ON);
1451
1452 ret = at76_set_preamble(priv, priv->preamble_type);
1453 if (ret < 0)
1454 return ret;
1455
1456 ret = at76_set_frag(priv, priv->frag_threshold);
1457 if (ret < 0)
1458 return ret;
1459
1460 ret = at76_set_rts(priv, priv->rts_threshold);
1461 if (ret < 0)
1462 return ret;
1463
1464 ret = at76_set_autorate_fallback(priv,
1465 priv->txrate == TX_RATE_AUTO ? 1 : 0);
1466 if (ret < 0)
1467 return ret;
1468
1469 ret = at76_set_pm_mode(priv);
1470 if (ret < 0)
1471 return ret;
1472
1473 if (at76_debug & DBG_MIB) {
1474 at76_dump_mib_mac(priv);
1475 at76_dump_mib_mac_addr(priv);
1476 at76_dump_mib_mac_mgmt(priv);
1477 at76_dump_mib_mac_wep(priv);
1478 at76_dump_mib_mdomain(priv);
1479 at76_dump_mib_phy(priv);
1480 at76_dump_mib_local(priv);
1481 }
1482
1483 return 0;
1484}
1485
1486/* Enable or disable promiscuous mode */
1487static void at76_work_set_promisc(struct work_struct *work)
1488{
1489 struct at76_priv *priv = container_of(work, struct at76_priv,
1490 work_set_promisc);
1491 int ret = 0;
1492
Jason Andryuk1cc198f2009-02-21 09:53:29 +02001493 if (priv->device_unplugged)
1494 return;
1495
Kalle Valo1264b952009-02-07 09:27:10 +02001496 mutex_lock(&priv->mtx);
1497
1498 priv->mib_buf.type = MIB_LOCAL;
1499 priv->mib_buf.size = 1;
1500 priv->mib_buf.index = offsetof(struct mib_local, promiscuous_mode);
1501 priv->mib_buf.data.byte = priv->promisc ? 1 : 0;
1502
1503 ret = at76_set_mib(priv, &priv->mib_buf);
1504 if (ret < 0)
Joe Perchesc96c31e2010-07-26 14:39:58 -07001505 wiphy_err(priv->hw->wiphy,
1506 "set_mib (promiscuous_mode) failed: %d\n", ret);
Kalle Valo1264b952009-02-07 09:27:10 +02001507
1508 mutex_unlock(&priv->mtx);
1509}
1510
1511/* Submit Rx urb back to the device */
1512static void at76_work_submit_rx(struct work_struct *work)
1513{
1514 struct at76_priv *priv = container_of(work, struct at76_priv,
1515 work_submit_rx);
1516
1517 mutex_lock(&priv->mtx);
1518 at76_submit_rx_urb(priv);
1519 mutex_unlock(&priv->mtx);
1520}
1521
1522static void at76_rx_tasklet(unsigned long param)
1523{
1524 struct urb *urb = (struct urb *)param;
1525 struct at76_priv *priv = urb->context;
1526 struct at76_rx_buffer *buf;
1527 struct ieee80211_rx_status rx_status = { 0 };
1528
1529 if (priv->device_unplugged) {
1530 at76_dbg(DBG_DEVSTART, "device unplugged");
1531 if (urb)
1532 at76_dbg(DBG_DEVSTART, "urb status %d", urb->status);
1533 return;
1534 }
1535
1536 if (!priv->rx_skb || !priv->rx_skb->data)
1537 return;
1538
1539 buf = (struct at76_rx_buffer *)priv->rx_skb->data;
1540
1541 if (urb->status != 0) {
1542 if (urb->status != -ENOENT && urb->status != -ECONNRESET)
1543 at76_dbg(DBG_URB,
1544 "%s %s: - nonzero Rx bulk status received: %d",
1545 __func__, wiphy_name(priv->hw->wiphy),
1546 urb->status);
1547 return;
1548 }
1549
1550 at76_dbg(DBG_RX_ATMEL_HDR,
1551 "%s: rx frame: rate %d rssi %d noise %d link %d",
1552 wiphy_name(priv->hw->wiphy), buf->rx_rate, buf->rssi,
1553 buf->noise_level, buf->link_quality);
1554
1555 skb_pull(priv->rx_skb, AT76_RX_HDRLEN);
1556 skb_trim(priv->rx_skb, le16_to_cpu(buf->wlength));
1557 at76_dbg_dump(DBG_RX_DATA, priv->rx_skb->data,
1558 priv->rx_skb->len, "RX: len=%d", priv->rx_skb->len);
1559
1560 rx_status.signal = buf->rssi;
1561 rx_status.flag |= RX_FLAG_DECRYPTED;
1562 rx_status.flag |= RX_FLAG_IV_STRIPPED;
1563
1564 at76_dbg(DBG_MAC80211, "calling ieee80211_rx_irqsafe(): %d/%d",
1565 priv->rx_skb->len, priv->rx_skb->data_len);
Johannes Bergf1d58c22009-06-17 13:13:00 +02001566 memcpy(IEEE80211_SKB_RXCB(priv->rx_skb), &rx_status, sizeof(rx_status));
1567 ieee80211_rx_irqsafe(priv->hw, priv->rx_skb);
Kalle Valo1264b952009-02-07 09:27:10 +02001568
1569 /* Use a new skb for the next receive */
1570 priv->rx_skb = NULL;
1571
1572 at76_submit_rx_urb(priv);
1573}
1574
1575/* Load firmware into kernel memory and parse it */
1576static struct fwentry *at76_load_firmware(struct usb_device *udev,
1577 enum board_type board_type)
1578{
1579 int ret;
1580 char *str;
1581 struct at76_fw_header *fwh;
1582 struct fwentry *fwe = &firmwares[board_type];
1583
1584 mutex_lock(&fw_mutex);
1585
1586 if (fwe->loaded) {
1587 at76_dbg(DBG_FW, "re-using previously loaded fw");
1588 goto exit;
1589 }
1590
1591 at76_dbg(DBG_FW, "downloading firmware %s", fwe->fwname);
1592 ret = request_firmware(&fwe->fw, fwe->fwname, &udev->dev);
1593 if (ret < 0) {
1594 dev_printk(KERN_ERR, &udev->dev, "firmware %s not found!\n",
1595 fwe->fwname);
1596 dev_printk(KERN_ERR, &udev->dev,
1597 "you may need to download the firmware from "
1598 "http://developer.berlios.de/projects/at76c503a/\n");
1599 goto exit;
1600 }
1601
1602 at76_dbg(DBG_FW, "got it.");
1603 fwh = (struct at76_fw_header *)(fwe->fw->data);
1604
1605 if (fwe->fw->size <= sizeof(*fwh)) {
1606 dev_printk(KERN_ERR, &udev->dev,
1607 "firmware is too short (0x%zx)\n", fwe->fw->size);
1608 goto exit;
1609 }
1610
1611 /* CRC currently not checked */
1612 fwe->board_type = le32_to_cpu(fwh->board_type);
1613 if (fwe->board_type != board_type) {
1614 dev_printk(KERN_ERR, &udev->dev,
1615 "board type mismatch, requested %u, got %u\n",
1616 board_type, fwe->board_type);
1617 goto exit;
1618 }
1619
1620 fwe->fw_version.major = fwh->major;
1621 fwe->fw_version.minor = fwh->minor;
1622 fwe->fw_version.patch = fwh->patch;
1623 fwe->fw_version.build = fwh->build;
1624
1625 str = (char *)fwh + le32_to_cpu(fwh->str_offset);
1626 fwe->intfw = (u8 *)fwh + le32_to_cpu(fwh->int_fw_offset);
1627 fwe->intfw_size = le32_to_cpu(fwh->int_fw_len);
1628 fwe->extfw = (u8 *)fwh + le32_to_cpu(fwh->ext_fw_offset);
1629 fwe->extfw_size = le32_to_cpu(fwh->ext_fw_len);
1630
1631 fwe->loaded = 1;
1632
1633 dev_printk(KERN_DEBUG, &udev->dev,
1634 "using firmware %s (version %d.%d.%d-%d)\n",
1635 fwe->fwname, fwh->major, fwh->minor, fwh->patch, fwh->build);
1636
1637 at76_dbg(DBG_DEVSTART, "board %u, int %d:%d, ext %d:%d", board_type,
1638 le32_to_cpu(fwh->int_fw_offset), le32_to_cpu(fwh->int_fw_len),
1639 le32_to_cpu(fwh->ext_fw_offset), le32_to_cpu(fwh->ext_fw_len));
1640 at76_dbg(DBG_DEVSTART, "firmware id %s", str);
1641
1642exit:
1643 mutex_unlock(&fw_mutex);
1644
1645 if (fwe->loaded)
1646 return fwe;
1647 else
1648 return NULL;
1649}
1650
Sebastian Smolorz41b4b282010-06-22 16:53:37 +02001651static int at76_join(struct at76_priv *priv)
1652{
1653 struct at76_req_join join;
1654 int ret;
1655
1656 memset(&join, 0, sizeof(struct at76_req_join));
1657 memcpy(join.essid, priv->essid, priv->essid_size);
1658 join.essid_size = priv->essid_size;
1659 memcpy(join.bssid, priv->bssid, ETH_ALEN);
1660 join.bss_type = INFRASTRUCTURE_MODE;
1661 join.channel = priv->channel;
1662 join.timeout = cpu_to_le16(2000);
1663
1664 at76_dbg(DBG_MAC80211, "%s: sending CMD_JOIN", __func__);
1665 ret = at76_set_card_command(priv->udev, CMD_JOIN, &join,
1666 sizeof(struct at76_req_join));
1667
1668 if (ret < 0) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001669 wiphy_err(priv->hw->wiphy, "at76_set_card_command failed: %d\n",
1670 ret);
Sebastian Smolorz41b4b282010-06-22 16:53:37 +02001671 return 0;
1672 }
1673
1674 ret = at76_wait_completion(priv, CMD_JOIN);
1675 at76_dbg(DBG_MAC80211, "%s: CMD_JOIN returned: 0x%02x", __func__, ret);
1676 if (ret != CMD_STATUS_COMPLETE) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001677 wiphy_err(priv->hw->wiphy, "at76_wait_completion failed: %d\n",
1678 ret);
Sebastian Smolorz41b4b282010-06-22 16:53:37 +02001679 return 0;
1680 }
1681
1682 at76_set_pm_mode(priv);
1683
1684 return 0;
1685}
1686
Sebastian Smolorza1850452010-06-22 16:55:17 +02001687static void at76_work_join_bssid(struct work_struct *work)
1688{
1689 struct at76_priv *priv = container_of(work, struct at76_priv,
1690 work_join_bssid);
1691
1692 if (priv->device_unplugged)
1693 return;
1694
1695 mutex_lock(&priv->mtx);
1696
1697 if (is_valid_ether_addr(priv->bssid))
1698 at76_join(priv);
1699
1700 mutex_unlock(&priv->mtx);
1701}
1702
Kalle Valo1264b952009-02-07 09:27:10 +02001703static void at76_mac80211_tx_callback(struct urb *urb)
1704{
1705 struct at76_priv *priv = urb->context;
1706 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(priv->tx_skb);
1707
1708 at76_dbg(DBG_MAC80211, "%s()", __func__);
1709
1710 switch (urb->status) {
1711 case 0:
1712 /* success */
1713 info->flags |= IEEE80211_TX_STAT_ACK;
1714 break;
1715 case -ENOENT:
1716 case -ECONNRESET:
1717 /* fail, urb has been unlinked */
1718 /* FIXME: add error message */
1719 break;
1720 default:
1721 at76_dbg(DBG_URB, "%s - nonzero tx status received: %d",
1722 __func__, urb->status);
1723 break;
1724 }
1725
1726 memset(&info->status, 0, sizeof(info->status));
1727
1728 ieee80211_tx_status_irqsafe(priv->hw, priv->tx_skb);
1729
1730 priv->tx_skb = NULL;
1731
1732 ieee80211_wake_queues(priv->hw);
1733}
1734
1735static int at76_mac80211_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
1736{
1737 struct at76_priv *priv = hw->priv;
1738 struct at76_tx_buffer *tx_buffer = priv->bulk_out_buffer;
1739 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Sebastian Smolorza1850452010-06-22 16:55:17 +02001740 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
Kalle Valo1264b952009-02-07 09:27:10 +02001741 int padding, submit_len, ret;
1742
1743 at76_dbg(DBG_MAC80211, "%s()", __func__);
1744
1745 if (priv->tx_urb->status == -EINPROGRESS) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001746 wiphy_err(priv->hw->wiphy,
1747 "%s called while tx urb is pending\n", __func__);
Kalle Valo1264b952009-02-07 09:27:10 +02001748 return NETDEV_TX_BUSY;
1749 }
1750
Sebastian Smolorza1850452010-06-22 16:55:17 +02001751 /* The following code lines are important when the device is going to
1752 * authenticate with a new bssid. The driver must send CMD_JOIN before
1753 * an authentication frame is transmitted. For this to succeed, the
1754 * correct bssid of the AP must be known. As mac80211 does not inform
1755 * drivers about the bssid prior to the authentication process the
1756 * following workaround is necessary. If the TX frame is an
1757 * authentication frame extract the bssid and send the CMD_JOIN. */
1758 if (mgmt->frame_control & cpu_to_le16(IEEE80211_STYPE_AUTH)) {
1759 if (compare_ether_addr(priv->bssid, mgmt->bssid)) {
1760 memcpy(priv->bssid, mgmt->bssid, ETH_ALEN);
1761 ieee80211_queue_work(hw, &priv->work_join_bssid);
1762 return NETDEV_TX_BUSY;
1763 }
1764 }
1765
Kalle Valo1264b952009-02-07 09:27:10 +02001766 ieee80211_stop_queues(hw);
1767
1768 at76_ledtrig_tx_activity(); /* tell ledtrigger we send a packet */
1769
1770 WARN_ON(priv->tx_skb != NULL);
1771
1772 priv->tx_skb = skb;
1773 padding = at76_calc_padding(skb->len);
1774 submit_len = AT76_TX_HDRLEN + skb->len + padding;
1775
1776 /* setup 'Atmel' header */
1777 memset(tx_buffer, 0, sizeof(*tx_buffer));
1778 tx_buffer->padding = padding;
1779 tx_buffer->wlength = cpu_to_le16(skb->len);
1780 tx_buffer->tx_rate = ieee80211_get_tx_rate(hw, info)->hw_value;
1781 memset(tx_buffer->reserved, 0, sizeof(tx_buffer->reserved));
1782 memcpy(tx_buffer->packet, skb->data, skb->len);
1783
1784 at76_dbg(DBG_TX_DATA, "%s tx: wlen 0x%x pad 0x%x rate %d hdr",
1785 wiphy_name(priv->hw->wiphy), le16_to_cpu(tx_buffer->wlength),
1786 tx_buffer->padding, tx_buffer->tx_rate);
1787
1788 /* send stuff */
1789 at76_dbg_dump(DBG_TX_DATA_CONTENT, tx_buffer, submit_len,
1790 "%s(): tx_buffer %d bytes:", __func__, submit_len);
1791 usb_fill_bulk_urb(priv->tx_urb, priv->udev, priv->tx_pipe, tx_buffer,
1792 submit_len, at76_mac80211_tx_callback, priv);
1793 ret = usb_submit_urb(priv->tx_urb, GFP_ATOMIC);
1794 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001795 wiphy_err(priv->hw->wiphy, "error in tx submit urb: %d\n", ret);
Kalle Valo1264b952009-02-07 09:27:10 +02001796 if (ret == -EINVAL)
Joe Perchesc96c31e2010-07-26 14:39:58 -07001797 wiphy_err(priv->hw->wiphy,
1798 "-einval: tx urb %p hcpriv %p complete %p\n",
1799 priv->tx_urb,
1800 priv->tx_urb->hcpriv, priv->tx_urb->complete);
Kalle Valo1264b952009-02-07 09:27:10 +02001801 }
1802
1803 return 0;
1804}
1805
1806static int at76_mac80211_start(struct ieee80211_hw *hw)
1807{
1808 struct at76_priv *priv = hw->priv;
1809 int ret;
1810
1811 at76_dbg(DBG_MAC80211, "%s()", __func__);
1812
1813 mutex_lock(&priv->mtx);
1814
1815 ret = at76_submit_rx_urb(priv);
1816 if (ret < 0) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001817 wiphy_err(priv->hw->wiphy, "open: submit_rx_urb failed: %d\n",
1818 ret);
Kalle Valo1264b952009-02-07 09:27:10 +02001819 goto error;
1820 }
1821
1822 at76_startup_device(priv);
1823
1824 at76_start_monitor(priv);
1825
1826error:
1827 mutex_unlock(&priv->mtx);
1828
1829 return 0;
1830}
1831
1832static void at76_mac80211_stop(struct ieee80211_hw *hw)
1833{
1834 struct at76_priv *priv = hw->priv;
1835
1836 at76_dbg(DBG_MAC80211, "%s()", __func__);
1837
Luis R. Rodriguez9ed21d32009-07-27 11:56:24 -07001838 cancel_delayed_work(&priv->dwork_hw_scan);
Sebastian Smolorza1850452010-06-22 16:55:17 +02001839 cancel_work_sync(&priv->work_join_bssid);
Luis R. Rodriguezebc8ab12009-07-27 11:56:23 -07001840 cancel_work_sync(&priv->work_set_promisc);
1841
Kalle Valo1264b952009-02-07 09:27:10 +02001842 mutex_lock(&priv->mtx);
1843
1844 if (!priv->device_unplugged) {
1845 /* We are called by "ifconfig ethX down", not because the
1846 * device is not available anymore. */
1847 at76_set_radio(priv, 0);
1848
1849 /* We unlink rx_urb because at76_open() re-submits it.
1850 * If unplugged, at76_delete_device() takes care of it. */
1851 usb_kill_urb(priv->rx_urb);
1852 }
1853
1854 mutex_unlock(&priv->mtx);
1855}
1856
1857static int at76_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001858 struct ieee80211_vif *vif)
Kalle Valo1264b952009-02-07 09:27:10 +02001859{
1860 struct at76_priv *priv = hw->priv;
1861 int ret = 0;
1862
1863 at76_dbg(DBG_MAC80211, "%s()", __func__);
1864
1865 mutex_lock(&priv->mtx);
1866
Johannes Berg1ed32e42009-12-23 13:15:45 +01001867 switch (vif->type) {
Kalle Valo1264b952009-02-07 09:27:10 +02001868 case NL80211_IFTYPE_STATION:
1869 priv->iw_mode = IW_MODE_INFRA;
1870 break;
1871 default:
1872 ret = -EOPNOTSUPP;
1873 goto exit;
1874 }
1875
1876exit:
1877 mutex_unlock(&priv->mtx);
1878
1879 return ret;
1880}
1881
1882static void at76_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001883 struct ieee80211_vif *vif)
Kalle Valo1264b952009-02-07 09:27:10 +02001884{
1885 at76_dbg(DBG_MAC80211, "%s()", __func__);
1886}
1887
Kalle Valo1264b952009-02-07 09:27:10 +02001888static void at76_dwork_hw_scan(struct work_struct *work)
1889{
1890 struct at76_priv *priv = container_of(work, struct at76_priv,
1891 dwork_hw_scan.work);
1892 int ret;
1893
Jason Andryukdeda8622009-02-21 09:53:14 +02001894 if (priv->device_unplugged)
1895 return;
1896
Kalle Valo1264b952009-02-07 09:27:10 +02001897 mutex_lock(&priv->mtx);
1898
1899 ret = at76_get_cmd_status(priv->udev, CMD_SCAN);
1900 at76_dbg(DBG_MAC80211, "%s: CMD_SCAN status 0x%02x", __func__, ret);
1901
1902 /* FIXME: add maximum time for scan to complete */
1903
1904 if (ret != CMD_STATUS_COMPLETE) {
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001905 ieee80211_queue_delayed_work(priv->hw, &priv->dwork_hw_scan,
1906 SCAN_POLL_INTERVAL);
John W. Linville21a4cc02009-05-28 11:39:02 +02001907 mutex_unlock(&priv->mtx);
1908 return;
Kalle Valo1264b952009-02-07 09:27:10 +02001909 }
1910
Kalle Valo1264b952009-02-07 09:27:10 +02001911 if (is_valid_ether_addr(priv->bssid))
1912 at76_join(priv);
1913
Kalle Valo1264b952009-02-07 09:27:10 +02001914 mutex_unlock(&priv->mtx);
John W. Linville21a4cc02009-05-28 11:39:02 +02001915
1916 ieee80211_scan_completed(priv->hw, false);
1917
1918 ieee80211_wake_queues(priv->hw);
Kalle Valo1264b952009-02-07 09:27:10 +02001919}
1920
Jason Andryuk19e8bc72009-02-18 22:40:57 +02001921static int at76_hw_scan(struct ieee80211_hw *hw,
Johannes Berga060bbf2010-04-27 11:59:34 +02001922 struct ieee80211_vif *vif,
Jason Andryuk19e8bc72009-02-18 22:40:57 +02001923 struct cfg80211_scan_request *req)
Kalle Valo1264b952009-02-07 09:27:10 +02001924{
1925 struct at76_priv *priv = hw->priv;
1926 struct at76_req_scan scan;
Jason Andryuk19e8bc72009-02-18 22:40:57 +02001927 u8 *ssid = NULL;
1928 int ret, len = 0;
Kalle Valo1264b952009-02-07 09:27:10 +02001929
1930 at76_dbg(DBG_MAC80211, "%s():", __func__);
Kalle Valo1264b952009-02-07 09:27:10 +02001931
Jason Andryukdeda8622009-02-21 09:53:14 +02001932 if (priv->device_unplugged)
1933 return 0;
1934
Kalle Valo1264b952009-02-07 09:27:10 +02001935 mutex_lock(&priv->mtx);
1936
1937 ieee80211_stop_queues(hw);
1938
1939 memset(&scan, 0, sizeof(struct at76_req_scan));
1940 memset(scan.bssid, 0xFF, ETH_ALEN);
Jason Andryuk19e8bc72009-02-18 22:40:57 +02001941
1942 if (req->n_ssids) {
1943 scan.scan_type = SCAN_TYPE_ACTIVE;
1944 ssid = req->ssids[0].ssid;
1945 len = req->ssids[0].ssid_len;
1946 } else {
1947 scan.scan_type = SCAN_TYPE_PASSIVE;
1948 }
1949
1950 if (len) {
Kalle Valo1264b952009-02-07 09:27:10 +02001951 memcpy(scan.essid, ssid, len);
1952 scan.essid_size = len;
1953 }
Jason Andryuk19e8bc72009-02-18 22:40:57 +02001954
Kalle Valo1264b952009-02-07 09:27:10 +02001955 scan.min_channel_time = cpu_to_le16(priv->scan_min_time);
1956 scan.max_channel_time = cpu_to_le16(priv->scan_max_time);
1957 scan.probe_delay = cpu_to_le16(priv->scan_min_time * 1000);
1958 scan.international_scan = 0;
1959
1960 at76_dbg(DBG_MAC80211, "%s: sending CMD_SCAN", __func__);
1961 ret = at76_set_card_command(priv->udev, CMD_SCAN, &scan, sizeof(scan));
1962
1963 if (ret < 0) {
1964 err("CMD_SCAN failed: %d", ret);
1965 goto exit;
1966 }
1967
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001968 ieee80211_queue_delayed_work(priv->hw, &priv->dwork_hw_scan,
1969 SCAN_POLL_INTERVAL);
Kalle Valo1264b952009-02-07 09:27:10 +02001970
1971exit:
1972 mutex_unlock(&priv->mtx);
1973
1974 return 0;
1975}
1976
1977static int at76_config(struct ieee80211_hw *hw, u32 changed)
1978{
1979 struct at76_priv *priv = hw->priv;
1980
Johannes Bergf424afa2009-08-17 16:18:07 +02001981 at76_dbg(DBG_MAC80211, "%s(): channel %d",
1982 __func__, hw->conf.channel->hw_value);
Kalle Valo1264b952009-02-07 09:27:10 +02001983 at76_dbg_dump(DBG_MAC80211, priv->bssid, ETH_ALEN, "bssid:");
1984
1985 mutex_lock(&priv->mtx);
1986
1987 priv->channel = hw->conf.channel->hw_value;
1988
1989 if (is_valid_ether_addr(priv->bssid))
1990 at76_join(priv);
1991 else
1992 at76_start_monitor(priv);
1993
1994 mutex_unlock(&priv->mtx);
1995
1996 return 0;
1997}
1998
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001999static void at76_bss_info_changed(struct ieee80211_hw *hw,
2000 struct ieee80211_vif *vif,
2001 struct ieee80211_bss_conf *conf,
2002 u32 changed)
Kalle Valo1264b952009-02-07 09:27:10 +02002003{
2004 struct at76_priv *priv = hw->priv;
2005
2006 at76_dbg(DBG_MAC80211, "%s():", __func__);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02002007
2008 if (!(changed & BSS_CHANGED_BSSID))
2009 return;
2010
Kalle Valo1264b952009-02-07 09:27:10 +02002011 at76_dbg_dump(DBG_MAC80211, conf->bssid, ETH_ALEN, "bssid:");
2012
2013 mutex_lock(&priv->mtx);
2014
2015 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
2016
2017 if (is_valid_ether_addr(priv->bssid))
2018 /* mac80211 is joining a bss */
2019 at76_join(priv);
2020
2021 mutex_unlock(&priv->mtx);
Kalle Valo1264b952009-02-07 09:27:10 +02002022}
2023
2024/* must be atomic */
2025static void at76_configure_filter(struct ieee80211_hw *hw,
2026 unsigned int changed_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02002027 unsigned int *total_flags, u64 multicast)
Kalle Valo1264b952009-02-07 09:27:10 +02002028{
2029 struct at76_priv *priv = hw->priv;
2030 int flags;
2031
2032 at76_dbg(DBG_MAC80211, "%s(): changed_flags=0x%08x "
Johannes Berg3ac64be2009-08-17 16:16:53 +02002033 "total_flags=0x%08x",
2034 __func__, changed_flags, *total_flags);
Kalle Valo1264b952009-02-07 09:27:10 +02002035
2036 flags = changed_flags & AT76_SUPPORTED_FILTERS;
2037 *total_flags = AT76_SUPPORTED_FILTERS;
2038
Jason Andryukdeda8622009-02-21 09:53:14 +02002039 /* Bail out after updating flags to prevent a WARN_ON in mac80211. */
2040 if (priv->device_unplugged)
2041 return;
2042
Kalle Valo1264b952009-02-07 09:27:10 +02002043 /* FIXME: access to priv->promisc should be protected with
2044 * priv->mtx, but it's impossible because this function needs to be
2045 * atomic */
2046
2047 if (flags && !priv->promisc) {
2048 /* mac80211 wants us to enable promiscuous mode */
2049 priv->promisc = 1;
2050 } else if (!flags && priv->promisc) {
2051 /* we need to disable promiscuous mode */
2052 priv->promisc = 0;
2053 } else
2054 return;
2055
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002056 ieee80211_queue_work(hw, &priv->work_set_promisc);
Kalle Valo1264b952009-02-07 09:27:10 +02002057}
2058
2059static int at76_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2060 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2061 struct ieee80211_key_conf *key)
2062{
2063 struct at76_priv *priv = hw->priv;
2064
2065 int i;
2066
2067 at76_dbg(DBG_MAC80211, "%s(): cmd %d key->alg %d key->keyidx %d "
2068 "key->keylen %d",
2069 __func__, cmd, key->alg, key->keyidx, key->keylen);
2070
2071 if (key->alg != ALG_WEP)
2072 return -EOPNOTSUPP;
2073
2074 key->hw_key_idx = key->keyidx;
2075
2076 mutex_lock(&priv->mtx);
2077
2078 switch (cmd) {
2079 case SET_KEY:
2080 memcpy(priv->wep_keys[key->keyidx], key->key, key->keylen);
2081 priv->wep_keys_len[key->keyidx] = key->keylen;
2082
2083 /* FIXME: find out how to do this properly */
2084 priv->wep_key_id = key->keyidx;
2085
2086 break;
2087 case DISABLE_KEY:
2088 default:
2089 priv->wep_keys_len[key->keyidx] = 0;
2090 break;
2091 }
2092
2093 priv->wep_enabled = 0;
2094
2095 for (i = 0; i < WEP_KEYS; i++) {
2096 if (priv->wep_keys_len[i] != 0)
2097 priv->wep_enabled = 1;
2098 }
2099
2100 at76_startup_device(priv);
2101
2102 mutex_unlock(&priv->mtx);
2103
2104 return 0;
2105}
2106
2107static const struct ieee80211_ops at76_ops = {
2108 .tx = at76_mac80211_tx,
2109 .add_interface = at76_add_interface,
2110 .remove_interface = at76_remove_interface,
2111 .config = at76_config,
Johannes Berg2d0ddec2009-04-23 16:13:26 +02002112 .bss_info_changed = at76_bss_info_changed,
Kalle Valo1264b952009-02-07 09:27:10 +02002113 .configure_filter = at76_configure_filter,
2114 .start = at76_mac80211_start,
2115 .stop = at76_mac80211_stop,
2116 .hw_scan = at76_hw_scan,
2117 .set_key = at76_set_key,
2118};
2119
2120/* Allocate network device and initialize private data */
2121static struct at76_priv *at76_alloc_new_device(struct usb_device *udev)
2122{
2123 struct ieee80211_hw *hw;
2124 struct at76_priv *priv;
2125
2126 hw = ieee80211_alloc_hw(sizeof(struct at76_priv), &at76_ops);
2127 if (!hw) {
2128 printk(KERN_ERR DRIVER_NAME ": could not register"
2129 " ieee80211_hw\n");
2130 return NULL;
2131 }
2132
2133 priv = hw->priv;
2134 priv->hw = hw;
2135
2136 priv->udev = udev;
2137
2138 mutex_init(&priv->mtx);
2139 INIT_WORK(&priv->work_set_promisc, at76_work_set_promisc);
2140 INIT_WORK(&priv->work_submit_rx, at76_work_submit_rx);
Sebastian Smolorza1850452010-06-22 16:55:17 +02002141 INIT_WORK(&priv->work_join_bssid, at76_work_join_bssid);
Kalle Valo1264b952009-02-07 09:27:10 +02002142 INIT_DELAYED_WORK(&priv->dwork_hw_scan, at76_dwork_hw_scan);
2143
Jason Andryukdeda8622009-02-21 09:53:14 +02002144 tasklet_init(&priv->rx_tasklet, at76_rx_tasklet, 0);
Kalle Valo1264b952009-02-07 09:27:10 +02002145
2146 priv->pm_mode = AT76_PM_OFF;
2147 priv->pm_period = 0;
2148
2149 /* unit us */
2150 priv->hw->channel_change_time = 100000;
2151
2152 return priv;
2153}
2154
2155static int at76_alloc_urbs(struct at76_priv *priv,
2156 struct usb_interface *interface)
2157{
2158 struct usb_endpoint_descriptor *endpoint, *ep_in, *ep_out;
2159 int i;
2160 int buffer_size;
2161 struct usb_host_interface *iface_desc;
2162
2163 at76_dbg(DBG_PROC_ENTRY, "%s: ENTER", __func__);
2164
2165 at76_dbg(DBG_URB, "%s: NumEndpoints %d ", __func__,
2166 interface->altsetting[0].desc.bNumEndpoints);
2167
2168 ep_in = NULL;
2169 ep_out = NULL;
2170 iface_desc = interface->cur_altsetting;
2171 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
2172 endpoint = &iface_desc->endpoint[i].desc;
2173
2174 at76_dbg(DBG_URB, "%s: %d. endpoint: addr 0x%x attr 0x%x",
2175 __func__, i, endpoint->bEndpointAddress,
2176 endpoint->bmAttributes);
2177
2178 if (!ep_in && usb_endpoint_is_bulk_in(endpoint))
2179 ep_in = endpoint;
2180
2181 if (!ep_out && usb_endpoint_is_bulk_out(endpoint))
2182 ep_out = endpoint;
2183 }
2184
2185 if (!ep_in || !ep_out) {
2186 dev_printk(KERN_ERR, &interface->dev,
2187 "bulk endpoints missing\n");
2188 return -ENXIO;
2189 }
2190
2191 priv->rx_pipe = usb_rcvbulkpipe(priv->udev, ep_in->bEndpointAddress);
2192 priv->tx_pipe = usb_sndbulkpipe(priv->udev, ep_out->bEndpointAddress);
2193
2194 priv->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2195 priv->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2196 if (!priv->rx_urb || !priv->tx_urb) {
2197 dev_printk(KERN_ERR, &interface->dev, "cannot allocate URB\n");
2198 return -ENOMEM;
2199 }
2200
2201 buffer_size = sizeof(struct at76_tx_buffer) + MAX_PADDING_SIZE;
2202 priv->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
2203 if (!priv->bulk_out_buffer) {
2204 dev_printk(KERN_ERR, &interface->dev,
2205 "cannot allocate output buffer\n");
2206 return -ENOMEM;
2207 }
2208
2209 at76_dbg(DBG_PROC_ENTRY, "%s: EXIT", __func__);
2210
2211 return 0;
2212}
2213
2214static struct ieee80211_rate at76_rates[] = {
2215 { .bitrate = 10, .hw_value = TX_RATE_1MBIT, },
2216 { .bitrate = 20, .hw_value = TX_RATE_2MBIT, },
2217 { .bitrate = 55, .hw_value = TX_RATE_5_5MBIT, },
2218 { .bitrate = 110, .hw_value = TX_RATE_11MBIT, },
2219};
2220
2221static struct ieee80211_channel at76_channels[] = {
2222 { .center_freq = 2412, .hw_value = 1 },
2223 { .center_freq = 2417, .hw_value = 2 },
2224 { .center_freq = 2422, .hw_value = 3 },
2225 { .center_freq = 2427, .hw_value = 4 },
2226 { .center_freq = 2432, .hw_value = 5 },
2227 { .center_freq = 2437, .hw_value = 6 },
2228 { .center_freq = 2442, .hw_value = 7 },
2229 { .center_freq = 2447, .hw_value = 8 },
2230 { .center_freq = 2452, .hw_value = 9 },
2231 { .center_freq = 2457, .hw_value = 10 },
2232 { .center_freq = 2462, .hw_value = 11 },
2233 { .center_freq = 2467, .hw_value = 12 },
2234 { .center_freq = 2472, .hw_value = 13 },
2235 { .center_freq = 2484, .hw_value = 14 }
2236};
2237
2238static struct ieee80211_supported_band at76_supported_band = {
2239 .channels = at76_channels,
2240 .n_channels = ARRAY_SIZE(at76_channels),
2241 .bitrates = at76_rates,
2242 .n_bitrates = ARRAY_SIZE(at76_rates),
2243};
2244
2245/* Register network device and initialize the hardware */
2246static int at76_init_new_device(struct at76_priv *priv,
2247 struct usb_interface *interface)
2248{
Kalle Valofe348cb2009-09-24 11:02:51 -07002249 struct wiphy *wiphy;
2250 size_t len;
Kalle Valo1264b952009-02-07 09:27:10 +02002251 int ret;
2252
2253 /* set up the endpoint information */
2254 /* check out the endpoints */
2255
2256 at76_dbg(DBG_DEVSTART, "USB interface: %d endpoints",
2257 interface->cur_altsetting->desc.bNumEndpoints);
2258
2259 ret = at76_alloc_urbs(priv, interface);
2260 if (ret < 0)
2261 goto exit;
2262
2263 /* MAC address */
2264 ret = at76_get_hw_config(priv);
2265 if (ret < 0) {
2266 dev_printk(KERN_ERR, &interface->dev,
2267 "cannot get MAC address\n");
2268 goto exit;
2269 }
2270
2271 priv->domain = at76_get_reg_domain(priv->regulatory_domain);
2272
2273 priv->channel = DEF_CHANNEL;
2274 priv->iw_mode = IW_MODE_INFRA;
2275 priv->rts_threshold = DEF_RTS_THRESHOLD;
2276 priv->frag_threshold = DEF_FRAG_THRESHOLD;
2277 priv->short_retry_limit = DEF_SHORT_RETRY_LIMIT;
2278 priv->txrate = TX_RATE_AUTO;
2279 priv->preamble_type = PREAMBLE_TYPE_LONG;
2280 priv->beacon_period = 100;
2281 priv->auth_mode = WLAN_AUTH_OPEN;
2282 priv->scan_min_time = DEF_SCAN_MIN_TIME;
2283 priv->scan_max_time = DEF_SCAN_MAX_TIME;
2284 priv->scan_mode = SCAN_TYPE_ACTIVE;
Jason Andryukdeda8622009-02-21 09:53:14 +02002285 priv->device_unplugged = 0;
Kalle Valo1264b952009-02-07 09:27:10 +02002286
2287 /* mac80211 initialisation */
Kalle Valofe348cb2009-09-24 11:02:51 -07002288 wiphy = priv->hw->wiphy;
Jason Andryuk19e8bc72009-02-18 22:40:57 +02002289 priv->hw->wiphy->max_scan_ssids = 1;
Johannes Berg18a83652009-03-31 12:12:05 +02002290 priv->hw->wiphy->max_scan_ie_len = 0;
Kalle Valo1264b952009-02-07 09:27:10 +02002291 priv->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
2292 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &at76_supported_band;
2293 priv->hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
2294 IEEE80211_HW_SIGNAL_UNSPEC;
Jason Andryuk19e8bc72009-02-18 22:40:57 +02002295 priv->hw->max_signal = 100;
Kalle Valo1264b952009-02-07 09:27:10 +02002296
2297 SET_IEEE80211_DEV(priv->hw, &interface->dev);
2298 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
2299
Kalle Valofe348cb2009-09-24 11:02:51 -07002300 len = sizeof(wiphy->fw_version);
2301 snprintf(wiphy->fw_version, len, "%d.%d.%d-%d",
2302 priv->fw_version.major, priv->fw_version.minor,
2303 priv->fw_version.patch, priv->fw_version.build);
2304
2305 wiphy->hw_version = priv->board_type;
2306
Kalle Valo1264b952009-02-07 09:27:10 +02002307 ret = ieee80211_register_hw(priv->hw);
2308 if (ret) {
2309 printk(KERN_ERR "cannot register mac80211 hw (status %d)!\n",
2310 ret);
2311 goto exit;
2312 }
2313
2314 priv->mac80211_registered = 1;
2315
Joe Perchesc96c31e2010-07-26 14:39:58 -07002316 wiphy_info(priv->hw->wiphy, "usb %s, mac %pm, firmware %d.%d.%d-%d\n",
2317 dev_name(&interface->dev), priv->mac_addr,
2318 priv->fw_version.major, priv->fw_version.minor,
2319 priv->fw_version.patch, priv->fw_version.build);
2320 wiphy_info(priv->hw->wiphy, "regulatory domain 0x%02x: %s\n",
2321 priv->regulatory_domain, priv->domain->name);
Kalle Valo1264b952009-02-07 09:27:10 +02002322
2323exit:
2324 return ret;
2325}
2326
2327static void at76_delete_device(struct at76_priv *priv)
2328{
2329 at76_dbg(DBG_PROC_ENTRY, "%s: ENTER", __func__);
2330
2331 /* The device is gone, don't bother turning it off */
2332 priv->device_unplugged = 1;
2333
Jason Andryukdeda8622009-02-21 09:53:14 +02002334 tasklet_kill(&priv->rx_tasklet);
2335
Luis R. Rodriguez64344d72009-07-29 14:20:41 -07002336 if (priv->mac80211_registered)
Kalle Valo1264b952009-02-07 09:27:10 +02002337 ieee80211_unregister_hw(priv->hw);
Kalle Valo1264b952009-02-07 09:27:10 +02002338
2339 if (priv->tx_urb) {
2340 usb_kill_urb(priv->tx_urb);
2341 usb_free_urb(priv->tx_urb);
2342 }
2343 if (priv->rx_urb) {
2344 usb_kill_urb(priv->rx_urb);
2345 usb_free_urb(priv->rx_urb);
2346 }
2347
2348 at76_dbg(DBG_PROC_ENTRY, "%s: unlinked urbs", __func__);
2349
Jason Andryukdeda8622009-02-21 09:53:14 +02002350 kfree(priv->bulk_out_buffer);
2351
Jason Andryuk1cc198f2009-02-21 09:53:29 +02002352 del_timer_sync(&ledtrig_tx_timer);
2353
Wei Yongjun54cb2282009-04-08 22:16:38 +00002354 kfree_skb(priv->rx_skb);
Kalle Valo1264b952009-02-07 09:27:10 +02002355
2356 usb_put_dev(priv->udev);
2357
2358 at76_dbg(DBG_PROC_ENTRY, "%s: before freeing priv/ieee80211_hw",
2359 __func__);
2360 ieee80211_free_hw(priv->hw);
2361
2362 at76_dbg(DBG_PROC_ENTRY, "%s: EXIT", __func__);
2363}
2364
2365static int at76_probe(struct usb_interface *interface,
2366 const struct usb_device_id *id)
2367{
2368 int ret;
2369 struct at76_priv *priv;
2370 struct fwentry *fwe;
2371 struct usb_device *udev;
2372 int op_mode;
2373 int need_ext_fw = 0;
2374 struct mib_fw_version fwv;
2375 int board_type = (int)id->driver_info;
2376
2377 udev = usb_get_dev(interface_to_usbdev(interface));
2378
2379 /* Load firmware into kernel memory */
2380 fwe = at76_load_firmware(udev, board_type);
2381 if (!fwe) {
2382 ret = -ENOENT;
2383 goto error;
2384 }
2385
2386 op_mode = at76_get_op_mode(udev);
2387
2388 at76_dbg(DBG_DEVSTART, "opmode %d", op_mode);
2389
2390 /* we get OPMODE_NONE with 2.4.23, SMC2662W-AR ???
2391 we get 204 with 2.4.23, Fiberline FL-WL240u (505A+RFMD2958) ??? */
2392
2393 if (op_mode == OPMODE_HW_CONFIG_MODE) {
2394 dev_printk(KERN_ERR, &interface->dev,
2395 "cannot handle a device in HW_CONFIG_MODE\n");
2396 ret = -EBUSY;
2397 goto error;
2398 }
2399
2400 if (op_mode != OPMODE_NORMAL_NIC_WITH_FLASH
2401 && op_mode != OPMODE_NORMAL_NIC_WITHOUT_FLASH) {
2402 /* download internal firmware part */
2403 dev_printk(KERN_DEBUG, &interface->dev,
2404 "downloading internal firmware\n");
2405 ret = at76_load_internal_fw(udev, fwe);
2406 if (ret < 0) {
2407 dev_printk(KERN_ERR, &interface->dev,
2408 "error %d downloading internal firmware\n",
2409 ret);
2410 goto error;
2411 }
2412 usb_put_dev(udev);
2413 return ret;
2414 }
2415
2416 /* Internal firmware already inside the device. Get firmware
2417 * version to test if external firmware is loaded.
2418 * This works only for newer firmware, e.g. the Intersil 0.90.x
2419 * says "control timeout on ep0in" and subsequent
2420 * at76_get_op_mode() fail too :-( */
2421
2422 /* if version >= 0.100.x.y or device with built-in flash we can
2423 * query the device for the fw version */
2424 if ((fwe->fw_version.major > 0 || fwe->fw_version.minor >= 100)
2425 || (op_mode == OPMODE_NORMAL_NIC_WITH_FLASH)) {
2426 ret = at76_get_mib(udev, MIB_FW_VERSION, &fwv, sizeof(fwv));
2427 if (ret < 0 || (fwv.major | fwv.minor) == 0)
2428 need_ext_fw = 1;
2429 } else
2430 /* No way to check firmware version, reload to be sure */
2431 need_ext_fw = 1;
2432
2433 if (need_ext_fw) {
2434 dev_printk(KERN_DEBUG, &interface->dev,
2435 "downloading external firmware\n");
2436
2437 ret = at76_load_external_fw(udev, fwe);
2438 if (ret)
2439 goto error;
2440
2441 /* Re-check firmware version */
2442 ret = at76_get_mib(udev, MIB_FW_VERSION, &fwv, sizeof(fwv));
2443 if (ret < 0) {
2444 dev_printk(KERN_ERR, &interface->dev,
2445 "error %d getting firmware version\n", ret);
2446 goto error;
2447 }
2448 }
2449
2450 priv = at76_alloc_new_device(udev);
2451 if (!priv) {
2452 ret = -ENOMEM;
2453 goto error;
2454 }
2455
2456 usb_set_intfdata(interface, priv);
2457
2458 memcpy(&priv->fw_version, &fwv, sizeof(struct mib_fw_version));
2459 priv->board_type = board_type;
2460
2461 ret = at76_init_new_device(priv, interface);
2462 if (ret < 0)
2463 at76_delete_device(priv);
2464
2465 return ret;
2466
2467error:
2468 usb_put_dev(udev);
2469 return ret;
2470}
2471
2472static void at76_disconnect(struct usb_interface *interface)
2473{
2474 struct at76_priv *priv;
2475
2476 priv = usb_get_intfdata(interface);
2477 usb_set_intfdata(interface, NULL);
2478
2479 /* Disconnect after loading internal firmware */
2480 if (!priv)
2481 return;
2482
Joe Perchesc96c31e2010-07-26 14:39:58 -07002483 wiphy_info(priv->hw->wiphy, "disconnecting\n");
Kalle Valo1264b952009-02-07 09:27:10 +02002484 at76_delete_device(priv);
2485 dev_printk(KERN_INFO, &interface->dev, "disconnected\n");
2486}
2487
2488/* Structure for registering this driver with the USB subsystem */
2489static struct usb_driver at76_driver = {
2490 .name = DRIVER_NAME,
2491 .probe = at76_probe,
2492 .disconnect = at76_disconnect,
2493 .id_table = dev_table,
2494};
2495
2496static int __init at76_mod_init(void)
2497{
2498 int result;
2499
2500 printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION " loading\n");
2501
2502 mutex_init(&fw_mutex);
2503
2504 /* register this driver with the USB subsystem */
2505 result = usb_register(&at76_driver);
2506 if (result < 0)
2507 printk(KERN_ERR DRIVER_NAME
2508 ": usb_register failed (status %d)\n", result);
2509
2510 led_trigger_register_simple("at76_usb-tx", &ledtrig_tx);
2511 return result;
2512}
2513
2514static void __exit at76_mod_exit(void)
2515{
2516 int i;
2517
2518 printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION " unloading\n");
2519 usb_deregister(&at76_driver);
2520 for (i = 0; i < ARRAY_SIZE(firmwares); i++) {
2521 if (firmwares[i].fw)
2522 release_firmware(firmwares[i].fw);
2523 }
2524 led_trigger_unregister_simple(ledtrig_tx);
2525}
2526
Jason Andryuk2ce4f9d2009-02-18 22:41:04 +02002527module_param_named(debug, at76_debug, uint, 0600);
Kalle Valo1264b952009-02-07 09:27:10 +02002528MODULE_PARM_DESC(debug, "Debugging level");
2529
2530module_init(at76_mod_init);
2531module_exit(at76_mod_exit);
2532
2533MODULE_AUTHOR("Oliver Kurth <oku@masqmail.cx>");
2534MODULE_AUTHOR("Joerg Albert <joerg.albert@gmx.de>");
2535MODULE_AUTHOR("Alex <alex@foogod.com>");
2536MODULE_AUTHOR("Nick Jones");
2537MODULE_AUTHOR("Balint Seeber <n0_5p4m_p13453@hotmail.com>");
2538MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>");
2539MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
2540MODULE_AUTHOR("Kalle Valo <kalle.valo@iki.fi>");
Sebastian Smolorza1850452010-06-22 16:55:17 +02002541MODULE_AUTHOR("Sebastian Smolorz <sesmo@gmx.net>");
Kalle Valo1264b952009-02-07 09:27:10 +02002542MODULE_DESCRIPTION(DRIVER_DESC);
2543MODULE_LICENSE("GPL");